源码资源网 发表于 2025-4-15 12:12:26

SpringBoot 集成Jedis操作set

SpringBoot 集成Jedis操作set
题外话:Redis是个有趣的东西,相信搞java的或多或少都会用到,面试时也总离不开问Redis,之前觉得redis只是用做缓存,飞快!也因为最初在封装底层的时候,使用Redisson,所以大部分都只用到了String这种类型,不管相应的value是List还是Map,最多也就以json格式存储,慢慢的用多了,才发现在业务中错过了许多优化的地方;其中Set类型是一个不错的选择,举个例子,我们实际业务中存在粉丝订阅关系,同时,因为采用Spring Cloud分布式架构,加上各个微服务之间做了分库,导致许多地方在查询时需要feign调用订阅关系去做其他逻辑,用Set存储可以解决粉丝关注,粉丝数统计,我关注的人也关注了谁等等问题;1、pom.xml<!-- jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.2</version>
</dependency>2、注入bean@Bean
public JedisPool redisPoolFactory(
@Value("${spring.redis.host}") String redisHost,
@Value("${spring.redis.port}") int redisPort,
@Value("${spring.redis.password}") String redisPassword,
@Value("${spring.redis.database}") int database ,
@Value("${spring.redis.jedis.pool.max-wait}") int maxWaitMillis,
@Value("${spring.redis.jedis.pool.max-idle}") int maxIdle,
@Value("${spring.redis.jedis.pool.max-active}") int maxActive
      ){
      JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
      jedisPoolConfig.setMaxIdle(maxIdle);
      jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
      jedisPoolConfig.setMaxTotal(maxActive);
      jedisPoolConfig.setMinIdle(0);
      jedisPoolConfig.setMaxIdle(maxIdle);
      JedisPool jedisPool = new JedisPool(jedisPoolConfig,redisHost,redisPort,0,redisPassword);
      return jedisPool;
      }
@Bean
public JedisUtils jedisUtils (JedisPool jedisPool ){
      return new JedisUtils(jedisPool);
      }3、JedisUtils操作setpackage com.cookie.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.Set;
/**
* author : cxq
* Date : 2019/7/11
*/
//@Component
public class JedisUtils {
    private final static Logger logger = LoggerFactory.getLogger(JedisUtils.class);
    // @Autowired
    private JedisPool jedisPool ;
    public JedisUtils(JedisPool jedisPool) {
      this.jedisPool = jedisPool;
    }
    /**
   * 查询set集合数据
   * @param key
   * @return
   */
    public Set<String> getSet(String key ){
      Jedis jedis = null ;
      Set<String> set = null ;
      try {
            jedis = jedisPool.getResource();
            set = jedis.smembers(key);
      }catch (Exception e ){
            logger.error(" get set error : "+e.getMessage());
      }
      return set ;
    }
    /**
   * 往set中添加数据
   * @param key
   * @param values
   * @return
   */
    public Long addSet(String key , String... values ){
      Jedis jedis = null ;
      try {
            jedis = jedisPool.getResource();
            return jedis.sadd(key,values);
      }catch (Exception e ){
            logger.error(" get set error : "+e.getMessage());
      }
      return 0L ;
    }
    /**
   * 删除数据
   * @param key
   * @param values
   * @return
   */
    public Long delSet(String key , String... values ){
      Jedis jedis = null ;
      try {
            jedis = jedisPool.getResource();
            return jedis.srem(key,values);
      }catch (Exception e ){
            logger.error(" del set error : "+e.getMessage());
      }
      return 0L ;
    }
    /**
   * 求第一个key与其他key不同的部分
   * @param keys
   * @return
   */
    public Set<String> getDiffSet(String... keys){
      Jedis jedis = null ;
      try {
            jedis = jedisPool.getResource();
            return jedis.sdiff(keys);
      }catch (Exception e ){
            logger.error(" get diff set error : "+e.getMessage());
      }
      return null ;
    }
    /**
   * 求key的合集
   * @param keys
   * @return
   */
    public Set<String> getUnionSet(String... keys){
      Jedis jedis = null ;
      try {
            jedis = jedisPool.getResource();
            return jedis.sunion(keys);
      }catch (Exception e ){
            logger.error(" get union set error : "+e.getMessage());
      }
      return null ;
    }
    /**
   * 求key的交集
   * @param keys
   * @return
   */
    public Set<String> getInterSet(String... keys){
      Jedis jedis = null ;
      try {
            jedis = jedisPool.getResource();
            return jedis.sinter(keys);
      }catch (Exception e ){
            logger.error(" get inter set error : "+e.getMessage());
      }
      return null ;
    }
    /**
   * 获取key的长度
   * @param key
   * @return
   */
    public Long getSetCount(String key ){
      Jedis jedis = null ;
      try {
            jedis = jedisPool.getResource();
            return jedis.scard(key);
      }catch (Exception e ){
            logger.error(" get set count error : "+e.getMessage());
      }
      return 0L ;
    }
    /**
   * 判断值是否存在
   * @param key
   * @param value
   * @return
   */
    public boolean checkValueIsInSet(String key , String value ){
      Jedis jedis = null ;
      try {
            jedis = jedisPool.getResource();
            return jedis.sismember(key,value);
      }catch (Exception e ){
            logger.error(" check member is in set error : "+e.getMessage());
      }
      return false ;
    }
}

页: [1]
查看完整版本: SpringBoot 集成Jedis操作set

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