JedisUtil.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. package com.kmall.manager.manager.redis;
  2. import com.google.common.collect.Sets;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.stereotype.Component;
  6. import redis.clients.jedis.Jedis;
  7. import redis.clients.jedis.JedisPool;
  8. import redis.clients.jedis.JedisPoolConfig;
  9. import redis.clients.jedis.exceptions.JedisException;
  10. import java.io.Serializable;
  11. import java.util.*;
  12. @Component
  13. public class JedisUtil implements Serializable {
  14. protected static Logger logger = LoggerFactory.getLogger(JedisUtil.class);
  15. private static JedisPool jedisPool = null;
  16. static {
  17. JedisPoolConfig config = new JedisPoolConfig();
  18. /*config.setMaxIdle(Integer.parseInt(ResourceUtil.getConfigByName("redis.pool.maxIdle")));
  19. config.setMaxWaitMillis(Long.parseLong(ResourceUtil.getConfigByName("redis.pool.maxWait")));
  20. config.setMaxTotal(Integer.parseInt(ResourceUtil.getConfigByName("redis.pool.maxTotal")));
  21. config.setTestOnBorrow(Boolean.parseBoolean(ResourceUtil.getConfigByName("redis.pool.testOnBorrow")));
  22. String redisIp = ResourceUtil.getConfigByName("redis.host");
  23. int port = Integer.parseInt(ResourceUtil.getConfigByName("redis.port"));
  24. jedisPool = new JedisPool(config, redisIp, port, 10000);*/
  25. try {
  26. if (null == JedisPropertiesBuilder.instance()) {
  27. logger.error("jedis init is error.");
  28. throw new Exception("jedis init is error.");
  29. }
  30. JedisProperties jp = JedisPropertiesBuilder.instance();
  31. config.setMaxIdle(Integer.parseInt(jp.getPool().getMaxIdle()));
  32. config.setMaxWaitMillis(Long.parseLong(jp.getPool().getMaxWait()));
  33. config.setMaxTotal(Integer.parseInt(jp.getPool().getMaxTotal()));
  34. config.setTestOnBorrow(Boolean.parseBoolean(jp.getPool().getTestOnBorrow()));
  35. String redisIp = jp.getHost();
  36. int port = Integer.parseInt(jp.getPort());
  37. String password = jp.getPassword();
  38. int database = jp.getDatabase();
  39. jedisPool = new JedisPool(config, redisIp, port, 10000, password, database);
  40. logger.info("redis连接成功: {}:{}, 连接池:{}", redisIp, port, jedisPool);
  41. } catch (Exception e) {
  42. logger.error("init RedisUtils is error", e);
  43. e.printStackTrace();
  44. }
  45. }
  46. public static JedisPool getPool() {
  47. return jedisPool;
  48. }
  49. /**
  50. * 获取缓存
  51. *
  52. * @param key 键
  53. * @return 值
  54. */
  55. public static String get(String key) {
  56. String value = null;
  57. Jedis jedis = null;
  58. try {
  59. jedis = getResource();
  60. if (jedis.exists(key)) {
  61. value = jedis.get(key);
  62. value = org.apache.commons.lang.StringUtils.isNotBlank(value) && !"nil".equalsIgnoreCase(value) ? value : null;
  63. logger.debug("get {} = {}", key, value);
  64. }
  65. } catch (Exception e) {
  66. logger.warn("get {} ", key, e);
  67. } finally {
  68. returnResource(jedis);
  69. }
  70. return value;
  71. }
  72. /**
  73. * 获取缓存
  74. *
  75. * @param key 键
  76. * @return 值
  77. */
  78. public static Object getObject(String key) {
  79. Object value = null;
  80. Jedis jedis = null;
  81. try {
  82. jedis = getResource();
  83. if (jedis.exists(getBytesKey(key))) {
  84. value = toObject(jedis.get(getBytesKey(key)));
  85. logger.debug("getObject {} = {}", key, value);
  86. }
  87. } catch (Exception e) {
  88. logger.warn("getObject {} ", key, e);
  89. } finally {
  90. returnResource(jedis);
  91. }
  92. return value;
  93. }
  94. /**
  95. * 设置缓存
  96. *
  97. * @param key 键
  98. * @param value 值
  99. * @param cacheSeconds 超时时间,0为不超时
  100. * @return
  101. */
  102. public static String set(String key, String value, int cacheSeconds) {
  103. String result = null;
  104. Jedis jedis = null;
  105. try {
  106. jedis = getResource();
  107. result = jedis.set(key, value);
  108. if (cacheSeconds != 0) {
  109. jedis.expire(key, cacheSeconds);
  110. }
  111. logger.debug("set {} = {}", key, value);
  112. } catch (Exception e) {
  113. logger.warn("set {} ", key, e);
  114. } finally {
  115. returnResource(jedis);
  116. }
  117. return result;
  118. }
  119. /**
  120. * 设置缓存过期时间
  121. *
  122. * @param key 键
  123. * @param cacheSeconds 超时时间,0为不超时
  124. * @return
  125. */
  126. public static void expire(String key, int cacheSeconds) {
  127. Jedis jedis = null;
  128. try {
  129. jedis = getResource();
  130. if (jedis.exists(getBytesKey(key))) {
  131. jedis.expire(key, cacheSeconds);
  132. logger.debug("expire {} = {}", key, cacheSeconds);
  133. }
  134. } catch (Exception e) {
  135. logger.warn("expire {} ", key, e);
  136. } finally {
  137. returnResource(jedis);
  138. }
  139. }
  140. /**
  141. * 设置缓存
  142. *
  143. * @param key 键
  144. * @param value 值
  145. * @param cacheSeconds 超时时间,0为不超时
  146. * @return
  147. */
  148. public static String setObject(String key, Object value, int cacheSeconds) {
  149. String result = null;
  150. Jedis jedis = null;
  151. try {
  152. jedis = getResource();
  153. result = jedis.set(getBytesKey(key), toBytes(value));
  154. if (cacheSeconds != 0) {
  155. jedis.expire(key, cacheSeconds);
  156. }
  157. logger.debug("setObject {} = {}", key, value);
  158. } catch (Exception e) {
  159. logger.warn("setObject {} ", key, e);
  160. } finally {
  161. returnResource(jedis);
  162. }
  163. return result;
  164. }
  165. /**
  166. * key筛选
  167. *
  168. * @param pattern
  169. * @return
  170. */
  171. public static Set<String> keys(String pattern) {
  172. Set<String> result = Sets.newHashSet();
  173. Jedis jedis = null;
  174. try {
  175. jedis = getResource();
  176. result = jedis.keys(pattern);
  177. } catch (Exception e) {
  178. logger.warn("pattern {}", pattern, e);
  179. } finally {
  180. returnResource(jedis);
  181. }
  182. return result;
  183. }
  184. /**
  185. * 获取List缓存
  186. *
  187. * @param key 键
  188. * @return 值
  189. */
  190. public static List<String> getList(String key) {
  191. List<String> value = null;
  192. Jedis jedis = null;
  193. try {
  194. jedis = getResource();
  195. if (jedis.exists(key)) {
  196. value = jedis.lrange(key, 0, -1);
  197. }
  198. } catch (Exception e) {
  199. logger.warn("getList {}", key, e);
  200. } finally {
  201. returnResource(jedis);
  202. }
  203. return value;
  204. }
  205. /**
  206. * 获取List缓存
  207. *
  208. * @param key 键
  209. * @return 值
  210. */
  211. public static List<Object> getObjectList(String key) {
  212. List<Object> value = null;
  213. Jedis jedis = null;
  214. try {
  215. jedis = getResource();
  216. byte[] keybyte = getBytesKey(key);
  217. if (jedis.exists(keybyte)) {
  218. List<byte[]> list = jedis.lrange(keybyte, 0, -1);
  219. value = new ArrayList<>();
  220. for (byte[] bs : list) {
  221. value.add(toObject(bs));
  222. }
  223. logger.debug("getObjectList {} ", key, value);
  224. }
  225. } catch (Exception e) {
  226. logger.warn("getObjectList {} ", key, e);
  227. } finally {
  228. returnResource(jedis);
  229. }
  230. return value;
  231. }
  232. /**
  233. * @param keyPrefix
  234. * @return List<Object>
  235. * @Description:根据key前缀模糊查询缓存对象
  236. * @date 2016年9月5日
  237. * @author zhuliyun
  238. */
  239. public static List<Object> getObjectListByKeyPrefix(String keyPrefix) {
  240. List<Object> value = new ArrayList<>();
  241. Jedis jedis = null;
  242. try {
  243. jedis = getResource();
  244. Set<String> keys = getKeysByPrefix(keyPrefix);
  245. if (keys != null && keys.size() > 0) {
  246. for (String key : keys) {
  247. Object object = getObject(key);
  248. value.add(object);
  249. logger.debug("getObjectList {} ", key, value);
  250. }
  251. }
  252. } catch (Exception e) {
  253. logger.warn("getObjectList {} ", keyPrefix, e);
  254. } finally {
  255. returnResource(jedis);
  256. }
  257. return value;
  258. }
  259. /**
  260. * 设置List缓存
  261. *
  262. * @param key 键
  263. * @param value 值
  264. * @param cacheSeconds 超时时间,0为不超时
  265. * @return
  266. */
  267. public static long setList(String key, List<String> value, int cacheSeconds) {
  268. long result = 0;
  269. Jedis jedis = null;
  270. try {
  271. jedis = getResource();
  272. if (jedis.exists(key)) {
  273. jedis.del(key);
  274. }
  275. result = jedis.rpush(key, (String[]) value.toArray());
  276. if (cacheSeconds != 0) {
  277. jedis.expire(key, cacheSeconds);
  278. }
  279. logger.debug("setList {} ", key, value);
  280. } catch (Exception e) {
  281. logger.warn("setList {} ", key, e);
  282. } finally {
  283. returnResource(jedis);
  284. }
  285. return result;
  286. }
  287. /**
  288. * 设置List缓存
  289. *
  290. * @param key 键
  291. * @param value 值
  292. * @param cacheSeconds 超时时间,0为不超时
  293. * @return
  294. */
  295. public static long setObjectList(String key, List<Object> value,
  296. int cacheSeconds) {
  297. long result = 0;
  298. Jedis jedis = null;
  299. try {
  300. jedis = getResource();
  301. if (jedis.exists(getBytesKey(key))) {
  302. jedis.del(key);
  303. }
  304. List<byte[]> list = new ArrayList<>();
  305. for (Object o : value) {
  306. list.add(toBytes(o));
  307. }
  308. result = jedis.rpush(getBytesKey(key), (byte[][]) list.toArray());
  309. if (cacheSeconds != 0) {
  310. jedis.expire(key, cacheSeconds);
  311. }
  312. logger.debug("setObjectList {} ", key, value);
  313. } catch (Exception e) {
  314. logger.warn("setObjectList {} ", key, e);
  315. } finally {
  316. returnResource(jedis);
  317. }
  318. return result;
  319. }
  320. /**
  321. * 向List缓存中添加值
  322. *
  323. * @param key 键
  324. * @param value 值
  325. * @return
  326. */
  327. public static long listAdd(String key, String... value) {
  328. long result = 0;
  329. Jedis jedis = null;
  330. try {
  331. jedis = getResource();
  332. result = jedis.rpush(key, value);
  333. logger.debug("listAdd {} ", key, value);
  334. } catch (Exception e) {
  335. logger.warn("listAdd {} ", key, e);
  336. } finally {
  337. returnResource(jedis);
  338. }
  339. return result;
  340. }
  341. /**
  342. * 向List缓存中添加值
  343. *
  344. * @param key 键
  345. * @param value 值
  346. * @return
  347. */
  348. public static long listObjectAdd(String key, Object... value) {
  349. long result = 0;
  350. Jedis jedis = null;
  351. try {
  352. jedis = getResource();
  353. List<byte[]> list = new ArrayList<>();
  354. for (Object o : value) {
  355. list.add(toBytes(o));
  356. }
  357. result = jedis.rpush(getBytesKey(key), (byte[][]) list.toArray());
  358. logger.debug("listObjectAdd {} ", key, value);
  359. } catch (Exception e) {
  360. logger.warn("listObjectAdd {} ", key, e);
  361. } finally {
  362. returnResource(jedis);
  363. }
  364. return result;
  365. }
  366. /**
  367. * 获取缓存
  368. *
  369. * @param key 键
  370. * @return 值
  371. */
  372. public static Set<String> getSet(String key) {
  373. Set<String> value = null;
  374. Jedis jedis = null;
  375. try {
  376. jedis = getResource();
  377. if (jedis.exists(key)) {
  378. value = jedis.smembers(key);
  379. logger.debug("getSet {} ", key, value);
  380. }
  381. } catch (Exception e) {
  382. logger.warn("getSet {} ", key, e);
  383. } finally {
  384. returnResource(jedis);
  385. }
  386. return value;
  387. }
  388. /**
  389. * @param keyPrefix
  390. * @return Set<String>
  391. * @Description:获取以指定开头前缀的所有Key
  392. * @date 2016年9月5日
  393. * @author zhuliyun
  394. */
  395. public static Set<String> getKeysByPrefix(String keyPrefix) {
  396. Set<String> value = null;
  397. Jedis jedis = null;
  398. try {
  399. jedis = getResource();
  400. value = jedis.keys(keyPrefix + "*");
  401. logger.debug("getSet {} ", keyPrefix, value);
  402. } catch (Exception e) {
  403. logger.warn("getSet {} ", keyPrefix, e);
  404. } finally {
  405. returnResource(jedis);
  406. }
  407. return value;
  408. }
  409. /**
  410. * 获取缓存
  411. *
  412. * @param key 键
  413. * @return 值
  414. */
  415. public static Set<Object> getObjectSet(String key) {
  416. Set<Object> value = null;
  417. Jedis jedis = null;
  418. try {
  419. jedis = getResource();
  420. if (jedis.exists(getBytesKey(key))) {
  421. value = new HashSet<Object>();
  422. Set<byte[]> set = jedis.smembers(getBytesKey(key));
  423. for (byte[] bs : set) {
  424. value.add(toObject(bs));
  425. }
  426. logger.debug("getObjectSet {} ", key, value);
  427. }
  428. } catch (Exception e) {
  429. logger.warn("getObjectSet {} ", key, e);
  430. } finally {
  431. returnResource(jedis);
  432. }
  433. return value;
  434. }
  435. /**
  436. * 设置Set缓存
  437. *
  438. * @param key 键
  439. * @param value 值
  440. * @param cacheSeconds 超时时间,0为不超时
  441. * @return
  442. */
  443. public static long setSet(String key, Set<String> value, int cacheSeconds) {
  444. long result = 0;
  445. Jedis jedis = null;
  446. try {
  447. jedis = getResource();
  448. if (jedis.exists(key)) {
  449. jedis.del(key);
  450. }
  451. result = jedis.sadd(key, (String[]) value.toArray());
  452. if (cacheSeconds != 0) {
  453. jedis.expire(key, cacheSeconds);
  454. }
  455. logger.debug("setSet {} ", key, value);
  456. } catch (Exception e) {
  457. logger.warn("setSet {} ", key, e);
  458. } finally {
  459. returnResource(jedis);
  460. }
  461. return result;
  462. }
  463. /**
  464. * 设置Set缓存
  465. *
  466. * @param key 键
  467. * @param value 值
  468. * @param cacheSeconds 超时时间,0为不超时
  469. * @return
  470. */
  471. public static long setObjectSet(String key, Set<Object> value,
  472. int cacheSeconds) {
  473. long result = 0;
  474. Jedis jedis = null;
  475. try {
  476. jedis = getResource();
  477. if (jedis.exists(getBytesKey(key))) {
  478. jedis.del(key);
  479. }
  480. Set<byte[]> set = new HashSet<byte[]>();
  481. for (Object o : value) {
  482. set.add(toBytes(o));
  483. }
  484. result = jedis.sadd(getBytesKey(key), (byte[][]) set.toArray());
  485. if (cacheSeconds != 0) {
  486. jedis.expire(key, cacheSeconds);
  487. }
  488. logger.debug("setObjectSet {} ", key, value);
  489. } catch (Exception e) {
  490. logger.warn("setObjectSet {} ", key, e);
  491. } finally {
  492. returnResource(jedis);
  493. }
  494. return result;
  495. }
  496. /**
  497. * 向Set缓存中添加值
  498. *
  499. * @param key 键
  500. * @param value 值
  501. * @return
  502. */
  503. public static long setSetAdd(String key, String... value) {
  504. long result = 0;
  505. Jedis jedis = null;
  506. try {
  507. jedis = getResource();
  508. result = jedis.sadd(key, value);
  509. logger.debug("setSetAdd {} ", key, value);
  510. } catch (Exception e) {
  511. logger.warn("setSetAdd {} ", key, e);
  512. } finally {
  513. returnResource(jedis);
  514. }
  515. return result;
  516. }
  517. /**
  518. * 向Set缓存中添加值
  519. *
  520. * @param key 键
  521. * @param value 值
  522. * @return
  523. */
  524. public static long setSetObjectAdd(String key, Object... value) {
  525. long result = 0;
  526. Jedis jedis = null;
  527. try {
  528. jedis = getResource();
  529. Set<byte[]> set = new HashSet<byte[]>();
  530. for (Object o : value) {
  531. set.add(toBytes(o));
  532. }
  533. result = jedis.rpush(getBytesKey(key), (byte[][]) set.toArray());
  534. logger.debug("setSetObjectAdd {} ", key, value);
  535. } catch (Exception e) {
  536. logger.warn("setSetObjectAdd {} ", key, e);
  537. } finally {
  538. returnResource(jedis);
  539. }
  540. return result;
  541. }
  542. /**
  543. * 获取Map缓存
  544. *
  545. * @param key 键
  546. * @return 值
  547. */
  548. public static Map<String, String> getMap(String key) {
  549. Map<String, String> value = null;
  550. Jedis jedis = null;
  551. try {
  552. jedis = getResource();
  553. if (jedis.exists(key)) {
  554. value = jedis.hgetAll(key);
  555. logger.debug("getMap {} ", key, value);
  556. }
  557. } catch (Exception e) {
  558. logger.warn("getMap {} ", key, e);
  559. } finally {
  560. returnResource(jedis);
  561. }
  562. return value;
  563. }
  564. /**
  565. * 获取Map缓存
  566. *
  567. * @param key 键
  568. * @return 值
  569. */
  570. public static Map<String, Object> getObjectMap(String key) {
  571. Map<String, Object> value = null;
  572. Jedis jedis = null;
  573. try {
  574. jedis = getResource();
  575. if (jedis.exists(getBytesKey(key))) {
  576. value = new HashMap<String, Object>();
  577. Map<byte[], byte[]> map = jedis.hgetAll(getBytesKey(key));
  578. for (Map.Entry<byte[], byte[]> e : map.entrySet()) {
  579. value.put(String.valueOf(e.getKey()),
  580. toObject(e.getValue()));
  581. }
  582. logger.debug("getObjectMap {} ", key, value);
  583. }
  584. } catch (Exception e) {
  585. logger.warn("getObjectMap {} ", key, e);
  586. } finally {
  587. returnResource(jedis);
  588. }
  589. return value;
  590. }
  591. /**
  592. * 设置Map缓存
  593. *
  594. * @param key 键
  595. * @param value 值
  596. * @param cacheSeconds 超时时间,0为不超时
  597. * @return
  598. */
  599. public static String setMap(String key, Map<String, String> value,
  600. int cacheSeconds) {
  601. String result = null;
  602. Jedis jedis = null;
  603. try {
  604. jedis = getResource();
  605. if (jedis.exists(key)) {
  606. jedis.del(key);
  607. }
  608. result = jedis.hmset(key, value);
  609. if (cacheSeconds != 0) {
  610. jedis.expire(key, cacheSeconds);
  611. }
  612. logger.debug("setMap {} ", key, value);
  613. } catch (Exception e) {
  614. logger.warn("setMap {} ", key, e);
  615. } finally {
  616. returnResource(jedis);
  617. }
  618. return result;
  619. }
  620. /**
  621. * 设置Map缓存
  622. *
  623. * @param key 键
  624. * @param value 值
  625. * @param cacheSeconds 超时时间,0为不超时
  626. * @return
  627. */
  628. public static String setObjectMap(String key, Map<String, Object> value,
  629. int cacheSeconds) {
  630. String result = null;
  631. Jedis jedis = null;
  632. try {
  633. jedis = getResource();
  634. if (jedis.exists(getBytesKey(key))) {
  635. jedis.del(key);
  636. }
  637. Map<byte[], byte[]> map = new HashMap<byte[], byte[]>();
  638. for (Map.Entry<String, Object> e : value.entrySet()) {
  639. map.put(getBytesKey(e.getKey()), toBytes(e.getValue()));
  640. }
  641. result = jedis.hmset(getBytesKey(key), (Map<byte[], byte[]>) map);
  642. if (cacheSeconds != 0) {
  643. jedis.expire(key, cacheSeconds);
  644. }
  645. logger.debug("setObjectMap {} ", key, value);
  646. } catch (Exception e) {
  647. logger.warn("setObjectMap {} ", key, e);
  648. } finally {
  649. returnResource(jedis);
  650. }
  651. return result;
  652. }
  653. /**
  654. * 向Map缓存中添加值
  655. *
  656. * @param key 键
  657. * @param value 值
  658. * @return
  659. */
  660. public static String mapPut(String key, Map<String, String> value) {
  661. String result = null;
  662. Jedis jedis = null;
  663. try {
  664. jedis = getResource();
  665. result = jedis.hmset(key, value);
  666. logger.debug("mapPut {} ", key, value);
  667. } catch (Exception e) {
  668. logger.warn("mapPut {} ", key, e);
  669. } finally {
  670. returnResource(jedis);
  671. }
  672. return result;
  673. }
  674. /**
  675. * 向Map缓存中添加值
  676. *
  677. * @param key 键
  678. * @param value 值
  679. * @return
  680. */
  681. public static String mapObjectPut(String key, Map<String, Object> value) {
  682. String result = null;
  683. Jedis jedis = null;
  684. try {
  685. jedis = getResource();
  686. Map<byte[], byte[]> map = new HashMap<byte[], byte[]>();
  687. for (Map.Entry<String, Object> e : value.entrySet()) {
  688. map.put(getBytesKey(e.getKey()), toBytes(e.getValue()));
  689. }
  690. result = jedis.hmset(getBytesKey(key), (Map<byte[], byte[]>) map);
  691. logger.debug("mapObjectPut {} ", key, value);
  692. } catch (Exception e) {
  693. logger.warn("mapObjectPut {} ", key, e);
  694. } finally {
  695. returnResource(jedis);
  696. }
  697. return result;
  698. }
  699. /**
  700. * 移除Map缓存中的值
  701. *
  702. * @param key 键
  703. * @param mapKey 值
  704. * @return
  705. */
  706. public static long mapRemove(String key, String mapKey) {
  707. long result = 0;
  708. Jedis jedis = null;
  709. try {
  710. jedis = getResource();
  711. result = jedis.hdel(key, mapKey);
  712. logger.debug("mapRemove {} {}", key, mapKey);
  713. } catch (Exception e) {
  714. logger.warn("mapRemove {} ", key, e);
  715. } finally {
  716. returnResource(jedis);
  717. }
  718. return result;
  719. }
  720. /**
  721. * 移除Map缓存中的值
  722. *
  723. * @param key 键
  724. * @param mapKey 值
  725. * @return
  726. */
  727. public static long mapObjectRemove(String key, String mapKey) {
  728. long result = 0;
  729. Jedis jedis = null;
  730. try {
  731. jedis = getResource();
  732. result = jedis.hdel(getBytesKey(key), getBytesKey(mapKey));
  733. logger.debug("mapObjectRemove {} {}", key, mapKey);
  734. } catch (Exception e) {
  735. logger.warn("mapObjectRemove {} ", key, e);
  736. } finally {
  737. returnResource(jedis);
  738. }
  739. return result;
  740. }
  741. /**
  742. * 判断Map缓存中的Key是否存在
  743. *
  744. * @param key 键
  745. * @param mapKey 值
  746. * @return
  747. */
  748. public static boolean mapExists(String key, String mapKey) {
  749. boolean result = false;
  750. Jedis jedis = null;
  751. try {
  752. jedis = getResource();
  753. result = jedis.hexists(key, mapKey);
  754. logger.debug("mapExists {} {}", key, mapKey);
  755. } catch (Exception e) {
  756. logger.warn("mapExists {} ", key, e);
  757. } finally {
  758. returnResource(jedis);
  759. }
  760. return result;
  761. }
  762. /**
  763. * 判断Map缓存中的Key是否存在
  764. *
  765. * @param key 键
  766. * @param mapKey 值
  767. * @return
  768. */
  769. public static boolean mapObjectExists(String key, String mapKey) {
  770. boolean result = false;
  771. Jedis jedis = null;
  772. try {
  773. jedis = getResource();
  774. result = jedis.hexists(getBytesKey(key), getBytesKey(mapKey));
  775. logger.debug("mapObjectExists {} {}", key, mapKey);
  776. } catch (Exception e) {
  777. logger.warn("mapObjectExists {} ", key, e);
  778. } finally {
  779. returnResource(jedis);
  780. }
  781. return result;
  782. }
  783. /**
  784. * 删除缓存
  785. *
  786. * @param key 键
  787. * @return
  788. */
  789. public static long del(String key) {
  790. long result = 0;
  791. Jedis jedis = null;
  792. try {
  793. jedis = getResource();
  794. if (jedis.exists(key)) {
  795. result = jedis.del(key);
  796. logger.debug("del {}", key);
  797. } else {
  798. logger.debug("del {} not exists", key);
  799. }
  800. } catch (Exception e) {
  801. logger.warn("del {}", key, e);
  802. } finally {
  803. returnResource(jedis);
  804. }
  805. return result;
  806. }
  807. /**
  808. * 删除缓存
  809. *
  810. * @param key 键
  811. * @return
  812. */
  813. public static long delObject(String key) {
  814. long result = 0;
  815. Jedis jedis = null;
  816. try {
  817. jedis = getResource();
  818. if (jedis.exists(getBytesKey(key))) {
  819. result = jedis.del(getBytesKey(key));
  820. logger.debug("delObject {}", key);
  821. } else {
  822. logger.debug("delObject {} not exists", key);
  823. }
  824. } catch (Exception e) {
  825. logger.warn("delObject {}", key, e);
  826. } finally {
  827. returnResource(jedis);
  828. }
  829. return result;
  830. }
  831. /**
  832. * 缓存是否存在
  833. *
  834. * @param key 键
  835. * @return
  836. */
  837. public static boolean exists(String key) {
  838. boolean result = false;
  839. Jedis jedis = null;
  840. try {
  841. jedis = getResource();
  842. result = jedis.exists(key);
  843. logger.debug("exists {}", key);
  844. } catch (Exception e) {
  845. logger.warn("exists {}", key, e);
  846. } finally {
  847. returnResource(jedis);
  848. }
  849. return result;
  850. }
  851. /**
  852. * 缓存是否存在
  853. *
  854. * @param key 键
  855. * @return
  856. */
  857. public static boolean existsObject(String key) {
  858. boolean result = false;
  859. Jedis jedis = null;
  860. try {
  861. jedis = getResource();
  862. result = jedis.exists(getBytesKey(key));
  863. logger.debug("existsObject {}", key);
  864. } catch (Exception e) {
  865. logger.warn("existsObject {}", key, e);
  866. } finally {
  867. returnResource(jedis);
  868. }
  869. return result;
  870. }
  871. /**
  872. * 获取资源
  873. *
  874. * @return
  875. * @throws JedisException
  876. */
  877. public static Jedis getResource() throws JedisException {
  878. Jedis jedis = null;
  879. try {
  880. logger.debug("jedisPool.{}", jedisPool);
  881. jedis = jedisPool.getResource();
  882. logger.debug("getResource.", jedis);
  883. } catch (JedisException e) {
  884. logger.warn("getResource.", e);
  885. returnBrokenResource(jedis);
  886. throw e;
  887. }
  888. return jedis;
  889. }
  890. /**
  891. * 归还资源
  892. *
  893. * @param jedis
  894. */
  895. public static void returnBrokenResource(Jedis jedis) {
  896. if (jedis != null) {
  897. jedisPool.returnBrokenResource(jedis);
  898. }
  899. }
  900. /**
  901. * 释放资源
  902. *
  903. * @param jedis
  904. */
  905. public static void returnResource(Jedis jedis) {
  906. if (jedis != null) {
  907. jedisPool.returnResource(jedis);
  908. }
  909. }
  910. /**
  911. * 获取byte[]类型Key
  912. *
  913. * @param object
  914. * @return
  915. */
  916. public static byte[] getBytesKey(Object object) {
  917. if (object instanceof String) {
  918. String ob =(String) object;
  919. return ob.getBytes();
  920. } else {
  921. return ObjectUtils.serialize(object);
  922. }
  923. }
  924. /**
  925. * 获取byte[]类型Key
  926. *
  927. * @param key
  928. * @return
  929. */
  930. public static Object getObjectKey(byte[] key) {
  931. try {
  932. return String.valueOf(key);
  933. } catch (UnsupportedOperationException uoe) {
  934. try {
  935. return JedisUtil.toObject(key);
  936. } catch (UnsupportedOperationException uoe2) {
  937. uoe2.printStackTrace();
  938. }
  939. }
  940. return null;
  941. }
  942. public static boolean isBlank(String str) {
  943. return str == null || "".equals(str.trim());
  944. }
  945. /**
  946. * Object转换byte[]类型
  947. *
  948. * @param object
  949. * @return
  950. */
  951. public static byte[] toBytes(Object object) {
  952. return ObjectUtils.serialize(object);
  953. }
  954. /**
  955. * byte[]型转换Object
  956. *
  957. * @param bytes
  958. * @return
  959. */
  960. public static Object toObject(byte[] bytes) {
  961. return ObjectUtils.unserialize(bytes);
  962. }
  963. }