题外话:
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
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
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 ;
}
}
















