1
0

JedisUtil.java 28 KB

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