这里使用的是ShardedJedisPool,而不是RedisTemplate

1、配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:util="http://www.springframework.org/schema/util"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  6. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
  7. default-autowire="byName">
  8.  
  9. <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
  10. <property name="maxTotal" value="${redis.pool.maxTotal}" />
  11. <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
  12. <property name="maxIdle" value="${redis.pool.maxIdle}" />
  13. <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
  14. </bean>
  15. <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" scope="singleton">
  16. <constructor-arg index="0" ref="jedisPoolConfig" />
  17. <constructor-arg index="1">
  18. <list>
  19. <bean name="slave" class="redis.clients.jedis.JedisShardInfo">
  20. <constructor-arg index="0" value="${redis.slave.ip}" />
  21. <constructor-arg index="1" value="${redis.slave.port}" />
  22. </bean>
  23. <bean name="master" class="redis.clients.jedis.JedisShardInfo">
  24. <constructor-arg index="0" value="${redis.master.ip}" />
  25. <constructor-arg index="1" value="${redis.master.port}"/>
  26. </bean>
  27. </list>
  28. </constructor-arg>
  29. </bean>
  30. <bean id="jedisConnectionFactory"
  31. class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  32. <property name="hostName" value="${redis.master.ip}" />
  33. <property name="port" value="${redis.master.port}" />
  34. <property name="poolConfig" ref="jedisPoolConfig" />
  35. </bean>
  36. <!-- spring session 配置 -->
  37. <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
  38. <property name="maxInactiveIntervalInSeconds" value="1800"/>
  39. <property name="redisNamespace" value="ianbase" />
  40. </bean>
  41. <!--spring session 监听器-->
  42. <!-- 让Spring Session不再执行config命令 -->
  43. <util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/>
  44. </beans>

2、序列化工具

  1. import java.io.ByteArrayInputStream;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.ObjectInputStream;
  4. import java.io.ObjectOutputStream;
  5.  
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. /**
  9. * <p>
  10. * Title:对象序列化和反序列化。
  11. */
  12. public class SerializeUtil {
  13.  
  14. private static Logger LOG = LoggerFactory.getLogger(SerializeUtil.class);
  15. /**
  16. * 序列化
  17. *
  18. * @param object
  19. * @return byte[]
  20. */
  21. public static byte[] serialize(Object object) {
  22. ObjectOutputStream oos = null;
  23. ByteArrayOutputStream baos = null;
  24. if(object==null){
  25. return null;
  26. }
  27. try {
  28. // 序列化
  29. baos = new ByteArrayOutputStream();
  30. oos = new ObjectOutputStream(baos);
  31. oos.writeObject(object);
  32. byte[] bytes = baos.toByteArray();
  33. return bytes;
  34. } catch (Exception e) {
  35. LOG.error("serialize error",e);
  36. } finally {
  37. try {
  38. if (oos != null) {
  39. oos.close();
  40. }
  41. if (baos != null) {
  42. baos.close();
  43. }
  44. } catch (Exception e2) {
  45. LOG.error("serialize close error",e2);
  46. }
  47. }
  48. return null;
  49. }
  50. /**
  51. * 序列化
  52. *
  53. * @param byte[]
  54. * @return object
  55. */
  56. public static Object deserialize(byte[] bytes) {
  57. if(bytes==null){
  58. return null;
  59. }
  60. ByteArrayInputStream bais = null;
  61. try {
  62. // 反序列化
  63. bais = new ByteArrayInputStream(bytes);
  64. ObjectInputStream ois = new ObjectInputStream(bais);
  65. return ois.readObject();
  66. } catch (Exception e) {
  67. LOG.error("unserialize error",e);
  68. }
  69. return null;
  70. }
  71.  
  72. }

3、RedisClient

  1. import org.slf4j.Logger;
  2. import org.slf4j.LoggerFactory;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Component;
  5.  
  6. import SerializeUtil;
  7.  
  8. import redis.clients.jedis.Jedis;
  9. import redis.clients.jedis.ShardedJedis;
  10. import redis.clients.jedis.ShardedJedisPipeline;
  11. import redis.clients.jedis.ShardedJedisPool;
  12.  
  13. import java.nio.charset.Charset;
  14. import java.util.ArrayList;
  15. import java.util.Collection;
  16. import java.util.HashSet;
  17. import java.util.Iterator;
  18. import java.util.LinkedHashMap;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.Set;
  22.  
  23. /**
  24. * redis操作工具类
  25. */
  26. @SuppressWarnings("unchecked")
  27. @Component
  28. public class RedisClient {
  29.  
  30. private static final Logger LOG = LoggerFactory.getLogger(ShardRedisClient.class);
  31. @Autowired
  32. private ShardedJedisPool shardedJedisPool;
  33.  
  34. /**
  35. * 字符集
  36. */
  37. private final Charset charset = Charset.forName("UTF-8");
  38.  
  39. public String deserialize(byte[] bytes) {
  40. return (bytes == null ? null : new String(bytes, charset));
  41. }
  42.  
  43. public byte[] serialize(String string) {
  44. return (string == null ? null : string.getBytes(charset));
  45. }
  46.  
  47. /**
  48. * 设置单个值
  49. *
  50. * @param key
  51. * @param value
  52. * @return
  53. */
  54. public boolean set(String key, String value) {
  55. boolean flag = false;
  56. ShardedJedis shardedJedis = shardedJedisPool.getResource();
  57. try {
  58. shardedJedis.set(key, value);
  59. flag=true;
  60. } catch (Exception e) {
  61. LOG.error("set error", e);
  62. } finally {
  63. if(shardedJedis!=null){
  64. shardedJedis.close();
  65. }
  66. }
  67. return flag;
  68. }
  69. /**
  70. * 批量设置设置
  71. *
  72. * @param key
  73. * @param value
  74. * @return
  75. */
  76. public boolean setList(String key, String value) {
  77. boolean flag = false;
  78. ShardedJedis shardedJedis = shardedJedisPool.getResource();
  79. try {
  80. ShardedJedisPipeline shardedJedisPipeline=shardedJedis.pipelined();
  81. shardedJedisPipeline.set(key, value);
  82. shardedJedisPipeline.sync();
  83. flag=true;
  84. } catch (Exception e) {
  85. LOG.error("set error", e);
  86. } finally {
  87. if(shardedJedis!=null){
  88. shardedJedis.close();
  89. }
  90. }
  91. return flag;
  92. }
  93. /**
  94. * 设置单个值,并且设置过期时间
  95. *
  96. * @param key
  97. * @param value
  98. * @param second
  99. * @return
  100. */
  101. public boolean set(String key, String value, int second) {
  102. ShardedJedis shardedJedis = null;
  103. try {
  104. shardedJedis = shardedJedisPool.getResource();
  105. shardedJedis.setex(key, second, value);
  106. return true;
  107. } catch (Exception ex) {
  108. LOG.error("set error.", ex);
  109. } finally {
  110. if(shardedJedis!=null){
  111. shardedJedis.close();
  112. }
  113.  
  114. }
  115. return false;
  116. }
  117. /**
  118. * 删除所有匹配的key
  119. *
  120. * @param prefixKey
  121. * @return
  122. */
  123. public boolean delKesByPrefix(String prefixKey) {
  124. ShardedJedis jedis = null;
  125. Set<String> setResult = new HashSet<String>();
  126. try {
  127. jedis = shardedJedisPool.getResource();
  128. Iterator<Jedis> jedisIterator = jedis.getAllShards().iterator();
  129. while(jedisIterator.hasNext()){
  130. setResult = jedisIterator.next().keys(prefixKey+"*");
  131. }
  132. Iterator<String> it=setResult.iterator();
  133. while(it.hasNext()){
  134. String key=it.next();
  135. jedis.del(key);
  136. }
  137. return true;
  138. } catch (Exception e) {
  139. LOG.error("getByPrefix error", e);
  140. }finally {
  141. if(jedis!=null){
  142. jedis.close();
  143. }
  144. }
  145. return false;
  146. }
  147. /**
  148. * 获取单个值
  149. *
  150. * @param key
  151. * @return
  152. */
  153. public String get(String key) {
  154. String result = null;
  155. ShardedJedis shardedJedis = shardedJedisPool.getResource();
  156. if (shardedJedis == null) {
  157. return result;
  158. }
  159. try {
  160. result = shardedJedis.get(key);
  161. } catch (Exception e) {
  162. LOG.error("redis get error", e);
  163. } finally {
  164. if(shardedJedis!=null){
  165. shardedJedis.close();
  166. }
  167. }
  168. return result;
  169. }
  170. /**
  171. * 删除HashSet对象
  172. *
  173. * @param key
  174. * 键值
  175. * @param field
  176. * 属性
  177. * @return 删除的记录数
  178. */
  179. public long del(String key) {
  180. ShardedJedis shardedJedis = null;
  181. long count = 0;
  182. try {
  183. shardedJedis = shardedJedisPool.getResource();
  184. count = shardedJedis.del(key);
  185. } catch (Exception ex) {
  186. LOG.error("hdel error.", ex);
  187.  
  188. } finally {
  189. if(shardedJedis!=null){
  190. shardedJedis.close();
  191. }
  192. }
  193. return count;
  194. }
  195. /**
  196. * 设置一个key的过期时间(单位:秒)
  197. *
  198. * @param key
  199. * key值
  200. * @param seconds
  201. * 多少秒后过期
  202. * @return 1:设置了过期时间, 0:没有设置过期时间/不能设置过期时间
  203. */
  204. public long expire(String key, int seconds) {
  205. if (key == null || key.equals("")) {
  206. return 0;
  207. }
  208. ShardedJedis shardedJedis = null;
  209. try {
  210. shardedJedis = shardedJedisPool.getResource();
  211. return shardedJedis.expire(key, seconds);
  212. } catch (Exception ex) {
  213. LOG.error("EXPIRE error[key=" + key + " seconds=" + seconds + "]" + ex.getMessage(), ex);
  214. } finally {
  215. if(shardedJedis!=null){
  216. shardedJedis.close();
  217. }
  218. }
  219. return 0;
  220. }
  221. /**
  222. * 设置HashSet对象
  223. *
  224. * @param key
  225. * 键值
  226. * @param field
  227. * 属性
  228. * @param value
  229. * Json String or String value
  230. * @return
  231. */
  232. public boolean hset( String key,String field, String value) {
  233. if (value == null)
  234. return false;
  235. ShardedJedis shardedJedis = null;
  236. try {
  237. shardedJedis = shardedJedisPool.getResource();
  238. shardedJedis.hset(key, field, value);
  239. return true;
  240. } catch (Exception ex) {
  241. LOG.error("hset error.", ex);
  242. } finally {
  243. if(shardedJedis!=null){
  244. shardedJedis.close();
  245. }
  246. }
  247. return false;
  248. }
  249. /**
  250. * 设置HashSet对象
  251. *
  252. * @param key
  253. * 键值
  254. * @param field
  255. * 属性
  256. * @param value
  257. * Json String or String value
  258. * @return
  259. */
  260. public boolean hmset(String key, Map<String ,String> map ) {
  261. if (map == null)
  262. return false;
  263. ShardedJedis shardedJedis = null;
  264. try {
  265. shardedJedis = shardedJedisPool.getResource();
  266. shardedJedis.hmset(key, map);
  267. return true;
  268. } catch (Exception ex) {
  269. LOG.error("hmset error.", ex);
  270. } finally {
  271. if(shardedJedis!=null){
  272. shardedJedis.close();
  273. }
  274. }
  275. return false;
  276. }
  277. /**
  278. * 设置HashSet对象
  279. *
  280. * @param key
  281. * 键值
  282. * @param field
  283. * 属性
  284. * @param value
  285. * Json String or String value
  286. * @return
  287. */
  288. public boolean hmsetObject(String key, Map<String ,Object> map ) {
  289. if (map == null)
  290. return false;
  291. ShardedJedis shardedJedis = null;
  292. try {
  293. shardedJedis = shardedJedisPool.getResource();
  294. final Map<byte[], byte[]> hashes = new LinkedHashMap<byte[], byte[]>(map.size());
  295.  
  296. for (Map.Entry<String, Object> entry : map.entrySet()) {
  297. hashes.put(serialize(entry.getKey()), SerializeUtil.serialize(entry.getValue()));
  298. }
  299. shardedJedis.hmset(serialize(key), hashes);
  300. return true;
  301. } catch (Exception ex) {
  302. LOG.error("hmset error.", ex);
  303. } finally {
  304. if(shardedJedis!=null){
  305. shardedJedis.close();
  306. }
  307. }
  308. return false;
  309. }
  310.  
  311. /**
  312. * 设置HashSet对象
  313. *
  314. * @param key
  315. * 键值
  316. * @param field
  317. * 属性
  318. * @param value
  319. * Json String or String value
  320. * @return
  321. */
  322. public <T> boolean hmsetObjectClass(String key, Map<String ,T> map) {
  323. if (map == null)
  324. return false;
  325. ShardedJedis shardedJedis = null;
  326. try {
  327. shardedJedis = shardedJedisPool.getResource();
  328. final Map<byte[], byte[]> hashes = new LinkedHashMap<byte[], byte[]>(map.size());
  329.  
  330. for (Map.Entry<String, T> entry : map.entrySet()) {
  331. hashes.put(serialize(entry.getKey()), SerializeUtil.serialize(entry.getValue()));
  332. }
  333. shardedJedis.hmset(serialize(key), hashes);
  334. return true;
  335. } catch (Exception ex) {
  336. LOG.error("hmset error.", ex);
  337. } finally {
  338. if(shardedJedis!=null){
  339. shardedJedis.close();
  340. }
  341. }
  342. return false;
  343. }
  344.  
  345. /**
  346. * 设置HashSet对象 ,并且设置过期时间
  347. *
  348. * @param key
  349. * 键值
  350. * @param map
  351. *
  352. * @param seconds
  353. * 过期时间,单位秒。
  354. * @return
  355. */
  356. public boolean hmset(String key, Map<String ,String> map,int seconds ) {
  357. if(this.hmset(key, map)){
  358. return this.expire(key, seconds)==1;
  359. }
  360. return false;
  361. }
  362.  
  363. /**
  364. * 获得HashSet对象
  365. *
  366. * @param key
  367. * 键值
  368. * @param field
  369. * 属性
  370. * @return Json String or String value
  371. */
  372. public String hget(String key, String field) {
  373. ShardedJedis shardedJedis = null;
  374. try {
  375. shardedJedis = shardedJedisPool.getResource();
  376. return shardedJedis.hget(key, field);
  377. } catch (Exception ex) {
  378. LOG.error("hget error.", ex);
  379.  
  380. } finally {
  381. if(shardedJedis!=null){
  382. shardedJedis.close();
  383. }
  384. }
  385. return null;
  386. }
  387. /**
  388. * 获得HashSet对象
  389. *
  390. * @param key
  391. * 键值
  392. * @param field
  393. * 属性
  394. * @return Json String or String value
  395. */
  396. public Object hgetObject(String key, String field) {
  397. ShardedJedis shardedJedis = null;
  398. try {
  399. shardedJedis = shardedJedisPool.getResource();
  400. byte[] value=shardedJedis.hget(serialize(key), serialize(field));
  401. return SerializeUtil.deserialize(value);
  402. } catch (Exception ex) {
  403. LOG.error("hget error.", ex);
  404.  
  405. } finally {
  406. if(shardedJedis!=null){
  407. shardedJedis.close();
  408. }
  409. }
  410. return null;
  411. }
  412. /**
  413. * 获得HashSet对象
  414. *
  415. * @param key
  416. * 键值
  417. * @return Json String or String value
  418. */
  419. public Map<String,Object> hgetMap(String key) {
  420. ShardedJedis shardedJedis = null;
  421. try {
  422. shardedJedis = shardedJedisPool.getResource();
  423. Map<byte[], byte[]> entries =shardedJedis.hgetAll(serialize(key));
  424. Map<String,Object> map = new LinkedHashMap<String,Object>(entries.size());
  425.  
  426. for (Map.Entry<byte[], byte[]> entry : entries.entrySet()) {
  427. map.put(deserialize(entry.getKey()), SerializeUtil.deserialize(entry.getValue()));
  428. }
  429. return map;
  430. } catch (Exception ex) {
  431. LOG.error("hgetAll error.", ex);
  432.  
  433. } finally {
  434. if(shardedJedis!=null){
  435. shardedJedis.close();
  436. }
  437. }
  438. return null;
  439. }
  440.  
  441. /**
  442. * 获得缓存的Map,指定class
  443. *
  444. * @param key
  445. * @return
  446. */
  447. public <T> Map<String, T> hgetMapClass(Class<T> t,String key) {
  448. ShardedJedis shardedJedis = null;
  449. try {
  450. shardedJedis = shardedJedisPool.getResource();
  451. Map<byte[], byte[]> entries =shardedJedis.hgetAll(serialize(key));
  452. Map map = new LinkedHashMap<String,Object>(entries.size());
  453.  
  454. for (Map.Entry<byte[], byte[]> entry : entries.entrySet()) {
  455. map.put(deserialize(entry.getKey()), t.cast(SerializeUtil.deserialize(entry.getValue())));
  456. }
  457. return map;
  458. } catch (Exception ex) {
  459. LOG.error("hgetAll error.", ex);
  460. } finally {
  461. if(shardedJedis!=null){
  462. shardedJedis.close();
  463. }
  464. }
  465. return null;
  466. }
  467. /**
  468. * 删除HashSet对象
  469. *
  470. * @param key
  471. * 键值
  472. * @param field
  473. * 属性值
  474. * @return 删除的记录数
  475. */
  476. public long hdel(String key,String field) {
  477. ShardedJedis shardedJedis = null;
  478. long count = 0;
  479. try {
  480. shardedJedis = shardedJedisPool.getResource();
  481. count = shardedJedis.hdel(key,field);
  482. } catch (Exception ex) {
  483. LOG.error("hdel error.", ex);
  484.  
  485. } finally {
  486. if(shardedJedis!=null){
  487. shardedJedis.close();
  488. }
  489. }
  490. return count;
  491. }
  492.  
  493. /**
  494. * 删除HashSet对象
  495. *
  496. * @param key
  497. * 键值
  498. * @param field
  499. * 属性
  500. * @return 删除的记录数
  501. */
  502. public long hdel(String key, String... field) {
  503. ShardedJedis shardedJedis = null;
  504. long count = 0;
  505. try {
  506. shardedJedis = shardedJedisPool.getResource();
  507. count = shardedJedis.hdel(key,field);
  508. } catch (Exception ex) {
  509. LOG.error("hdel error.", ex);
  510.  
  511. } finally {
  512. if(shardedJedis!=null){
  513. shardedJedis.close();
  514. }
  515. }
  516. return count;
  517. }
  518.  
  519. /**
  520. * 判断key是否存在
  521. *
  522. * @param key
  523. * 键值
  524. * @param field
  525. * 属性
  526. * @return
  527. */
  528. public boolean hexists(String key, String field) {
  529. ShardedJedis shardedJedis = null;
  530. boolean isExist = false;
  531. try {
  532. shardedJedis = shardedJedisPool.getResource();
  533. isExist = shardedJedis.hexists(key,field);
  534. } catch (Exception ex) {
  535. LOG.error("hexists error.", ex);
  536.  
  537. } finally {
  538. if(shardedJedis!=null){
  539. shardedJedis.close();
  540. }
  541. }
  542. return isExist;
  543. }
  544.  
  545. /**
  546. * 判断key是否存在
  547. *
  548. * @param key
  549. * 键值
  550. * @return
  551. */
  552. public boolean exists(String key) {
  553. ShardedJedis shardedJedis = null;
  554. boolean isExist = false;
  555. try {
  556. shardedJedis = shardedJedisPool.getResource();
  557. isExist = shardedJedis.exists(key);
  558. } catch (Exception ex) {
  559. LOG.error("hexists error.", ex);
  560.  
  561. } finally {
  562. if(shardedJedis!=null){
  563. shardedJedis.close();
  564. }
  565. }
  566. return isExist;
  567. }
  568.  
  569. /**
  570. * 返回 key 指定的哈希集中所有字段的value值
  571. *
  572. * @param key
  573. * @return
  574. */
  575.  
  576. public List<String> hvals(String key) {
  577. ShardedJedis shardedJedis = null;
  578. List<String> retList = null;
  579. try {
  580. shardedJedis = shardedJedisPool.getResource();
  581. retList = shardedJedis.hvals(key);
  582. } catch (Exception ex) {
  583. LOG.error("hvals error.", ex);
  584.  
  585. } finally {
  586. if(shardedJedis!=null){
  587. shardedJedis.close();
  588. }
  589. }
  590. return retList;
  591. }
  592. /**
  593. * 返回 key 指定的哈希集中所有字段的value值
  594. *
  595. * @param key
  596. * @return
  597. */
  598.  
  599. public <T> List<T> hvalsObject(String key) {
  600. ShardedJedis shardedJedis = null;
  601. List<Object> retList = new ArrayList<Object>();
  602. try {
  603. shardedJedis = shardedJedisPool.getResource();
  604. Collection<byte[]> byteList=shardedJedis.hvals(serialize(key));
  605. Iterator<byte[]> it=byteList.iterator();
  606. while(it.hasNext()){
  607. retList.add(SerializeUtil.deserialize(it.next()));
  608. }
  609. } catch (Exception ex) {
  610. LOG.error("hvals error.", ex);
  611.  
  612. } finally {
  613. if(shardedJedis!=null){
  614. shardedJedis.close();
  615. }
  616. }
  617. return (List<T>)retList;
  618. }
  619.  
  620. /**
  621. * 返回 key 指定的哈希集中所有字段的field值
  622. *
  623. * @param key
  624. * @return
  625. */
  626.  
  627. public Set<String> hkeys(String key) {
  628. ShardedJedis shardedJedis = null;
  629. Set<String> retList = null;
  630. try {
  631. shardedJedis = shardedJedisPool.getResource();
  632. retList = shardedJedis.hkeys(key);
  633. } catch (Exception ex) {
  634. LOG.error("hkeys error.", ex);
  635.  
  636. } finally {
  637. if(shardedJedis!=null){
  638. shardedJedis.close();
  639. }
  640. }
  641. return retList;
  642. }
  643.  
  644. /**
  645. * 返回 key 指定的哈希field值总数
  646. *
  647. * @param key
  648. * @return
  649. */
  650. public long hlen(String key) {
  651. ShardedJedis shardedJedis = null;
  652. long retList = 0;
  653. try {
  654. shardedJedis = shardedJedisPool.getResource();
  655. retList = shardedJedis.hlen(key);
  656. } catch (Exception ex) {
  657. LOG.error("hkeys error.", ex);
  658.  
  659. } finally {
  660. if(shardedJedis!=null){
  661. shardedJedis.close();
  662. }
  663. }
  664. return retList;
  665. }
  666.  
  667. /**
  668. * 设置排序集合
  669. *
  670. * @param key
  671. * @param score
  672. * @param value
  673. * @return
  674. */
  675. public boolean setSortedSet(String key, long score, String value) {
  676. ShardedJedis shardedJedis = null;
  677. try {
  678. shardedJedis = shardedJedisPool.getResource();
  679. shardedJedis.zadd(key, score, value);
  680. return true;
  681. } catch (Exception ex) {
  682. LOG.error("setSortedSet error.", ex);
  683.  
  684. } finally {
  685. if(shardedJedis!=null){
  686. shardedJedis.close();
  687. }
  688. }
  689. return false;
  690. }
  691.  
  692. /**
  693. * 获得排序集合
  694. *
  695. * @param key
  696. * @param startScore
  697. * @param endScore
  698. * @param orderByDesc
  699. * @return
  700. */
  701. public Set<String> getSoredSet(String key, long startScore, long endScore, boolean orderByDesc) {
  702. ShardedJedis shardedJedis = null;
  703. try {
  704. shardedJedis = shardedJedisPool.getResource();
  705. if (orderByDesc) {
  706. return shardedJedis.zrevrangeByScore(key, endScore, startScore);
  707. } else {
  708. return shardedJedis.zrangeByScore(key, startScore, endScore);
  709. }
  710. } catch (Exception ex) {
  711. LOG.error("getSoredSet error.", ex);
  712.  
  713. } finally {
  714. if(shardedJedis!=null){
  715. shardedJedis.close();
  716. }
  717. }
  718. return null;
  719. }
  720.  
  721. /**
  722. * 计算排序长度
  723. *
  724. * @param key
  725. * @param startScore
  726. * @param endScore
  727. * @return
  728. */
  729. public long zcount(String key, long startScore, long endScore) {
  730. ShardedJedis shardedJedis = null;
  731. try {
  732. shardedJedis = shardedJedisPool.getResource();
  733. Long count = shardedJedis.zcount(key, startScore, endScore);
  734. return count == null ? 0L : count;
  735. } catch (Exception ex) {
  736. LOG.error("zcount error.", ex);
  737.  
  738. } finally {
  739. if(shardedJedis!=null){
  740. shardedJedis.close();
  741. }
  742. }
  743. return 0L;
  744. }
  745.  
  746. /**
  747. * 删除排序集合
  748. *
  749. * @param key
  750. * @param value
  751. * @return
  752. */
  753. public boolean zrem(String key, String value) {
  754. ShardedJedis shardedJedis = null;
  755. try {
  756. shardedJedis = shardedJedisPool.getResource();
  757. long count = shardedJedis.zrem(key, value);
  758. return count > 0;
  759. } catch (Exception ex) {
  760. LOG.error("zrem error.", ex);
  761.  
  762. } finally {
  763. if(shardedJedis!=null){
  764. shardedJedis.close();
  765. }
  766. }
  767. return false;
  768. }
  769.  
  770. /**
  771. * 获得排序集合
  772. *
  773. * @param key
  774. * @param startRange
  775. * @param endRange
  776. * @param orderByDesc
  777. * @return
  778. */
  779. public Set<String> zrange(String key, int startRange, int endRange, boolean orderByDesc) {
  780. ShardedJedis shardedJedis = null;
  781. try {
  782. shardedJedis = shardedJedisPool.getResource();
  783. if (orderByDesc) {
  784. return shardedJedis.zrevrange(key, startRange, endRange);
  785. } else {
  786. return shardedJedis.zrange(key, startRange, endRange);
  787. }
  788. } catch (Exception ex) {
  789. LOG.error("zrange error.", ex);
  790.  
  791. } finally {
  792. if(shardedJedis!=null){
  793. shardedJedis.close();
  794. }
  795. }
  796. return null;
  797. }
  798.  
  799. /**
  800. * 获得排序打分
  801. *
  802. * @param key
  803. * @return
  804. */
  805. public Double zscore(String key, String member) {
  806. ShardedJedis shardedJedis = null;
  807. try {
  808. shardedJedis = shardedJedisPool.getResource();
  809. return shardedJedis.zscore(key, member);
  810. } catch (Exception ex) {
  811. LOG.error("zscore error.", ex);
  812.  
  813. } finally {
  814. if(shardedJedis!=null){
  815. shardedJedis.close();
  816. }
  817. }
  818. return null;
  819. }
  820. /**
  821. * 从list中删除value 默认count 1
  822. *
  823. * @param key
  824. * @param values
  825. * 值list
  826. * @return
  827. */
  828. public int lrem(String key, List<String> values) {
  829. return lrem(key, 1, values);
  830. }
  831.  
  832. /**
  833. * 从list中删除value
  834. *
  835. * @param key
  836. * @param count
  837. * @param values
  838. * 值list
  839. * @return
  840. */
  841. public int lrem(String key, long count, List<String> values) {
  842. int result = 0;
  843. if (values != null && values.size() > 0) {
  844. for (String value : values) {
  845. if (lrem(key, count, value)) {
  846. result++;
  847. }
  848. }
  849. }
  850. return result;
  851. }
  852.  
  853. /**
  854. * 从list中删除value
  855. *
  856. * @param key
  857. * @param count
  858. * 要删除个数
  859. * @param value
  860. * @return
  861. */
  862. public boolean lrem(String key, long count, String value) {
  863. ShardedJedis shardedJedis = null;
  864. try {
  865. shardedJedis = shardedJedisPool.getResource();
  866. shardedJedis.lrem(key, count, value);
  867. return true;
  868. } catch (Exception ex) {
  869. LOG.error("lrem error.", ex);
  870.  
  871. } finally {
  872. if(shardedJedis!=null){
  873. shardedJedis.close();
  874. }
  875. }
  876. return false;
  877. }
  878.  
  879. /**
  880. * 截取List
  881. *
  882. * @param key
  883. * @param start
  884. * 起始位置
  885. * @param end
  886. * 结束位置
  887. * @return
  888. */
  889. public List<String> lrange(String key, long start, long end) {
  890. if (key == null || key.equals("")) {
  891. return null;
  892. }
  893. ShardedJedis shardedJedis = null;
  894. try {
  895. shardedJedis = shardedJedisPool.getResource();
  896. return shardedJedis.lrange(key, start, end);
  897. } catch (Exception ex) {
  898. LOG.error("lrange 出错[key=" + key + " start=" + start + " end=" + end + "]" + ex.getMessage(), ex);
  899.  
  900. } finally {
  901. if(shardedJedis!=null){
  902. shardedJedis.close();
  903. }
  904. }
  905. return null;
  906. }
  907.  
  908. /**
  909. * 检查List长度
  910. *
  911. * @param key
  912. * @return
  913. */
  914. public long llen(String key) {
  915. if (key == null) {
  916. return 0;
  917. }
  918. ShardedJedis shardedJedis = null;
  919. try {
  920. shardedJedis = shardedJedisPool.getResource();
  921. return shardedJedis.llen(key);
  922. } catch (Exception ex) {
  923. LOG.error("llen error.", ex);
  924.  
  925. } finally {
  926. if(shardedJedis!=null){
  927. shardedJedis.close();
  928. }
  929. }
  930. return 0;
  931. }
  932.  
  933. /**
  934. * 添加到List中(同时设置过期时间)
  935. *
  936. * @param key
  937. * key值
  938. * @param seconds
  939. * 过期时间 单位s
  940. * @param value
  941. * @return
  942. */
  943. public boolean lpush(String key, int seconds, String... value) {
  944. boolean result = lpush(key, value);
  945. if (result) {
  946. long i = expire(key, seconds);
  947. return i == 1;
  948. }
  949. return false;
  950. }
  951.  
  952. /**
  953. * 添加到List
  954. *
  955. * @param key
  956. * @param value
  957. * @return
  958. */
  959. public boolean lpush(String key, String... value) {
  960. if (key == null || value == null) {
  961. return false;
  962. }
  963. ShardedJedis shardedJedis = null;
  964. try {
  965. shardedJedis = shardedJedisPool.getResource();
  966. shardedJedis.lpush(key, value);
  967. return true;
  968. } catch (Exception ex) {
  969. LOG.error("lpush error.", ex);
  970.  
  971. } finally {
  972. if(shardedJedis!=null){
  973. shardedJedis.close();
  974. }
  975. }
  976. return false;
  977. }
  978.  
  979. /**
  980. * 添加到List(只新增)
  981. *
  982. * @param key
  983. * @param value
  984. * @return
  985. */
  986. public boolean lpush(String key, List<String> list) {
  987. if (key == null || list == null || list.size() == 0) {
  988. return false;
  989. }
  990. for (String value : list) {
  991. lpush(key, value);
  992. }
  993. return true;
  994. }
  995. /**
  996. * 截断一个List
  997. *
  998. * @param key
  999. * 列表key
  1000. * @param start
  1001. * 开始位置 从0开始
  1002. * @param end
  1003. * 结束位置
  1004. * @return 状态码
  1005. */
  1006. public String ltrim(String key, long start, long end) {
  1007. if (key == null || key.equals("")) {
  1008. return "-";
  1009. }
  1010. ShardedJedis shardedJedis = null;
  1011. try {
  1012. shardedJedis = shardedJedisPool.getResource();
  1013. return shardedJedis.ltrim(key, start, end);
  1014. } catch (Exception ex) {
  1015. LOG.error("ltrim 出错[key=" + key + " start=" + start + " end=" + end + "]" + ex.getMessage(), ex);
  1016.  
  1017. } finally {
  1018. if(shardedJedis!=null){
  1019. shardedJedis.close();
  1020. }
  1021. }
  1022. return "-";
  1023. }
  1024.  
  1025. /**
  1026. * 检查Set长度
  1027. *
  1028. * @param key
  1029. * @return
  1030. */
  1031. public long scard(String key) {
  1032. if (key == null) {
  1033. return 0;
  1034. }
  1035. ShardedJedis shardedJedis = null;
  1036. try {
  1037. shardedJedis = shardedJedisPool.getResource();
  1038. return shardedJedis.scard(key);
  1039. } catch (Exception ex) {
  1040. LOG.error("scard error.", ex);
  1041.  
  1042. } finally {
  1043. if(shardedJedis!=null){
  1044. shardedJedis.close();
  1045. }
  1046. }
  1047. return 0;
  1048. }
  1049.  
  1050. /**
  1051. * 添加到Set中(同时设置过期时间)
  1052. *
  1053. * @param key
  1054. * key值
  1055. * @param seconds
  1056. * 过期时间 单位s
  1057. * @param value
  1058. * @return 成功true
  1059. */
  1060. public boolean sadd(String key, int seconds, String... value) {
  1061. boolean result = sadd(key, value);
  1062. if (result) {
  1063. long i = expire(key, seconds);
  1064. return i == 1;
  1065. }
  1066. return false;
  1067. }
  1068.  
  1069. /**
  1070. * 添加到Set中
  1071. *
  1072. * @param key
  1073. * @param value
  1074. * @return
  1075. */
  1076. public boolean sadd(String key, String... value) {
  1077. if (key == null || value == null) {
  1078. return false;
  1079. }
  1080. ShardedJedis shardedJedis = null;
  1081. try {
  1082. shardedJedis = shardedJedisPool.getResource();
  1083. shardedJedis.sadd(key, value);
  1084. return true;
  1085. } catch (Exception ex) {
  1086. LOG.error("setList error.", ex);
  1087.  
  1088. } finally {
  1089. if(shardedJedis!=null){
  1090. shardedJedis.close();
  1091. }
  1092. }
  1093. return false;
  1094. }
  1095. /**
  1096. * 添加到Set中
  1097. *
  1098. * @param key
  1099. * @param value
  1100. * @return
  1101. */
  1102. public boolean saddObject(String key, Object... values) {
  1103. if (key == null || values == null) {
  1104. return false;
  1105. }
  1106. int length=values.length;
  1107. byte[][] array=new byte[length][];
  1108.  
  1109. ShardedJedis shardedJedis = null;
  1110. try {
  1111. shardedJedis = shardedJedisPool.getResource();
  1112. int i = 0;
  1113. for(Object value : values){
  1114. array[i++]=SerializeUtil.serialize(value);
  1115. }
  1116. shardedJedis.sadd(serialize(key), array);
  1117. return true;
  1118. } catch (Exception ex) {
  1119. LOG.error("setList error.", ex);
  1120.  
  1121. } finally {
  1122. if(shardedJedis!=null){
  1123. shardedJedis.close();
  1124. }
  1125. }
  1126. return false;
  1127. }
  1128.  
  1129. /**
  1130. * @param key
  1131. * @param value
  1132. * @return 判断值是否包含在set中
  1133. */
  1134. public boolean sismember(String key, String value) {
  1135. if (key == null || value == null) {
  1136. return false;
  1137. }
  1138. ShardedJedis shardedJedis = null;
  1139. try {
  1140. shardedJedis = shardedJedisPool.getResource();
  1141. return shardedJedis.sismember(key, value);
  1142. } catch (Exception ex) {
  1143. LOG.error("sismember error.", ex);
  1144.  
  1145. } finally {
  1146. if(shardedJedis!=null){
  1147. shardedJedis.close();
  1148. }
  1149. }
  1150. return false;
  1151. }
  1152.  
  1153. /**
  1154. * 获取Set
  1155. *
  1156. * @param key
  1157. * @return
  1158. */
  1159. public Set<String> smembers(String key) {
  1160. ShardedJedis shardedJedis = null;
  1161. try {
  1162. shardedJedis = shardedJedisPool.getResource();
  1163. return shardedJedis.smembers(key);
  1164. } catch (Exception ex) {
  1165. LOG.error("smembers error.", ex);
  1166.  
  1167. } finally {
  1168. if(shardedJedis!=null){
  1169. shardedJedis.close();
  1170. }
  1171. }
  1172. return null;
  1173. }
  1174. /**
  1175. * 获取Set
  1176. *
  1177. * @param key
  1178. * @return
  1179. */
  1180. public List<Object> smembersObjct(String key) {
  1181. List<Object> list = new ArrayList<Object>();
  1182. Set<byte[]> set = smembersByte(key);
  1183. Iterator<byte[]> it = set.iterator();
  1184. while (it.hasNext()) {
  1185. list.add(SerializeUtil.deserialize(it.next()));
  1186. }
  1187. return list;
  1188.  
  1189. }
  1190. /**
  1191. * 获取Set
  1192. *
  1193. * @param key
  1194. * @return
  1195. */
  1196. public Set<byte[]> smembersByte(String key) {
  1197. ShardedJedis shardedJedis = null;
  1198. try {
  1199. shardedJedis = shardedJedisPool.getResource();
  1200. return shardedJedis.smembers(key.getBytes(charset));
  1201. } catch (Exception ex) {
  1202. LOG.error("smembers error.", ex);
  1203.  
  1204. } finally {
  1205. if(shardedJedis!=null){
  1206. shardedJedis.close();
  1207. }
  1208. }
  1209. return null;
  1210. }
  1211.  
  1212. /**
  1213. * 从set中删除value
  1214. *
  1215. * @param key
  1216. * @return
  1217. */
  1218. public boolean srem(String key, String... value) {
  1219. ShardedJedis shardedJedis = null;
  1220. try {
  1221. shardedJedis = shardedJedisPool.getResource();
  1222. shardedJedis.srem(key, value);
  1223. return true;
  1224. } catch (Exception ex) {
  1225. LOG.error("getList error.", ex);
  1226.  
  1227. } finally {
  1228. if(shardedJedis!=null){
  1229. shardedJedis.close();
  1230. }
  1231. }
  1232. return false;
  1233. }
  1234.  
  1235. /**
  1236. * 获取List
  1237. *
  1238. * @param key
  1239. * @return
  1240. */
  1241. public List<String> lrange(String key) {
  1242. ShardedJedis shardedJedis = null;
  1243. try {
  1244. shardedJedis = shardedJedisPool.getResource();
  1245. return shardedJedis.lrange(key, 0, -1);
  1246. } catch (Exception ex) {
  1247. LOG.error("lrange error.", ex);
  1248.  
  1249. } finally {
  1250. if(shardedJedis!=null){
  1251. shardedJedis.close();
  1252. }
  1253. }
  1254. return null;
  1255. }
  1256. /**
  1257. * 获取List
  1258. *
  1259. * @param key
  1260. * @return
  1261. */
  1262. public List<Object> lrangeObject(String key) {
  1263. List<Object> objList=new ArrayList<Object>();
  1264. List<byte[]> list=lrangeByte( key) ;
  1265. if(list !=null &&list.size()>0){
  1266. for (int i=0;i<list.size();i++){
  1267. objList.add(SerializeUtil.deserialize(list.get(i)));
  1268. }
  1269. }
  1270. return objList;
  1271. }
  1272. /**
  1273. * 获取List
  1274. *
  1275. * @param key
  1276. * @return
  1277. */
  1278. public List<byte[]> lrangeByte(String key) {
  1279. ShardedJedis shardedJedis = null;
  1280. try {
  1281. shardedJedis = shardedJedisPool.getResource();
  1282.  
  1283. return shardedJedis.lrange(key.getBytes(charset), 0, -1);
  1284.  
  1285. } catch (Exception ex) {
  1286. LOG.error("lrange error.", ex);
  1287.  
  1288. } finally {
  1289. if(shardedJedis!=null){
  1290. shardedJedis.close();
  1291. }
  1292. }
  1293. return null;
  1294. }
  1295.  
  1296. public Object getValue(String key) {
  1297. Object ret = null;
  1298. ShardedJedis jedis = shardedJedisPool.getResource();
  1299. try {
  1300.  
  1301. // 去redis中取回序列化后的对象
  1302. byte[] obj = jedis.get(serialize(key));
  1303.  
  1304. // 取回的对象反序列化
  1305. if (obj != null) {
  1306. ret = SerializeUtil.deserialize(obj);
  1307. }
  1308. } catch (Exception e) {
  1309. LOG.error("get error.", e);
  1310. } finally {
  1311. if(jedis!=null){
  1312. jedis.close();
  1313. }
  1314. }
  1315. return ret;
  1316. }
  1317. }

shardedJedisPool工具类的更多相关文章

  1. redis 工具类 单个redis、JedisPool 及多个redis、shardedJedisPool与spring的集成配置

    http://www.cnblogs.com/edisonfeng/p/3571870.html http://javacrazyer.iteye.com/blog/1840161 http://ww ...

  2. redis分布式工具类 ----RedisShardedPoolUtil

    这个是redis分布式的工具类,看非分布式的看  这里 说一下redis的分布式,分布式,无疑,肯定不是一台redis服务器.假如说,我们有两台redis服务器,一个6379端口,一个6380端口.那 ...

  3. Java操作Redis工具类

    依赖 jar 包 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...

  4. java中的redis工具类

    1.redis基础类 package com.qlchat.component.redis.template; import javax.annotation.PostConstruct; impor ...

  5. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  6. Android—关于自定义对话框的工具类

    开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...

  7. [转]Java常用工具类集合

    转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...

  8. js常用工具类.

    一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...

  9. Guava库介绍之实用工具类

    作者:Jack47 转载请保留作者和原文出处 欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 本文是我写的Google开源的Java编程库Guava系列之一,主要介 ...

随机推荐

  1. cordova 开发 android app 简要流程

    1. 安装cordova:npm install -g cordova --registry=https://registry.npm.taobao.org 2. 创建cordova工程:进入工作目录 ...

  2. Matlab GUI读入图片

    % --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, han ...

  3. PrintDocument打印、预览、打印机设置和打印属性的方法(较完整) .

    private void Form1_Load(object sender, System.EventArgs e) { //获取或设置一个值,该值指示是否发送到文件或端口 printDocument ...

  4. 关于Visio的vba操作,遍历目录,对所有vsd文件操作,导入excel文件

    1.vba遍历要添加引用,runtime 2.不能打开单独的application,因为在获取到shape的picture属性时候,新打开的application不能够获取到.提示自动化错误. 3.定 ...

  5. 关于git stash的应用总结

    Step1 新增 git stash save -a "message" // 对于在项目里加入了代码新文件的开发来说,-a选项才会将新加入的代码文件同时放入暂存区 类似于 git ...

  6. 推荐一个Chrome扩展应用,能够自动去除CSDN广告

    作为一个程序员,每天编程遇到问题时,少不了前往国内著名的CSDN网站上查信息,看是否有同行遇到类似问题.很多时候根据遇到问题的错误消息进行搜索,结果都是一篇篇CSDN博客.这些博客打开后都会显示很多广 ...

  7. libevent evbuffer参考文章

    https://blog.csdn.net/FreeeLinux/article/details/52799951 http://cache.baiducontent.com/c?m=9d78d513 ...

  8. 小草的Trouble学生信息管理系统

    小草最近上课学C++,在图书馆纠结了好久,决定做这个小东西,没想到遇到了好多困难,好吧,功夫不负有心人,小草也在敲代码中提高了不少. 小草硬是学了好几天,才搞完这个东西,也算是了结了小草的一个心结. ...

  9. 【[SCOI2015]情报传递】

    非常无脑的板子题,就当是练一下板子 我们可以先将所有的操作离线下来,之后那些搜集过情报的点就有了点权,对于查询操作,就是查询一下这条路径上有几个点点权满足\(st<=now-C+1\) #inc ...

  10. 给自己的网站加上robots.txt

    今天给自己的网站加了一个robots.txt,在网上收集整理了一些资料,给自己网站也加上了robots.txt ! 顺便给大家分享一下! 一.robots.txt是什么? robots.txt是一个纯 ...