题外话:
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 操作 set
- package 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 ;
- }
- }
来源: https://www.cnblogs.com/pretttyboy/p/11325853.html