pom.xml
<!--reids集成-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
properties配置
#数据库索引 默认0
spring.redis.database=0
#redis服务器地址
spring.redis.host=127.0.0.1
#redis服务器端口
spring.redis.port=6379
#redis服务器连接密码
spring.redis.password=XXX
#连接池最大连接数(使用负数表示没有限制) 默认8
spring.redis.lettuce.pool.max-active=8
#连接池最大阻塞等待时间 默认没有限制 -1
spring.redis.lettuce.pool.max-wait=-1
#连接池中最大空闲连接 默认8
spring.redis.lettuce.pool.max-idle=8
#连接池中最小空闲连接 默认0
spring.redis.lettuce.pool.min-idle=0
redisTemplate配置,重写key、value序列化
@Configuration
@EnableCaching // 支持缓存
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate) {
// 初始化一个RedisCacheWriter
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory());
// 设置默认过期时间30M
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer()))
.entryTtl(Duration.ofMinutes(30L))
.disableCachingNullValues();
// 初始化RedisCacheManager
RedisCacheManager redisCacheManager = new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
return redisCacheManager;
}
/**
* retemplate相关配置
* springboot2.x 之后默认使用lettuce客户端连接(线程安全)
*
* @param factory
* @return
*/
@Bean(name = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 配置连接工厂
template.setConnectionFactory(factory);
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
// 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jacksonSeial.setObjectMapper(om);
// 值采用json序列化
template.setValueSerializer(jacksonSeial);
//使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
// 设置hash key 和value序列化模式
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(jacksonSeial);
template.afterPropertiesSet();
return template;
}
/**
* 对hash类型的数据操作
*
* @param redisTemplate
* @return
*/
@Bean
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForHash();
}
/**
* 对redis字符串类型数据操作
*
* @param redisTemplate
* @return
*/
@Bean
public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForValue();
}
/**
* 对链表类型的数据操作
*
* @param redisTemplate
* @return
*/
@Bean
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForList();
}
/**
* 对无序集合类型的数据操作
*
* @param redisTemplate
* @return
*/
@Bean
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForSet();
}
/**
* 对有序集合类型的数据操作
*
* @param redisTemplate
* @return
*/
@Bean
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForZSet();
}
}
调用示例1(注解):
@Cacheable(value = "listXxx", key = "#key")
public List<String> listXxx(String key) {
System.out.println("执行了listXxx方法");
return Arrays.asList("xxx", "yyy", System.currentTimeMillis() + "");
}
调用示例2:
@Autowired
private RedisTemplate redisTemplate;
/**
* 获取数据
* @param key
* @param field
* @return
*/
public String getRedisHash(String key, String field) {
HashOperations<String, String, String> hash = redisTemplate.opsForHash();
if (StringUtils.isEmpty(key)) {
return "";
}
String value = StringUtils.isEmpty(field) ? hash.get(key, key) : hash.get(key, field);
return (StringUtils.isEmpty(value) || "[]".equals(value)) ? "" : value;
}
/**
* redis缓存,只适用hash类型存储
* 默认缓存永久
*
* @param key redis 键
* @param field hash 键
* @param value 所缓存的数据
*/
public void setRedisHash(String key, String field, String value) {
//redis hash 对象,用来存储数据
HashOperations<String, String, String> hash = redisTemplate.opsForHash();
if (StringUtils.isEmpty(field)) {
hash.put(key, key, value);
} else {
hash.put(key, field, value);
}
}