<!-- 缓存-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- redis操作-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
import com.ourgame.lianzhongshowlog.exception.IgnoreExceptionCacheErrorHandler;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.*;
import javax.annotation.Resource;
import java.time.Duration;
import static org.springframework.data.redis.cache.RedisCacheConfiguration.defaultCacheConfig;
/**
* @author wuyd
* 创建时间:2020/5/14 14:51
*/
@Configuration
public class CacheConfig extends CachingConfigurerSupport {
/**
* 添加自定义缓存异常处理
* 当缓存读写异常时,忽略异常
*/
@Override
public CacheErrorHandler errorHandler() {
return new IgnoreExceptionCacheErrorHandler();
}
/**
* cacheManager名称
*/
public interface CacheManagerName {
String CACHE_MANAGER_30 = "CacheCacheManager30";
}
@Resource
private RedisConnectionFactory factory;
@Override
public KeyGenerator keyGenerator() {
return super.keyGenerator();
}
@Override
public CacheResolver cacheResolver() {
return new SimpleCacheResolver(cacheManager());
}
@Override
public CacheManager cacheManager() {
RedisCacheConfiguration cacheConfiguration =
defaultCacheConfig()
.entryTtl(Duration.ofMinutes(5))
.disableCachingNullValues()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer()));
return RedisCacheManager.builder(factory)
.cacheDefaults(cacheConfiguration).build();
}
/**
* 30分钟缓存
* @return cacheManager
*/
@Primary
@Bean(CacheManagerName.CACHE_MANAGER_30)
public CacheManager cacheManager30() {
RedisCacheConfiguration cacheConfiguration =
defaultCacheConfig()
.entryTtl(Duration.ofMinutes(30))
.disableCachingNullValues()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer()));
return RedisCacheManager.builder(factory)
.cacheDefaults(cacheConfiguration).build();
}
private RedisSerializer<String> keySerializer() {
return new StringRedisSerializer();
}
private GenericJackson2JsonRedisSerializer valueSerializer() {
return new GenericJackson2JsonRedisSerializer();
}
//IgnoreExceptionCacheErrorHandler
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.cache.interceptor.CacheErrorHandler;
/**
* @author wuyd
*/
@Slf4j
public class IgnoreExceptionCacheErrorHandler implements CacheErrorHandler {
@Override
public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
log.error("[缓存] handleCacheGetError,ex:{},key:{}",exception.getMessage(), key);
}
@Override
public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) {
log.error("[缓存] handleCachePutError,ex:{},key:{},val{}",exception.getMessage(), key, value);
}
@Override
public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
log.error("[缓存] handleCacheGetError,ex:{},key:{}",exception.getMessage(), key);
}
@Override
public void handleCacheClearError(RuntimeException exception, Cache cache) {
log.error("[缓存] handleCacheGetError,ex:{},cache:{}",exception.getMessage(), cache.toString());
}
}
这里采用了 Jackson2JsonRedisSerializer 做缓存的序列化,默认的时间和通用性不太好看这里格式化后如图
默认的在处理序列化的时候容易踩坑,比如时间序列化,多项目同步缓存等.
是我想要的
[{"labelId":"test1","labelName":"activityTest1","labelType":true,"labelDesc":"测试活动","labelDisplayOrder":1,"startDate":"2020-06-15 17:46:52","endDate":"2021-08-20 17:46:55","imgUrl":null,"isAcive":false,"sendLabelNum":0}]
默认5分钟
@Cacheable(value = "test:photo", key = "#id", unless = "#result == null")
30分钟
@Cacheable(value = "test:photo", key = "#id", cacheManager = CacheConfig.CacheManagerName.CACHE_MANAGER_30 unless = "#result == null")
这里只有简单的demo例子,具体的put ext等使用请看官网
spring boot data
2020年6月19日 更改源代码,解决反序列化时报错问题;