这里做一个工作笔记,业务场景:redis缓存配置,及redis工具类整合
先做redis配置:
pom.xml文件新增
<!-- redis -->
<spring-redis.version>1.8.10.RELEASE</spring-redis.version>
<redis-jedis.version>2.9.0</redis-jedis.version>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring-redis.version}</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${redis-jedis.version}</version>
</dependency>
redis.properties文件
# REDIS配置
# Redis数据库
spring.redis.database=0
# Redis地址
spring.redis.host=172.16.6.75
# Redis端口
spring.redis.port=6379
# Redis密码
spring.redis.password=redis@112358
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
初始化及开启redis缓存配置:RedisCacheConfig.java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
import java.io.Serializable;
/**
* redis缓存配置类
* @author WangHan
* @date 2018/4/3 14:23
*/
@Configuration
@EnableCaching
@PropertySource("classpath:redis.properties")
public class RedisCacheConfig extends CachingConfigurerSupport {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.pool.max-active}")
private int maxActive;
@Value("${spring.redis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.pool.min-idle}")
private int minIdle;
@Value("${spring.redis.pool.max-wait}")
private int maxWait;
@Bean
public JedisConnectionFactory redisConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setPassword(password);
factory.setHostName(host);
factory.setPort(port);
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(maxActive);
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMinIdle(minIdle);
jedisPoolConfig.setMaxWaitMillis(maxWait);
factory.setPoolConfig(jedisPoolConfig);
return factory;
}
@Bean
public RedisTemplate<String, Serializable> getRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
RedisSerializer redisSerializer = new JdkSerializationRedisSerializer();
redisTemplate.setDefaultSerializer(redisSerializer);
RedisSerializer<String> keySerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(keySerializer);
return redisTemplate;
}
@Bean
public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate) {
RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
redisCacheManager.setDefaultExpiration(3600 * 12);
return redisCacheManager;
}
}
新增redis工具类:
操作接口类:RedisCommand.java
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.ZSetOperations;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
/**
* redis操作工具接口
* @author WangHan
* @date 2018/7/3 10:56
*/
public interface RedisCommand<K, V> {
/**
* 用户排序通过注册时间的 权重值
* @param date
* @return
*/
double getCreateTimeScore(long date);
/**
* 获取Redis中所有的键的key
* @return
*/
Set<K> getAllKeys();
/**
* 获取所有的普通key-value
* @return
*/
Map<K,V> getAllString();
/**
* 获取所有的Set -key-value
* @return
*/
Map<K,Set<V>> getAllSet();
/**
* 获取所有的ZSet正序 -key-value 不获取权重值
* @return
*/
Map<K,Set<V>> getAllZSetReverseRange();
/**
* 获取所有的ZSet倒序 -key-value 不获取权重值
* @return
*/
Map<K,Set<V>> getAllZSetRange();
/**
* 获取所有的List -key-value
* @return
*/
Map<K,List<V>> getAllList();
/**
* 获取所有的Map -key-value
* @return
*/
Map<K,Map<K,V>> getAllMap();
/**
* 添加一个list
* @param key
* @param objectList
*/
void addList(K key, List<V> objectList);
/**
* 向list中增加值
* @param key
* @param obj
* @return 返回在list中的下标
*/
long addList(K key,V obj);
/**
*
* 向list中增加值
* @param key
* @param obj
* @return 返回在list中的下标
*/
long addList(K key,V ...obj);
/**
*
* 输出list
* @param key List的key
* @param s 开始下标
* @param e 结束的下标
* @return
*/
List<V> getList(K key, long s, long e);
/**
* 输出