依星源码资源网,依星资源网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

【好消息,好消息,好消息】VIP会员可以发表文章赚积分啦 !
查看: 89|回复: 0

SpringBoot 集成Jedis操作set

[复制链接] 主动推送

1万

主题

1万

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
15437
发表于 2025-4-15 12:12:26 | 显示全部楼层 |阅读模式
SpringBoot 集成Jedis操作set
题外话:
Redis是个有趣的东西,相信搞java的或多或少都会用到,面试时也总离不开问Redis,之前觉得redis只是用做缓存,飞快!也因为最初在封装底层的时候,使用Redisson,所以大部分都只用到了String这种类型,不管相应的value是List还是Map,最多也就以json格式存储,慢慢的用多了,才发现在业务中错过了许多优化的地方;
其中Set类型是一个不错的选择,举个例子,我们实际业务中存在粉丝订阅关系,同时,因为采用Spring Cloud分布式架构,加上各个微服务之间做了分库,导致许多地方在查询时需要feign调用订阅关系去做其他逻辑,用Set存储可以解决粉丝关注,粉丝数统计,我关注的人也关注了谁等等问题;
1、pom.xml
  1. <!-- jedis -->
  2. <dependency>
  3. <groupId>redis.clients</groupId>
  4. <artifactId>jedis</artifactId>
  5. <version>2.8.2</version>
  6. </dependency>
复制代码
2、注入bean
  1. @Bean
  2. public JedisPool redisPoolFactory(
  3. @Value("${spring.redis.host}") String redisHost,
  4. @Value("${spring.redis.port}") int redisPort,
  5. @Value("${spring.redis.password}") String redisPassword,
  6. @Value("${spring.redis.database}") int database ,
  7. @Value("${spring.redis.jedis.pool.max-wait}") int maxWaitMillis,
  8. @Value("${spring.redis.jedis.pool.max-idle}") int maxIdle,
  9. @Value("${spring.redis.jedis.pool.max-active}") int maxActive
  10.         ){
  11.         JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
  12.         jedisPoolConfig.setMaxIdle(maxIdle);
  13.         jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
  14.         jedisPoolConfig.setMaxTotal(maxActive);
  15.         jedisPoolConfig.setMinIdle(0);
  16.         jedisPoolConfig.setMaxIdle(maxIdle);
  17.         JedisPool jedisPool = new JedisPool(jedisPoolConfig,redisHost,redisPort,0,redisPassword);
  18.         return jedisPool;
  19.         }
  20. @Bean
  21. public JedisUtils jedisUtils (JedisPool jedisPool ){
  22.         return new JedisUtils(jedisPool);
  23.         }
复制代码
3、JedisUtils操作set
  1. package com.cookie.util;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Component;
  6. import redis.clients.jedis.Jedis;
  7. import redis.clients.jedis.JedisPool;
  8. import java.util.Set;
  9. /**
  10. * author : cxq
  11. * Date : 2019/7/11
  12. */
  13. //@Component
  14. public class JedisUtils {
  15.     private final static Logger logger = LoggerFactory.getLogger(JedisUtils.class);
  16.     // @Autowired
  17.     private JedisPool jedisPool ;
  18.     public JedisUtils(JedisPool jedisPool) {
  19.         this.jedisPool = jedisPool;
  20.     }
  21.     /**
  22.      * 查询set集合数据
  23.      * @param key
  24.      * @return
  25.      */
  26.     public Set<String> getSet(String key ){
  27.         Jedis jedis = null ;
  28.         Set<String> set = null ;
  29.         try {
  30.             jedis = jedisPool.getResource();
  31.             set = jedis.smembers(key);
  32.         }catch (Exception e ){
  33.             logger.error(" get set error : "+e.getMessage());
  34.         }
  35.         return set ;
  36.     }
  37.     /**
  38.      * 往set中添加数据
  39.      * @param key
  40.      * @param values
  41.      * @return
  42.      */
  43.     public Long addSet(String key , String... values ){
  44.         Jedis jedis = null ;
  45.         try {
  46.             jedis = jedisPool.getResource();
  47.             return jedis.sadd(key,values);
  48.         }catch (Exception e ){
  49.             logger.error(" get set error : "+e.getMessage());
  50.         }
  51.         return 0L ;
  52.     }
  53.     /**
  54.      * 删除数据
  55.      * @param key
  56.      * @param values
  57.      * @return
  58.      */
  59.     public Long delSet(String key , String... values ){
  60.         Jedis jedis = null ;
  61.         try {
  62.             jedis = jedisPool.getResource();
  63.             return jedis.srem(key,values);
  64.         }catch (Exception e ){
  65.             logger.error(" del set error : "+e.getMessage());
  66.         }
  67.         return 0L ;
  68.     }
  69.     /**
  70.      * 求第一个key与其他key不同的部分
  71.      * @param keys
  72.      * @return
  73.      */
  74.     public Set<String> getDiffSet(String... keys){
  75.         Jedis jedis = null ;
  76.         try {
  77.             jedis = jedisPool.getResource();
  78.             return jedis.sdiff(keys);
  79.         }catch (Exception e ){
  80.             logger.error(" get diff set error : "+e.getMessage());
  81.         }
  82.         return null ;
  83.     }
  84.     /**
  85.      * 求key的合集
  86.      * @param keys
  87.      * @return
  88.      */
  89.     public Set<String> getUnionSet(String... keys){
  90.         Jedis jedis = null ;
  91.         try {
  92.             jedis = jedisPool.getResource();
  93.             return jedis.sunion(keys);
  94.         }catch (Exception e ){
  95.             logger.error(" get union set error : "+e.getMessage());
  96.         }
  97.         return null ;
  98.     }
  99.     /**
  100.      * 求key的交集
  101.      * @param keys
  102.      * @return
  103.      */
  104.     public Set<String> getInterSet(String... keys){
  105.         Jedis jedis = null ;
  106.         try {
  107.             jedis = jedisPool.getResource();
  108.             return jedis.sinter(keys);
  109.         }catch (Exception e ){
  110.             logger.error(" get inter set error : "+e.getMessage());
  111.         }
  112.         return null ;
  113.     }
  114.     /**
  115.      * 获取key的长度
  116.      * @param key
  117.      * @return
  118.      */
  119.     public Long getSetCount(String key ){
  120.         Jedis jedis = null ;
  121.         try {
  122.             jedis = jedisPool.getResource();
  123.             return jedis.scard(key);
  124.         }catch (Exception e ){
  125.             logger.error(" get set count error : "+e.getMessage());
  126.         }
  127.         return 0L ;
  128.     }
  129.     /**
  130.      * 判断值是否存在
  131.      * @param key
  132.      * @param value
  133.      * @return
  134.      */
  135.     public boolean checkValueIsInSet(String key , String value ){
  136.         Jedis jedis = null ;
  137.         try {
  138.             jedis = jedisPool.getResource();
  139.             return jedis.sismember(key,value);
  140.         }catch (Exception e ){
  141.             logger.error(" check member is in set error : "+e.getMessage());
  142.         }
  143.         return false ;
  144.     }
  145. }
复制代码


相关帖子

扫码关注微信公众号,及时获取最新资源信息!下载附件优惠VIP会员6折;永久VIP4折
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

免责声明:
1、本站提供的所有资源仅供参考学习使用,版权归原著所有,禁止下载本站资源参与商业和非法行为,请在24小时之内自行删除!
2、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,请勿任何商业目的与商业用途。
3、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。
4、论坛的所有内容都不保证其准确性,完整性,有效性,由于源码具有复制性,一经售出,概不退换。阅读本站内容因误导等因素而造成的损失本站不承担连带责任。
5、用户使用本网站必须遵守适用的法律法规,对于用户违法使用本站非法运营而引起的一切责任,由用户自行承担
6、本站所有资源来自互联网转载,版权归原著所有,用户访问和使用本站的条件是必须接受本站“免责声明”,如果不遵守,请勿访问或使用本网站
7、本站使用者因为违反本声明的规定而触犯中华人民共和国法律的,一切后果自己负责,本站不承担任何责任。
8、凡以任何方式登陆本网站或直接、间接使用本网站资料者,视为自愿接受本网站声明的约束。
9、本站以《2013 中华人民共和国计算机软件保护条例》第二章 “软件著作权” 第十七条为原则:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。若有学员需要商用本站资源,请务必联系版权方购买正版授权!
10、本网站如无意中侵犯了某个企业或个人的知识产权,请来信【站长信箱312337667@qq.com】告之,本站将立即删除。
郑重声明:
本站所有资源仅供用户本地电脑学习源代码的内含设计思想和原理,禁止任何其他用途!
本站所有资源、教程来自互联网转载,仅供学习交流,不得商业运营资源,不确保资源完整性,图片和资源仅供参考,不提供任何技术服务。
本站资源仅供本地编辑研究学习参考,禁止未经资源商正版授权参与任何商业行为,违法行为!如需商业请购买各资源商正版授权
本站仅收集资源,提供用户自学研究使用,本站不存在私自接受协助用户架设游戏或资源,非法运营资源行为。
 
在线客服
点击这里给我发消息 点击这里给我发消息 点击这里给我发消息
售前咨询热线
312337667

微信扫一扫,私享最新原创实用干货

QQ|免责声明|小黑屋|依星资源网 ( 鲁ICP备2021043233号-3 )|网站地图

GMT+8, 2025-4-30 11:01

Powered by Net188.com X3.4

邮箱:312337667@qq.com 客服QQ:312337667(工作时间:9:00~21:00)

快速回复 返回顶部 返回列表