打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
redis实现高并发投票网站
userphoto

2022.10.03 江苏

关注

注:原始资料来自于享学课堂,加上自己的理解和修改

目录

1、基本逻辑分析

1.1 文章投票功能模块需求

1.2 数据库设计

​1.3 使用到的数据类型和相关指令

1.4 key设计思路

1.5、记录已投票用户,防重投

1.6 记录文章分数

1.7、Redis缓存设计实战

1.8、缓存数据变化

2、代码实现

2.1文章处理相关接口

2.2 具体接口实现

2.3 redis的工具类


1、基本逻辑分析

1.1 文章投票功能模块需求

  • 用户可以发表文章,发表时默认给自己的文章投了一票
  • 用户在查看网站时可以按评分进行排列查看 (想到排序就是zset)
  • 用户也可以按照文章发布时间进行排序 (想到排序就是zset)
  • 为节约内存,一篇文章发表后,7天内可以投票,7天过后就不能再投票了 (这里就是加上过期时间)
  • 为防止同一用户多次投票,用户只能给一篇文章投一次票(重复就想到set)

1.2 数据库设计

文章基本信息表 article

 

文章票数与分值表 vote_data

文章投票详表 vote_details


1.3 使用到的数据类型和相关指令

指令如下:

  • HASH:hset  hincrBy  hgetAll   expire
  • SET:sadd  smembers
  • ZSET:zadd   zscore  zincrby  zrevrange

1.4 key设计思路

key的设计一般使用数据库名+主键,这样就能够保证key的唯一性

比如:set user:1 123

1.5、记录已投票用户,防重投

  • set:无序不重复
  • zset:有序不重复

防止重复首先就想到set,zset类型数据,由于没有排行等需求,使用更加简单的set就能满足需求

1.6 记录文章分数

由于涉及到分数的排行和文章发布时间的排行,首先想到的就是使用zset保存数据(默认分数从小到大升序排序)

1.7、Redis缓存设计实战

1.8、缓存数据变化

2、代码实现

2.1文章处理相关接口

  1. /**
  2. * RedisArticleService
  3. *
  4. * @author: honry.guan
  5. * @date: 2021/4/12 17:36
  6. */
  7. public interface RedisArticleService {
  8. /**
  9. * 文章发布,这里只是做个演示,一般存在数据库中
  10. *
  11. * @param title 标题
  12. * @param content 内容
  13. * @param link 链接
  14. * @param userId 用户ID
  15. * @return 文章的ID
  16. */
  17. String postArticle(String title, String content, String link, String userId);
  18. /**
  19. * 获取key所有数据
  20. *
  21. * @param key redis的key
  22. * @return 数据集
  23. */
  24. Map<String, String> hgetAll(String key);
  25. /**
  26. * 文章投票
  27. *
  28. * @param userId 用户id
  29. * @param articleId 文章id
  30. */
  31. void articleVote(String userId, String articleId);
  32. /**
  33. * 获取文章某个字段的值
  34. *
  35. * @param key redis的key
  36. * @param votes 字段field
  37. * @return 返回值
  38. */
  39. String hget(String key, String votes);
  40. /**
  41. * 文章列表查询(分页)
  42. *
  43. * @param page key
  44. * @return redis查询结果
  45. */
  46. List<Map<String, String>> getArticles(int page, String order);
  47. }

2.2 具体接口实现

代码key说明:

文章1的投票人,不能重复投,使用set:jedis.sadd("voted:1" , userId);

文章对象信息,使用hash保存:jedis.hmset(article, articleData);

分数排行,看着排行,就想到zset:jedis.zadd("score:info", now + Constants.VOTE_SCORE, article);

时间排行,看着排行,就想到zset:jedis.zadd("time:", now, article);

  1. @Service
  2. public class RedisArticleServiceImpl implements RedisArticleService {
  3. @Resource
  4. private JedisUtils jedis;
  5. /**
  6. * 文章提交发布
  7. *
  8. * @param title 标题
  9. * @param content 内容
  10. * @param link 链接
  11. * @param userId 用户ID
  12. * @return 文章的ID
  13. */
  14. @Override
  15. public String postArticle(String title, String content, String link, String userId) {
  16. //article:001// articleId=1
  17. String articleId = String.valueOf(jedis.incr("article:"));
  18. //投票键: voted:
  19. String voted = "voted:" + articleId;
  20. //自己先给自己投一票
  21. jedis.sadd(voted, userId);
  22. jedis.expire(voted, Constants.ONE_WEEK_IN_SECONDS);
  23. long now = System.currentTimeMillis() / 1000;
  24. String article = "article:" + articleId;
  25. HashMap<String, String> articleData = new HashMap<String, String>(5);
  26. articleData.put("title", title);
  27. articleData.put("link", link);
  28. articleData.put("user", userId);
  29. articleData.put("now", String.valueOf(now));
  30. articleData.put("votes", "1");
  31. //添加一篇文章
  32. jedis.hmset(article, articleData);
  33. //增加分数排行,随机初始化了一个分数now + Constants.VOTE_SCORE
  34. jedis.zadd("score:info", now + Constants.VOTE_SCORE, article);
  35. //增加时间排行
  36. jedis.zadd("time:", now, article);
  37. return articleId;
  38. }
  39. /**
  40. * 文章投票
  41. *
  42. * @param userId 用户ID
  43. * @param article 文章ID(article:001)
  44. */
  45. @Override
  46. public void articleVote(String userId, String article) {
  47. //计算投票截止时间
  48. long cutoff = (System.currentTimeMillis() / 1000) - Constants.ONE_WEEK_IN_SECONDS;
  49. //检查是否还可以对文章进行投票,如果该文章的发布时间比截止时间小,则已过期,不能进行投票
  50. if (jedis.zscore("time:", article) < cutoff) {
  51. return;
  52. }
  53. //获取文章主键idarticle:1 1
  54. String articleId = article.substring(article.indexOf(':') + 1);
  55. //添加投票人,投票人添加成功,才增加分数和投票数
  56. if (jedis.sadd("voted:" + articleId, userId) == 1) {
  57. //分值加400
  58. jedis.zincrby("score:info", Constants.VOTE_SCORE, article);
  59. //投票数加1
  60. jedis.hincrBy(article, "votes", 1L);
  61. }
  62. }
  63. /**
  64. * 文章列表查询(分页)
  65. *
  66. * @param page key
  67. * @return redis查询结果
  68. */
  69. @Override
  70. public List<Map<String, String>> getArticles(int page, String key) {
  71. int start = (page - 1) * Constants.ARTICLES_PER_PAGE;
  72. int end = start + Constants.ARTICLES_PER_PAGE - 1;
  73. //倒序查询出投票数最高的文章,zset有序集合,分值递减
  74. Set<String> ids = jedis.zrevrange(key, start, end);
  75. List<Map<String, String>> articles = new ArrayList<>();
  76. for (String id : ids) {
  77. Map<String, String> articleData = jedis.hgetAll(id);
  78. articleData.put("id", id);
  79. articles.add(articleData);
  80. }
  81. return articles;
  82. }
  83. @Override
  84. public String hget(String key, String feild) {
  85. return jedis.hget(key, feild);
  86. }
  87. @Override
  88. public Map<String, String> hgetAll(String key) {
  89. return jedis.hgetAll(key);
  90. }
  91. }

2.3 redis的工具类

  1. package com.james.cache.utils;
  2. import java.util.List;
  3. import java.util.Map;
  4. import java.util.Set;
  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.Pipeline;
  10. import redis.clients.jedis.BinaryClient.LIST_POSITION;
  11. @Component
  12. public class JedisUtils {
  13. private JedisPool pool = null;
  14. private String ip = "192.168.42.111";
  15. private int port = 6379;
  16. private String auth = "12345678";
  17. /**
  18. * 传入ip和端口号构建redis 连接
  19. *
  20. * @param ip
  21. * ip
  22. * @param prot
  23. * 端口
  24. */
  25. public JedisUtils() {
  26. if (pool == null) {
  27. JedisPoolConfig config = new JedisPoolConfig();
  28. config.setMaxTotal(500);
  29. config.setMaxIdle(5);
  30. config.setMaxWaitMillis(100);
  31. config.setTestOnBorrow(true);
  32. pool = new JedisPool(config, this.ip, this.port, 100000, this.auth);
  33. }
  34. }
  35. /**
  36. * 通过key获取储存在redis中的value 并释放连
  37. *
  38. * @param key
  39. * @return 成功返回value 失败返回null
  40. */
  41. public String get(String key) {
  42. Jedis jedis = null;
  43. String value = null;
  44. try {
  45. jedis = pool.getResource();
  46. value = jedis.get(key);
  47. } catch (Exception e) {
  48. pool.returnBrokenResource(jedis);
  49. e.printStackTrace();
  50. } finally {
  51. returnResource(pool, jedis);
  52. }
  53. return value;
  54. }
  55. /**
  56. * 向redis存入key和value,并释放连接资 如果key已经存在 则覆
  57. *
  58. * @param key
  59. * @param value
  60. * @return 成功 返回OK 失败返回 0
  61. */
  62. public String set(String key, String value) {
  63. Jedis jedis = null;
  64. try {
  65. jedis = pool.getResource();//每次操作时向pool借用个jedis对象,用完即还?
  66. return jedis.set(key, value);
  67. } catch (Exception e) {
  68. pool.returnBrokenResource(jedis);
  69. e.printStackTrace();
  70. return "0";
  71. } finally {
  72. returnResource(pool, jedis);
  73. }
  74. }
  75. /**
  76. * 向redis存入序列化的key和value,并释放连接资 如果key已经存在 则覆
  77. *
  78. * @param key
  79. * @param value
  80. * @return 成功 返回OK 失败返回 0
  81. */
  82. public String setSerializer(byte[] keyBytes, byte[] valueBytes) {
  83. Jedis jedis = null;
  84. try {
  85. jedis = pool.getResource();
  86. return jedis.set(keyBytes, valueBytes);
  87. } catch (Exception e) {
  88. pool.returnBrokenResource(jedis);
  89. e.printStackTrace();
  90. return "0";
  91. } finally {
  92. returnResource(pool, jedis);
  93. }
  94. }
  95. /**
  96. * 通过序列化key获取储存在redis中的序列化value 并释放连
  97. *
  98. * @param key
  99. * @return 成功返回value 失败返回null
  100. */
  101. public byte[] getSerializer(byte[] key) {
  102. Jedis jedis = null;
  103. byte[] value = null;
  104. try {
  105. jedis = pool.getResource();
  106. value = jedis.get(key);
  107. } catch (Exception e) {
  108. pool.returnBrokenResource(jedis);
  109. e.printStackTrace();
  110. } finally {
  111. returnResource(pool, jedis);
  112. }
  113. return value;
  114. }
  115. /**
  116. * 删除指定的key,也可以传入一个包含key的数
  117. *
  118. * @param keys
  119. * 个key 也可以使 string 数组
  120. * @return 返回删除成功的个
  121. */
  122. public Long del(String... keys) {
  123. Jedis jedis = null;
  124. try {
  125. jedis = pool.getResource();
  126. return jedis.del(keys);
  127. } catch (Exception e) {
  128. pool.returnBrokenResource(jedis);
  129. e.printStackTrace();
  130. return 0L;
  131. } finally {
  132. returnResource(pool, jedis);
  133. }
  134. }
  135. /**
  136. * 通过key向指定的value值追加?
  137. *
  138. * @param key
  139. * @param str
  140. * @return 成功返回 添加后value的长 失败 返回 添加 value 的长 异常返回0L
  141. */
  142. public Long append(String key, String str) {
  143. Jedis jedis = null;
  144. Long res = null;
  145. try {
  146. jedis = pool.getResource();
  147. res = jedis.append(key, str);
  148. } catch (Exception e) {
  149. pool.returnBrokenResource(jedis);
  150. e.printStackTrace();
  151. return 0L;
  152. } finally {
  153. returnResource(pool, jedis);
  154. }
  155. return res;
  156. }
  157. /**
  158. * 判断key是否存在
  159. *
  160. * @param key
  161. * @return true OR false
  162. */
  163. public Boolean exists(String key) {
  164. Jedis jedis = null;
  165. try {
  166. jedis = pool.getResource();
  167. return jedis.exists(key);
  168. } catch (Exception e) {
  169. pool.returnBrokenResource(jedis);
  170. e.printStackTrace();
  171. return false;
  172. } finally {
  173. returnResource(pool, jedis);
  174. }
  175. }
  176. /**
  177. * 设置key value,如果key已经存在则返0,nx==> not exist
  178. *
  179. * @param key
  180. * @param value
  181. * @return 成功返回1 如果存在 发生异常 返回 0
  182. */
  183. public Long setnx(String key, String value) {
  184. Jedis jedis = null;
  185. try {
  186. jedis = pool.getResource();
  187. return jedis.setnx(key, value);
  188. } catch (Exception e) {
  189. pool.returnBrokenResource(jedis);
  190. e.printStackTrace();
  191. return 0L;
  192. } finally {
  193. returnResource(pool, jedis);
  194. }
  195. }
  196. /**
  197. * 设置key value并制定这个键值的有效
  198. *
  199. * @param key
  200. * @param value
  201. * @param seconds
  202. * 单位:
  203. * @return 成功返回OK 失败和异常返回null
  204. */
  205. public String setex(String key, String value, int seconds) {
  206. Jedis jedis = null;
  207. String res = null;
  208. try {
  209. jedis = pool.getResource();
  210. res = jedis.setex(key, seconds, value);
  211. } catch (Exception e) {
  212. pool.returnBrokenResource(jedis);
  213. e.printStackTrace();
  214. } finally {
  215. returnResource(pool, jedis);
  216. }
  217. return res;
  218. }
  219. /**
  220. * 通过key 和offset 从指定的位置始将原先value替换 下标0,offset表示从offset下标始替
  221. * 如果替换的字符串长度过小则会这样 example: value : bigsea@zto.cn str : abc 从下7始替 则结果为
  222. * RES : bigsea.abc.cn
  223. *
  224. * @param key
  225. * @param str
  226. * @param offset
  227. * 下标位置
  228. * @return 返回替换 value 的长
  229. */
  230. public Long setrange(String key, String str, int offset) {
  231. Jedis jedis = null;
  232. try {
  233. jedis = pool.getResource();
  234. return jedis.setrange(key, offset, str);
  235. } catch (Exception e) {
  236. pool.returnBrokenResource(jedis);
  237. e.printStackTrace();
  238. return 0L;
  239. } finally {
  240. returnResource(pool, jedis);
  241. }
  242. }
  243. /**
  244. * 通过批量的key获取批量的value
  245. *
  246. * @param keys
  247. * string数组 也可以是个key
  248. * @return 成功返回value的集, 失败返回null的集 ,异常返回
  249. */
  250. public List<String> mget(String... keys) {
  251. Jedis jedis = null;
  252. List<String> values = null;
  253. try {
  254. jedis = pool.getResource();
  255. values = jedis.mget(keys);
  256. } catch (Exception e) {
  257. pool.returnBrokenResource(jedis);
  258. e.printStackTrace();
  259. } finally {
  260. returnResource(pool, jedis);
  261. }
  262. return values;
  263. }
  264. /**
  265. * 批量的设置key:value,可以 example: obj.mset(new
  266. * String[]{"key2","value1","key2","value2"})
  267. *
  268. * @param keysvalues
  269. * @return 成功返回OK 失败 异常 返回 null
  270. *
  271. */
  272. public String mset(String... keysvalues) {
  273. Jedis jedis = null;
  274. String res = null;
  275. try {
  276. jedis = pool.getResource();
  277. res = jedis.mset(keysvalues);
  278. } catch (Exception e) {
  279. pool.returnBrokenResource(jedis);
  280. e.printStackTrace();
  281. } finally {
  282. returnResource(pool, jedis);
  283. }
  284. return res;
  285. }
  286. /**
  287. * 删除多个字符串key 并释放连
  288. *
  289. * @param key*
  290. * @return 成功返回value 失败返回null
  291. */
  292. public boolean mdel(List<String> keys) {
  293. Jedis jedis = null;
  294. boolean flag = false;
  295. try {
  296. jedis = pool.getResource();//从连接�用Jedis对象
  297. Pipeline pipe = jedis.pipelined();//获取jedis对象的pipeline对象
  298. for(String key:keys){
  299. pipe.del(key); //将多个key放入pipe删除指令
  300. }
  301. pipe.sync(); //执行命令,完全此时pipeline对象的远程调
  302. flag = true;
  303. } catch (Exception e) {
  304. pool.returnBrokenResource(jedis);
  305. e.printStackTrace();
  306. } finally {
  307. returnResource(pool, jedis);
  308. }
  309. return flag;
  310. }
  311. /**
  312. * 批量的设置key:value,可以,如果key已经存在则会失败,操作会回 example: obj.msetnx(new
  313. * String[]{"key2","value1","key2","value2"})
  314. *
  315. * @param keysvalues
  316. * @return 成功返回1 失败返回0
  317. */
  318. public Long msetnx(String... keysvalues) {
  319. Jedis jedis = null;
  320. Long res = 0L;
  321. try {
  322. jedis = pool.getResource();
  323. res = jedis.msetnx(keysvalues);
  324. } catch (Exception e) {
  325. pool.returnBrokenResource(jedis);
  326. e.printStackTrace();
  327. } finally {
  328. returnResource(pool, jedis);
  329. }
  330. return res;
  331. }
  332. /**
  333. * 设置key的?,并返回一个旧
  334. *
  335. * @param key
  336. * @param value
  337. * @return 旧? 如果key不存 则返回null
  338. */
  339. public String getset(String key, String value) {
  340. Jedis jedis = null;
  341. String res = null;
  342. try {
  343. jedis = pool.getResource();
  344. res = jedis.getSet(key, value);
  345. } catch (Exception e) {
  346. pool.returnBrokenResource(jedis);
  347. e.printStackTrace();
  348. } finally {
  349. returnResource(pool, jedis);
  350. }
  351. return res;
  352. }
  353. /**
  354. * 通过下标 和key 获取指定下标位置 value
  355. *
  356. * @param key
  357. * @param startOffset
  358. * 始位 0 负数表示从右边开始截
  359. * @param endOffset
  360. * @return 如果没有返回null
  361. */
  362. public String getrange(String key, int startOffset, int endOffset) {
  363. Jedis jedis = null;
  364. String res = null;
  365. try {
  366. jedis = pool.getResource();
  367. res = jedis.getrange(key, startOffset, endOffset);
  368. } catch (Exception e) {
  369. pool.returnBrokenResource(jedis);
  370. e.printStackTrace();
  371. } finally {
  372. returnResource(pool, jedis);
  373. }
  374. return res;
  375. }
  376. /**
  377. * 通过key 对value进行加?+1操作,当value不是int类型时会返回错误,当key不存在是则value1
  378. *
  379. * @param key
  380. * @return 加�后的结
  381. */
  382. public Long incr(String key) {
  383. Jedis jedis = null;
  384. Long res = null;
  385. try {
  386. jedis = pool.getResource();
  387. res = jedis.incr(key);
  388. } catch (Exception e) {
  389. pool.returnBrokenResource(jedis);
  390. e.printStackTrace();
  391. } finally {
  392. returnResource(pool, jedis);
  393. }
  394. return res;
  395. }
  396. /**
  397. * 通过key给指定的value加?,如果key不存,则这是value为该
  398. *
  399. * @param key
  400. * @param integer
  401. * @return
  402. */
  403. public Long incrBy(String key, Long integer) {
  404. Jedis jedis = null;
  405. Long res = null;
  406. try {
  407. jedis = pool.getResource();
  408. res = jedis.incrBy(key, integer);
  409. } catch (Exception e) {
  410. pool.returnBrokenResource(jedis);
  411. e.printStackTrace();
  412. } finally {
  413. returnResource(pool, jedis);
  414. }
  415. return res;
  416. }
  417. /**
  418. * 对key的�做减减操作,如果key不存,则设置key-1
  419. *
  420. * @param key
  421. * @return
  422. */
  423. public Long decr(String key) {
  424. Jedis jedis = null;
  425. Long res = null;
  426. try {
  427. jedis = pool.getResource();
  428. res = jedis.decr(key);
  429. } catch (Exception e) {
  430. pool.returnBrokenResource(jedis);
  431. e.printStackTrace();
  432. } finally {
  433. returnResource(pool, jedis);
  434. }
  435. return res;
  436. }
  437. /**
  438. * 减去指定的?
  439. *
  440. * @param key
  441. * @param integer
  442. * @return
  443. */
  444. public Long decrBy(String key, Long integer) {
  445. Jedis jedis = null;
  446. Long res = null;
  447. try {
  448. jedis = pool.getResource();
  449. res = jedis.decrBy(key, integer);
  450. } catch (Exception e) {
  451. pool.returnBrokenResource(jedis);
  452. e.printStackTrace();
  453. } finally {
  454. returnResource(pool, jedis);
  455. }
  456. return res;
  457. }
  458. /**
  459. * 通过key获取value值的长度
  460. *
  461. * @param key
  462. * @return 失败返回null
  463. */
  464. public Long serlen(String key) {
  465. Jedis jedis = null;
  466. Long res = null;
  467. try {
  468. jedis = pool.getResource();
  469. res = jedis.strlen(key);
  470. } catch (Exception e) {
  471. pool.returnBrokenResource(jedis);
  472. e.printStackTrace();
  473. } finally {
  474. returnResource(pool, jedis);
  475. }
  476. return res;
  477. }
  478. /**
  479. * 通过key给field设置指定的?,如果key不存,则先创建
  480. *
  481. * @param key
  482. * @param field
  483. * 字段
  484. * @param value
  485. * @return 如果存在返回0 异常返回null
  486. */
  487. public Long hset(String key, String field, String value) {
  488. Jedis jedis = null;
  489. Long res = null;
  490. try {
  491. jedis = pool.getResource();
  492. res = jedis.hset(key, field, value);
  493. } catch (Exception e) {
  494. pool.returnBrokenResource(jedis);
  495. e.printStackTrace();
  496. } finally {
  497. returnResource(pool, jedis);
  498. }
  499. return res;
  500. }
  501. /**
  502. * 通过key给field设置指定的?,如果key不存在则先创,如果field已经存在,返回0
  503. *
  504. * @param key
  505. * @param field
  506. * @param value
  507. * @return
  508. */
  509. public Long hsetnx(String key, String field, String value) {
  510. Jedis jedis = null;
  511. Long res = null;
  512. try {
  513. jedis = pool.getResource();
  514. res = jedis.hsetnx(key, field, value);
  515. } catch (Exception e) {
  516. pool.returnBrokenResource(jedis);
  517. e.printStackTrace();
  518. } finally {
  519. returnResource(pool, jedis);
  520. }
  521. return res;
  522. }
  523. /**
  524. * 通过key同时设置 hash的多个field
  525. *
  526. * @param key
  527. * @param hash
  528. * @return 返回OK 异常返回null
  529. */
  530. public String hmset(String key, Map<String, String> hash) {
  531. Jedis jedis = null;
  532. String res = null;
  533. try {
  534. jedis = pool.getResource();
  535. res = jedis.hmset(key, hash);
  536. } catch (Exception e) {
  537. pool.returnBrokenResource(jedis);
  538. e.printStackTrace();
  539. } finally {
  540. returnResource(pool, jedis);
  541. }
  542. return res;
  543. }
  544. /**
  545. * 通过key field 获取指定 value
  546. *
  547. * @param key
  548. * @param field
  549. * @return 没有返回null
  550. */
  551. public String hget(String key, String field) {
  552. Jedis jedis = null;
  553. String res = null;
  554. try {
  555. jedis = pool.getResource();
  556. res = jedis.hget(key, field);
  557. } catch (Exception e) {
  558. pool.returnBrokenResource(jedis);
  559. e.printStackTrace();
  560. } finally {
  561. returnResource(pool, jedis);
  562. }
  563. return res;
  564. }
  565. /**
  566. * 通过key fields 获取指定的value 如果没有对应的value则返回null
  567. *
  568. * @param key
  569. * @param fields
  570. * 可以 个String 也可以是 String数组
  571. * @return
  572. */
  573. public List<String> hmget(String key, String... fields) {
  574. Jedis jedis = null;
  575. List<String> res = null;
  576. try {
  577. jedis = pool.getResource();
  578. res = jedis.hmget(key, fields);
  579. } catch (Exception e) {
  580. pool.returnBrokenResource(jedis);
  581. e.printStackTrace();
  582. } finally {
  583. returnResource(pool, jedis);
  584. }
  585. return res;
  586. }
  587. /**
  588. * 通过key给指定的field的value加上给定的?
  589. *
  590. * @param key
  591. * @param field
  592. * @param value
  593. * @return
  594. */
  595. public Long hincrby(String key, String field, Long value) {
  596. Jedis jedis = null;
  597. Long res = null;
  598. try {
  599. jedis = pool.getResource();
  600. res = jedis.hincrBy(key, field, value);
  601. } catch (Exception e) {
  602. pool.returnBrokenResource(jedis);
  603. e.printStackTrace();
  604. } finally {
  605. returnResource(pool, jedis);
  606. }
  607. return res;
  608. }
  609. /**
  610. * 通过key和field判断是否有指定的value存在
  611. *
  612. * @param key
  613. * @param field
  614. * @return
  615. */
  616. public Boolean hexists(String key, String field) {
  617. Jedis jedis = null;
  618. Boolean res = false;
  619. try {
  620. jedis = pool.getResource();
  621. res = jedis.hexists(key, field);
  622. } catch (Exception e) {
  623. pool.returnBrokenResource(jedis);
  624. e.printStackTrace();
  625. } finally {
  626. returnResource(pool, jedis);
  627. }
  628. return res;
  629. }
  630. /**
  631. * 通过key返回field的数
  632. *
  633. * @param key
  634. * @return
  635. */
  636. public Long hlen(String key) {
  637. Jedis jedis = null;
  638. Long res = null;
  639. try {
  640. jedis = pool.getResource();
  641. res = jedis.hlen(key);
  642. } catch (Exception e) {
  643. pool.returnBrokenResource(jedis);
  644. e.printStackTrace();
  645. } finally {
  646. returnResource(pool, jedis);
  647. }
  648. return res;
  649. }
  650. public Map<String,String> hgetAll(String key) {
  651. Jedis jedis = null;
  652. Map<String,String> res = null;
  653. try {
  654. jedis = pool.getResource();
  655. res = jedis.hgetAll(key);
  656. } catch (Exception e) {
  657. pool.returnBrokenResource(jedis);
  658. e.printStackTrace();
  659. } finally {
  660. returnResource(pool, jedis);
  661. }
  662. return res;
  663. }
  664. /**
  665. * 通过key 删除指定 field
  666. *
  667. * @param key
  668. * @param fields
  669. * 可以 field 也可以是 个数
  670. * @return
  671. */
  672. public Long hdel(String key, String... fields) {
  673. Jedis jedis = null;
  674. Long res = null;
  675. try {
  676. jedis = pool.getResource();
  677. res = jedis.hdel(key, fields);
  678. } catch (Exception e) {
  679. pool.returnBrokenResource(jedis);
  680. e.printStackTrace();
  681. } finally {
  682. returnResource(pool, jedis);
  683. }
  684. return res;
  685. }
  686. public Set<String> zrange(String key, Long start, Long end) {
  687. Jedis jedis = null;
  688. Set<String> res = null;
  689. try {
  690. jedis = pool.getResource();
  691. res = jedis.zrange(key, start, end);
  692. } catch (Exception e) {
  693. pool.returnBrokenResource(jedis);
  694. e.printStackTrace();
  695. } finally {
  696. returnResource(pool, jedis);
  697. }
  698. return res;
  699. }
  700. /**
  701. * 通过key返回有的field
  702. *
  703. * @param key
  704. * @return
  705. */
  706. public Set<String> hkeys(String key) {
  707. Jedis jedis = null;
  708. Set<String> res = null;
  709. try {
  710. jedis = pool.getResource();
  711. res = jedis.hkeys(key);
  712. } catch (Exception e) {
  713. pool.returnBrokenResource(jedis);
  714. e.printStackTrace();
  715. } finally {
  716. returnResource(pool, jedis);
  717. }
  718. return res;
  719. }
  720. /**
  721. * 通过key返回有和key有关的value
  722. *
  723. * @param key
  724. * @return
  725. */
  726. public List<String> hvals(String key) {
  727. Jedis jedis = null;
  728. List<String> res = null;
  729. try {
  730. jedis = pool.getResource();
  731. res = jedis.hvals(key);
  732. } catch (Exception e) {
  733. pool.returnBrokenResource(jedis);
  734. e.printStackTrace();
  735. } finally {
  736. returnResource(pool, jedis);
  737. }
  738. return res;
  739. }
  740. /**
  741. * 通过key获取有的field和value
  742. *
  743. * @param key
  744. * @return
  745. */
  746. public Map<String, String> hgetall(String key) {
  747. Jedis jedis = null;
  748. Map<String, String> res = null;
  749. try {
  750. jedis = pool.getResource();
  751. res = jedis.hgetAll(key);
  752. } catch (Exception e) {
  753. pool.returnBrokenResource(jedis);
  754. e.printStackTrace();
  755. } finally {
  756. returnResource(pool, jedis);
  757. }
  758. return res;
  759. }
  760. /**
  761. * <p>
  762. * 通过key向list头部添加字符
  763. * </p>
  764. *
  765. * @param key
  766. * @param strs
  767. * 可以使一个string 也可以使string数组
  768. * @return 返回list的value个数
  769. */
  770. public Long lpush(String key, String... strs) {
  771. Jedis jedis = null;
  772. Long res = null;
  773. try {
  774. jedis = pool.getResource();
  775. res = jedis.lpush(key, strs);
  776. } catch (Exception e) {
  777. pool.returnBrokenResource(jedis);
  778. e.printStackTrace();
  779. } finally {
  780. returnResource(pool, jedis);
  781. }
  782. return res;
  783. }
  784. /**
  785. * <p>
  786. * 通过key向list尾部添加字符
  787. * </p>
  788. *
  789. * @param key
  790. * @param strs
  791. * 可以使一个string 也可以使string数组
  792. * @return 返回list的value个数
  793. */
  794. public Long rpush(String key, String... strs) {
  795. Jedis jedis = null;
  796. Long res = null;
  797. try {
  798. jedis = pool.getResource();
  799. res = jedis.rpush(key, strs);
  800. } catch (Exception e) {
  801. pool.returnBrokenResource(jedis);
  802. e.printStackTrace();
  803. } finally {
  804. returnResource(pool, jedis);
  805. }
  806. return res;
  807. }
  808. /**
  809. * <p>
  810. * 通过key在list指定的位置之前或者之 添加字符串元
  811. * </p>
  812. *
  813. * @param key
  814. * @param where
  815. * LIST_POSITION枚举类型
  816. * @param pivot
  817. * list里面的value
  818. * @param value
  819. * 添加的value
  820. * @return
  821. */
  822. public Long linsert(String key, LIST_POSITION where, String pivot, String value) {
  823. Jedis jedis = null;
  824. Long res = null;
  825. try {
  826. jedis = pool.getResource();
  827. res = jedis.linsert(key, where, pivot, value);
  828. } catch (Exception e) {
  829. pool.returnBrokenResource(jedis);
  830. e.printStackTrace();
  831. } finally {
  832. returnResource(pool, jedis);
  833. }
  834. return res;
  835. }
  836. /**
  837. * <p>
  838. * 通过key设置list指定下标位置的value
  839. * </p>
  840. * <p>
  841. * 如果下标超过list里面value的个数则报错
  842. * </p>
  843. *
  844. * @param key
  845. * @param index
  846. * 0
  847. * @param value
  848. * @return 成功返回OK
  849. */
  850. public String lset(String key, Long index, String value) {
  851. Jedis jedis = null;
  852. String res = null;
  853. try {
  854. jedis = pool.getResource();
  855. res = jedis.lset(key, index, value);
  856. } catch (Exception e) {
  857. pool.returnBrokenResource(jedis);
  858. e.printStackTrace();
  859. } finally {
  860. returnResource(pool, jedis);
  861. }
  862. return res;
  863. }
  864. /**
  865. * <p>
  866. * 通过key从对应的list中删除指定的count value相同的元
  867. * </p>
  868. *
  869. * @param key
  870. * @param count
  871. * 当count0时删除全
  872. * @param value
  873. * @return 返回被删除的个数
  874. */
  875. public Long lrem(String key, long count, String value) {
  876. Jedis jedis = null;
  877. Long res = null;
  878. try {
  879. jedis = pool.getResource();
  880. res = jedis.lrem(key, count, value);
  881. } catch (Exception e) {
  882. pool.returnBrokenResource(jedis);
  883. e.printStackTrace();
  884. } finally {
  885. returnResource(pool, jedis);
  886. }
  887. return res;
  888. }
  889. /**
  890. * <p>
  891. * 通过key保留list中从strat下标始到end下标结束的value
  892. * </p>
  893. *
  894. * @param key
  895. * @param start
  896. * @param end
  897. * @return 成功返回OK
  898. */
  899. public String ltrim(String key, long start, long end) {
  900. Jedis jedis = null;
  901. String res = null;
  902. try {
  903. jedis = pool.getResource();
  904. res = jedis.ltrim(key, start, end);
  905. } catch (Exception e) {
  906. pool.returnBrokenResource(jedis);
  907. e.printStackTrace();
  908. } finally {
  909. returnResource(pool, jedis);
  910. }
  911. return res;
  912. }
  913. /**
  914. * <p>
  915. * 通过key从list的头部删除一个value,并返回该value
  916. * </p>
  917. *
  918. * @param key
  919. * @return
  920. */
  921. public String lpop(String key) {
  922. Jedis jedis = null;
  923. String res = null;
  924. try {
  925. jedis = pool.getResource();
  926. res = jedis.lpop(key);
  927. } catch (Exception e) {
  928. pool.returnBrokenResource(jedis);
  929. e.printStackTrace();
  930. } finally {
  931. returnResource(pool, jedis);
  932. }
  933. return res;
  934. }
  935. /**
  936. * <p>
  937. * 通过key从list尾部删除个value,并返回该元素
  938. * </p>
  939. *
  940. * @param key
  941. * @return
  942. */
  943. public String rpop(String key) {
  944. Jedis jedis = null;
  945. String res = null;
  946. try {
  947. jedis = pool.getResource();
  948. res = jedis.rpop(key);
  949. } catch (Exception e) {
  950. pool.returnBrokenResource(jedis);
  951. e.printStackTrace();
  952. } finally {
  953. returnResource(pool, jedis);
  954. }
  955. return res;
  956. }
  957. /**
  958. * <p>
  959. * 通过key从一个list的尾部删除一个value并添加到另一个list的头,并返回该value
  960. * </p>
  961. * <p>
  962. * 如果第一个list为空或�不存在则返回null
  963. * </p>
  964. *
  965. * @param srckey
  966. * @param dstkey
  967. * @return
  968. */
  969. public String rpoplpush(String srckey, String dstkey) {
  970. Jedis jedis = null;
  971. String res = null;
  972. try {
  973. jedis = pool.getResource();
  974. res = jedis.rpoplpush(srckey, dstkey);
  975. } catch (Exception e) {
  976. pool.returnBrokenResource(jedis);
  977. e.printStackTrace();
  978. } finally {
  979. returnResource(pool, jedis);
  980. }
  981. return res;
  982. }
  983. /**
  984. * <p>
  985. * 通过key获取list中指定下标位置的value
  986. * </p>
  987. *
  988. * @param key
  989. * @param index
  990. * @return 如果没有返回null
  991. */
  992. public String lindex(String key, long index) {
  993. Jedis jedis = null;
  994. String res = null;
  995. try {
  996. jedis = pool.getResource();
  997. res = jedis.lindex(key, index);
  998. } catch (Exception e) {
  999. pool.returnBrokenResource(jedis);
  1000. e.printStackTrace();
  1001. } finally {
  1002. returnResource(pool, jedis);
  1003. }
  1004. return res;
  1005. }
  1006. /**
  1007. * <p>
  1008. * 通过key返回list的长
  1009. * </p>
  1010. *
  1011. * @param key
  1012. * @return
  1013. */
  1014. public Long llen(String key) {
  1015. Jedis jedis = null;
  1016. Long res = null;
  1017. try {
  1018. jedis = pool.getResource();
  1019. res = jedis.llen(key);
  1020. } catch (Exception e) {
  1021. pool.returnBrokenResource(jedis);
  1022. e.printStackTrace();
  1023. } finally {
  1024. returnResource(pool, jedis);
  1025. }
  1026. return res;
  1027. }
  1028. /**
  1029. * <p>
  1030. * 通过key获取list指定下标位置的value
  1031. * </p>
  1032. * <p>
  1033. * 如果start 0 end -1 则返回全部的list中的value
  1034. * </p>
  1035. *
  1036. * @param key
  1037. * @param start
  1038. * @param end
  1039. * @return
  1040. */
  1041. public List<String> lrange(String key, long start, long end) {
  1042. Jedis jedis = null;
  1043. List<String> res = null;
  1044. try {
  1045. jedis = pool.getResource();
  1046. res = jedis.lrange(key, start, end);
  1047. } catch (Exception e) {
  1048. pool.returnBrokenResource(jedis);
  1049. e.printStackTrace();
  1050. } finally {
  1051. returnResource(pool, jedis);
  1052. }
  1053. return res;
  1054. }
  1055. /**
  1056. * <p>
  1057. * 通过key向指定的set中添加value
  1058. * </p>
  1059. *
  1060. * @param key
  1061. * @param members
  1062. * 可以是一个String 也可以是个String数组
  1063. * @return 添加成功的个
  1064. */
  1065. public Long sadd(String key, String... members) {
  1066. Jedis jedis = null;
  1067. Long res = null;
  1068. try {
  1069. jedis = pool.getResource();
  1070. res = jedis.sadd(key, members);
  1071. } catch (Exception e) {
  1072. pool.returnBrokenResource(jedis);
  1073. e.printStackTrace();
  1074. } finally {
  1075. returnResource(pool, jedis);
  1076. }
  1077. return res;
  1078. }
  1079. public void expire(String key, int times) {
  1080. Jedis jedis = null;
  1081. try {
  1082. jedis = pool.getResource();
  1083. jedis.expire(key, times);
  1084. } catch (Exception e) {
  1085. pool.returnBrokenResource(jedis);
  1086. e.printStackTrace();
  1087. } finally {
  1088. returnResource(pool, jedis);
  1089. }
  1090. }
  1091. /**
  1092. * <p>
  1093. * 通过key删除set中对应的value
  1094. * </p>
  1095. *
  1096. * @param key
  1097. * @param members
  1098. * 可以是一个String 也可以是个String数组
  1099. * @return 删除的个
  1100. */
  1101. public Long srem(String key, String... members) {
  1102. Jedis jedis = null;
  1103. Long res = null;
  1104. try {
  1105. jedis = pool.getResource();
  1106. res = jedis.srem(key, members);
  1107. } catch (Exception e) {
  1108. pool.returnBrokenResource(jedis);
  1109. e.printStackTrace();
  1110. } finally {
  1111. returnResource(pool, jedis);
  1112. }
  1113. return res;
  1114. }
  1115. /**
  1116. * <p>
  1117. * 通过key随机删除个set中的value并返回该
  1118. * </p>
  1119. *
  1120. * @param key
  1121. * @return
  1122. */
  1123. public String spop(String key) {
  1124. Jedis jedis = null;
  1125. String res = null;
  1126. try {
  1127. jedis = pool.getResource();
  1128. res = jedis.spop(key);
  1129. } catch (Exception e) {
  1130. pool.returnBrokenResource(jedis);
  1131. e.printStackTrace();
  1132. } finally {
  1133. returnResource(pool, jedis);
  1134. }
  1135. return res;
  1136. }
  1137. /**
  1138. * <p>
  1139. * 通过key获取set中的差集
  1140. * </p>
  1141. * <p>
  1142. * 以第个set为标
  1143. * </p>
  1144. *
  1145. * @param keys
  1146. * 可以使一个string 则返回set中所有的value 也可以是string数组
  1147. * @return
  1148. */
  1149. public Set<String> sdiff(String... keys) {
  1150. Jedis jedis = null;
  1151. Set<String> res = null;
  1152. try {
  1153. jedis = pool.getResource();
  1154. res = jedis.sdiff(keys);
  1155. } catch (Exception e) {
  1156. pool.returnBrokenResource(jedis);
  1157. e.printStackTrace();
  1158. } finally {
  1159. returnResource(pool, jedis);
  1160. }
  1161. return res;
  1162. }
  1163. /**
  1164. * <p>
  1165. * 通过key获取set中的差集并存入到另一个key
  1166. * </p>
  1167. * <p>
  1168. * 以第个set为标
  1169. * </p>
  1170. *
  1171. * @param dstkey
  1172. * 差集存入的key
  1173. * @param keys
  1174. * 可以使一个string 则返回set中所有的value 也可以是string数组
  1175. * @return
  1176. */
  1177. public Long sdiffstore(String dstkey, String... keys) {
  1178. Jedis jedis = null;
  1179. Long res = null;
  1180. try {
  1181. jedis = pool.getResource();
  1182. res = jedis.sdiffstore(dstkey, keys);
  1183. } catch (Exception e) {
  1184. pool.returnBrokenResource(jedis);
  1185. e.printStackTrace();
  1186. } finally {
  1187. returnResource(pool, jedis);
  1188. }
  1189. return res;
  1190. }
  1191. /**
  1192. * <p>
  1193. * 通过key获取指定set中的交集
  1194. * </p>
  1195. *
  1196. * @param keys
  1197. * 可以使一个string 也可以是个string数组
  1198. * @return
  1199. */
  1200. public Set<String> sinter(String... keys) {
  1201. Jedis jedis = null;
  1202. Set<String> res = null;
  1203. try {
  1204. jedis = pool.getResource();
  1205. res = jedis.sinter(keys);
  1206. } catch (Exception e) {
  1207. pool.returnBrokenResource(jedis);
  1208. e.printStackTrace();
  1209. } finally {
  1210. returnResource(pool, jedis);
  1211. }
  1212. return res;
  1213. }
  1214. /**
  1215. * <p>
  1216. * 通过key获取指定set中的交集 并将结果存入新的set
  1217. * </p>
  1218. *
  1219. * @param dstkey
  1220. * @param keys
  1221. * 可以使一个string 也可以是个string数组
  1222. * @return
  1223. */
  1224. public Long sinterstore(String dstkey, String... keys) {
  1225. Jedis jedis = null;
  1226. Long res = null;
  1227. try {
  1228. jedis = pool.getResource();
  1229. res = jedis.sinterstore(dstkey, keys);
  1230. } catch (Exception e) {
  1231. pool.returnBrokenResource(jedis);
  1232. e.printStackTrace();
  1233. } finally {
  1234. returnResource(pool, jedis);
  1235. }
  1236. return res;
  1237. }
  1238. /**
  1239. * <p>
  1240. * 通过key返回有set的并
  1241. * </p>
  1242. *
  1243. * @param keys
  1244. * 可以使一个string 也可以是个string数组
  1245. * @return
  1246. */
  1247. public Set<String> sunion(String... keys) {
  1248. Jedis jedis = null;
  1249. Set<String> res = null;
  1250. try {
  1251. jedis = pool.getResource();
  1252. res = jedis.sunion(keys);
  1253. } catch (Exception e) {
  1254. pool.returnBrokenResource(jedis);
  1255. e.printStackTrace();
  1256. } finally {
  1257. returnResource(pool, jedis);
  1258. }
  1259. return res;
  1260. }
  1261. /**
  1262. * <p>
  1263. * 通过key返回有set的并,并存入到新的set
  1264. * </p>
  1265. *
  1266. * @param dstkey
  1267. * @param keys
  1268. * 可以使一个string 也可以是个string数组
  1269. * @return
  1270. */
  1271. public Long sunionstore(String dstkey, String... keys) {
  1272. Jedis jedis = null;
  1273. Long res = null;
  1274. try {
  1275. jedis = pool.getResource();
  1276. res = jedis.sunionstore(dstkey, keys);
  1277. } catch (Exception e) {
  1278. pool.returnBrokenResource(jedis);
  1279. e.printStackTrace();
  1280. } finally {
  1281. returnResource(pool, jedis);
  1282. }
  1283. return res;
  1284. }
  1285. /**
  1286. * <p>
  1287. * 通过key将set中的value移除并添加到第二个set
  1288. * </p>
  1289. *
  1290. * @param srckey
  1291. * 要移除的
  1292. * @param dstkey
  1293. * 添加
  1294. * @param member
  1295. * set中的value
  1296. * @return
  1297. */
  1298. public Long smove(String srckey, String dstkey, String member) {
  1299. Jedis jedis = null;
  1300. Long res = null;
  1301. try {
  1302. jedis = pool.getResource();
  1303. res = jedis.smove(srckey, dstkey, member);
  1304. } catch (Exception e) {
  1305. pool.returnBrokenResource(jedis);
  1306. e.printStackTrace();
  1307. } finally {
  1308. returnResource(pool, jedis);
  1309. }
  1310. return res;
  1311. }
  1312. /**
  1313. * <p>
  1314. * 通过key获取set中value的个
  1315. * </p>
  1316. *
  1317. * @param key
  1318. * @return
  1319. */
  1320. public Long scard(String key) {
  1321. Jedis jedis = null;
  1322. Long res = null;
  1323. try {
  1324. jedis = pool.getResource();
  1325. res = jedis.scard(key);
  1326. } catch (Exception e) {
  1327. pool.returnBrokenResource(jedis);
  1328. e.printStackTrace();
  1329. } finally {
  1330. returnResource(pool, jedis);
  1331. }
  1332. return res;
  1333. }
  1334. /**
  1335. * <p>
  1336. * 通过key判断value是否是set中的元素
  1337. * </p>
  1338. *
  1339. * @param key
  1340. * @param member
  1341. * @return
  1342. */
  1343. public Boolean sismember(String key, String member) {
  1344. Jedis jedis = null;
  1345. Boolean res = null;
  1346. try {
  1347. jedis = pool.getResource();
  1348. res = jedis.sismember(key, member);
  1349. } catch (Exception e) {
  1350. pool.returnBrokenResource(jedis);
  1351. e.printStackTrace();
  1352. } finally {
  1353. returnResource(pool, jedis);
  1354. }
  1355. return res;
  1356. }
  1357. /**
  1358. * <p>
  1359. * 通过key获取set中随机的value,不删除元
  1360. * </p>
  1361. *
  1362. * @param key
  1363. * @return
  1364. */
  1365. public String srandmember(String key) {
  1366. Jedis jedis = null;
  1367. String res = null;
  1368. try {
  1369. jedis = pool.getResource();
  1370. res = jedis.srandmember(key);
  1371. } catch (Exception e) {
  1372. pool.returnBrokenResource(jedis);
  1373. e.printStackTrace();
  1374. } finally {
  1375. returnResource(pool, jedis);
  1376. }
  1377. return res;
  1378. }
  1379. /**
  1380. * <p>
  1381. * 通过key获取set中所有的value
  1382. * </p>
  1383. *
  1384. * @param key
  1385. * @return
  1386. */
  1387. public Set<String> smembers(String key) {
  1388. Jedis jedis = null;
  1389. Set<String> res = null;
  1390. try {
  1391. jedis = pool.getResource();
  1392. res = jedis.smembers(key);
  1393. } catch (Exception e) {
  1394. pool.returnBrokenResource(jedis);
  1395. e.printStackTrace();
  1396. } finally {
  1397. returnResource(pool, jedis);
  1398. }
  1399. return res;
  1400. }
  1401. /**
  1402. * 通过key向zset中添加value,score,其中score就是用来排序 如果该value已经存在则根据score更新元素
  1403. *
  1404. * @param key
  1405. * @param scoreMembers
  1406. * @return
  1407. */
  1408. public Long zadd(String key, Map<String,Double> scoreMembers) {
  1409. Jedis jedis = null;
  1410. Long res = null;
  1411. try {
  1412. jedis = pool.getResource();
  1413. res = jedis.zadd(key, scoreMembers);
  1414. } catch (Exception e) {
  1415. pool.returnBrokenResource(jedis);
  1416. e.printStackTrace();
  1417. } finally {
  1418. returnResource(pool, jedis);
  1419. }
  1420. return res;
  1421. }
  1422. /**
  1423. * 通过key向zset中添加value,score,其中score就是用来排序 如果该value已经存在则根据score更新元素
  1424. *
  1425. * @param key
  1426. * @param score
  1427. * @param member
  1428. * @return
  1429. */
  1430. public Long zadd(String key, double score, String member) {
  1431. Jedis jedis = null;
  1432. Long res = null;
  1433. try {
  1434. jedis = pool.getResource();
  1435. res = jedis.zadd(key, score, member);
  1436. } catch (Exception e) {
  1437. pool.returnBrokenResource(jedis);
  1438. e.printStackTrace();
  1439. } finally {
  1440. returnResource(pool, jedis);
  1441. }
  1442. return res;
  1443. }
  1444. /**
  1445. * 通过key删除在zset中指定的value
  1446. *
  1447. * @param key
  1448. * @param members
  1449. * 可以使一个string 也可以是个string数组
  1450. * @return
  1451. */
  1452. public Long zrem(String key, String... members) {
  1453. Jedis jedis = null;
  1454. Long res = null;
  1455. try {
  1456. jedis = pool.getResource();
  1457. res = jedis.zrem(key, members);
  1458. } catch (Exception e) {
  1459. pool.returnBrokenResource(jedis);
  1460. e.printStackTrace();
  1461. } finally {
  1462. returnResource(pool, jedis);
  1463. }
  1464. return res;
  1465. }
  1466. /**
  1467. * 通过key增加该zset中value的score的?
  1468. *
  1469. * @param key
  1470. * @param score
  1471. * @param member
  1472. * @return
  1473. */
  1474. public Double zincrby(String key, double score, String member) {
  1475. Jedis jedis = null;
  1476. Double res = null;
  1477. try {
  1478. jedis = pool.getResource();
  1479. res = jedis.zincrby(key, score, member);
  1480. } catch (Exception e) {
  1481. pool.returnBrokenResource(jedis);
  1482. e.printStackTrace();
  1483. } finally {
  1484. returnResource(pool, jedis);
  1485. }
  1486. return res;
  1487. }
  1488. public long hincrBy(String key, String field, long value) {
  1489. Jedis jedis = null;
  1490. long res = 0l;
  1491. try {
  1492. jedis = pool.getResource();
  1493. res = jedis.hincrBy(key, field, value);
  1494. } catch (Exception e) {
  1495. pool.returnBrokenResource(jedis);
  1496. e.printStackTrace();
  1497. } finally {
  1498. returnResource(pool, jedis);
  1499. }
  1500. return res;
  1501. }
  1502. /**
  1503. * 通过key返回zset中value的排 下标从小到大排序
  1504. *
  1505. * @param key
  1506. * @param member
  1507. * @return
  1508. */
  1509. public Long zrank(String key, String member) {
  1510. Jedis jedis = null;
  1511. Long res = null;
  1512. try {
  1513. jedis = pool.getResource();
  1514. res = jedis.zrank(key, member);
  1515. } catch (Exception e) {
  1516. pool.returnBrokenResource(jedis);
  1517. e.printStackTrace();
  1518. } finally {
  1519. returnResource(pool, jedis);
  1520. }
  1521. return res;
  1522. }
  1523. /**
  1524. * 通过key返回zset中value的排 下标从大到小排序
  1525. *
  1526. * @param key
  1527. * @param member
  1528. * @return
  1529. */
  1530. public Long zrevrank(String key, String member) {
  1531. Jedis jedis = null;
  1532. Long res = null;
  1533. try {
  1534. jedis = pool.getResource();
  1535. res = jedis.zrevrank(key, member);
  1536. } catch (Exception e) {
  1537. pool.returnBrokenResource(jedis);
  1538. e.printStackTrace();
  1539. } finally {
  1540. returnResource(pool, jedis);
  1541. }
  1542. return res;
  1543. }
  1544. /**
  1545. * 通过key将获取score从start到end中zset的value socre从大到小排序 当start0 end-1时返回全
  1546. *
  1547. * @param key
  1548. * @param start
  1549. * @param end
  1550. * @return
  1551. */
  1552. public Set<String> zrevrange(String key, long start, long end) {
  1553. Jedis jedis = null;
  1554. Set<String> res = null;
  1555. try {
  1556. jedis = pool.getResource();
  1557. res = jedis.zrevrange(key, start, end);
  1558. } catch (Exception e) {
  1559. pool.returnBrokenResource(jedis);
  1560. e.printStackTrace();
  1561. } finally {
  1562. returnResource(pool, jedis);
  1563. }
  1564. return res;
  1565. }
  1566. /**
  1567. * 通过key返回指定score内zset中的value
  1568. *
  1569. * @param key
  1570. * @param max
  1571. * @param min
  1572. * @return
  1573. */
  1574. public Set<String> zrangebyscore(String key, String max, String min) {
  1575. Jedis jedis = null;
  1576. Set<String> res = null;
  1577. try {
  1578. jedis = pool.getResource();
  1579. res = jedis.zrevrangeByScore(key, max, min);
  1580. } catch (Exception e) {
  1581. pool.returnBrokenResource(jedis);
  1582. e.printStackTrace();
  1583. } finally {
  1584. returnResource(pool, jedis);
  1585. }
  1586. return res;
  1587. }
  1588. /**
  1589. * 通过key返回指定score内zset中的value
  1590. *
  1591. * @param key
  1592. * @param max
  1593. * @param min
  1594. * @return
  1595. */
  1596. public Set<String> zrangeByScore(String key, double max, double min) {
  1597. Jedis jedis = null;
  1598. Set<String> res = null;
  1599. try {
  1600. jedis = pool.getResource();
  1601. res = jedis.zrevrangeByScore(key, max, min);
  1602. } catch (Exception e) {
  1603. pool.returnBrokenResource(jedis);
  1604. e.printStackTrace();
  1605. } finally {
  1606. returnResource(pool, jedis);
  1607. }
  1608. return res;
  1609. }
  1610. /**
  1611. * 返回指定区间内zset中value的数
  1612. *
  1613. * @param key
  1614. * @param min
  1615. * @param max
  1616. * @return
  1617. */
  1618. public Long zcount(String key, String min, String max) {
  1619. Jedis jedis = null;
  1620. Long res = null;
  1621. try {
  1622. jedis = pool.getResource();
  1623. res = jedis.zcount(key, min, max);
  1624. } catch (Exception e) {
  1625. pool.returnBrokenResource(jedis);
  1626. e.printStackTrace();
  1627. } finally {
  1628. returnResource(pool, jedis);
  1629. }
  1630. return res;
  1631. }
  1632. /**
  1633. * 通过key返回zset中的value个数
  1634. *
  1635. * @param key
  1636. * @return
  1637. */
  1638. public Long zcard(String key) {
  1639. Jedis jedis = null;
  1640. Long res = null;
  1641. try {
  1642. jedis = pool.getResource();
  1643. res = jedis.zcard(key);
  1644. } catch (Exception e) {
  1645. pool.returnBrokenResource(jedis);
  1646. e.printStackTrace();
  1647. } finally {
  1648. returnResource(pool, jedis);
  1649. }
  1650. return res;
  1651. }
  1652. /**
  1653. * 通过key获取zset中value的score
  1654. *
  1655. * @param key
  1656. * @param member
  1657. * @return
  1658. */
  1659. public Double zscore(String key, String member) {
  1660. Jedis jedis = null;
  1661. Double res = null;
  1662. try {
  1663. jedis = pool.getResource();
  1664. res = jedis.zscore(key, member);
  1665. } catch (Exception e) {
  1666. pool.returnBrokenResource(jedis);
  1667. e.printStackTrace();
  1668. } finally {
  1669. returnResource(pool, jedis);
  1670. }
  1671. return res;
  1672. }
  1673. /**
  1674. * 通过key删除给定区间内的元素
  1675. *
  1676. * @param key
  1677. * @param start
  1678. * @param end
  1679. * @return
  1680. */
  1681. public Long zremrangeByRank(String key, long start, long end) {
  1682. Jedis jedis = null;
  1683. Long res = null;
  1684. try {
  1685. jedis = pool.getResource();
  1686. res = jedis.zremrangeByRank(key, start, end);
  1687. } catch (Exception e) {
  1688. pool.returnBrokenResource(jedis);
  1689. e.printStackTrace();
  1690. } finally {
  1691. returnResource(pool, jedis);
  1692. }
  1693. return res;
  1694. }
  1695. /**
  1696. * 通过key删除指定score内的元素
  1697. *
  1698. * @param key
  1699. * @param start
  1700. * @param end
  1701. * @return
  1702. */
  1703. public Long zremrangeByScore(String key, double start, double end) {
  1704. Jedis jedis = null;
  1705. Long res = null;
  1706. try {
  1707. jedis = pool.getResource();
  1708. res = jedis.zremrangeByScore(key, start, end);
  1709. } catch (Exception e) {
  1710. pool.returnBrokenResource(jedis);
  1711. e.printStackTrace();
  1712. } finally {
  1713. returnResource(pool, jedis);
  1714. }
  1715. return res;
  1716. }
  1717. /**
  1718. * 返回满足pattern表达式的有key keys(*) 返回有的key
  1719. *
  1720. * @param pattern
  1721. * @return
  1722. */
  1723. public Set<String> keys(String pattern) {
  1724. Jedis jedis = null;
  1725. Set<String> res = null;
  1726. try {
  1727. jedis = pool.getResource();
  1728. res = jedis.keys(pattern);
  1729. } catch (Exception e) {
  1730. pool.returnBrokenResource(jedis);
  1731. e.printStackTrace();
  1732. } finally {
  1733. returnResource(pool, jedis);
  1734. }
  1735. return res;
  1736. }
  1737. /**
  1738. * 通过key判断值得类型
  1739. *
  1740. * @param key
  1741. * @return
  1742. */
  1743. public String type(String key) {
  1744. Jedis jedis = null;
  1745. String res = null;
  1746. try {
  1747. jedis = pool.getResource();
  1748. res = jedis.type(key);
  1749. } catch (Exception e) {
  1750. pool.returnBrokenResource(jedis);
  1751. e.printStackTrace();
  1752. } finally {
  1753. returnResource(pool, jedis);
  1754. }
  1755. return res;
  1756. }
  1757. /**
  1758. * 返还到连接池
  1759. *
  1760. * @param pool
  1761. * @param redis
  1762. */
  1763. public static void returnResource(JedisPool pool, Jedis jedis) {
  1764. if (jedis != null) {
  1765. pool.returnResource(jedis);
  1766. }
  1767. }
  1768. }

 

 

 

 

 

 

 

 

 

 

 

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
redis缓存和mysql数据库同步
springboot使用redis指南
使用 pipeline 提升 Redis 的访问性能
Jedis对redis的操作详解
缓存对事务的支持
什么是分布式锁及正确使用redis实现分布式锁
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服