JedisUtil.java 31 KB

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