项目中用到了redis缓存,稍微整理下,有错误还望指正。
易错点:
1.注解方式的使用,注意点是,使用注解的方式,注解@cacheable(...)等声明的一定是该类直接对外的第一层方法,不能是该类的某个方法调用的下一层方法!!!!否则, 缓存不会生效
spring boot 版本信息
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
1.基本配置,在application.yml文件添加
spring:
#redis配置
redis:
host: 127.0.0.1
port: 6379
password: 123456
jedis:
pool:
max-active: 8
max-wait: -1ms
min-idle: 0
max-idle: 8
timeout: 10000
2.配置缓存的key生成,缓存管理,异常处理等
package com.example.demo;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
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.*;
import java.lang.reflect.Method;
import java.time.Duration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* Created by He on 2018/8/23.
*/
@Configuration
@EnableCaching
@Slf4j
public class RedisConfig extends CachingConfigurerSupport {
/**
* @return 自定义策略生成的key
* @description 自定义的缓存key的生成策略
* 若想使用这个key 只需要讲注解上keyGenerator的值设置为keyGenerator即可</br>
*/
@Bean
public KeyGenerator keyGenerator() {
return (target, method, params) -> {
StringBuffer sb = new StringBuffer();
sb.append(target.getClass().getName());
sb.append(":");
sb.append(method.getName());
for (Object obj : params) {
sb.append(":" + obj.toString());
}
return sb.toString();
};
}
//缓存管理器,只使用注解方式,非注解方式使用原来的方式设置参数
@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory factory) {
/*以下为Spring boot1.x的方式*/
//方式1
// RedisCacheManager rcm = new RedisCacheManager(redisTemplate);//这里的redisTemplate使用redisTemplate(factory)
// //设置缓存过期时间
// rcm.setDefaultExpiration(60);//秒
// return rcm;
//方式2
// RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager
// .RedisCacheManagerBuilder
// .fromConnectionFactory(factory);
// return builder.build();
//方式3
// RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(factory);
// RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig();
// RedisCacheConfiguration configuration = defaultCacheConfig.entryTtl(Duration.ofSeconds(100));
// RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter, configuration);
/***************以下为Spring boot2.x的方式*************************/
//方式1,最简单的配置
// return RedisCacheManager.create(factory);
/*方式2,需要单独配置每个具体缓存空间的过期时间时使用,注意cacheName的对应*/
// 生成一个默认配置,通过config对象即可对缓存进行自定义配置
RedisCacheConfiguration cacheConfig &#