Yohann-study
基于@Cacheable自定义缓存策略
思路
-
自定义缓存key生成策略
-
自定义缓存策略
-
自定义注解,注解包含@Cacheable并设置固定属性
自定义注解
package com.yohann.boot.common.annotation;
import org.springframework.cache.annotation.Cacheable;
import java.lang.annotation.*;
@Cacheable(value = "Cache30Minutes",
keyGenerator = Cache30Minutes.KEY_GENERATOR,
cacheManager = Cache30Minutes.CACHE_MANAGER)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Cache30Minutes {
/**
* key生成策略名称
*/
String KEY_GENERATOR = "cache30MinutesKeyGenerator";
/**
* 缓存策略配置
*/
String CACHE_MANAGER = "cache30MinutesManager";
}
自定义key生成策略
package com.yohann.boot.config;
import com.alibaba.fastjson.JSON;
import com.yohann.boot.common.annotation.Cache30Minutes;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Component(Cache30Minutes.KEY_GENERATOR)
public class Cache30MinutesKeyGenerator implements KeyGenerator {
@Override
public Object generate(Object target, Method method, Object... objects) {
String className = target.getClass().getName();
String methodName = method.getName();
String encode = MD5Encoder.encode(JSON.toJSONString(objects).getBytes());
//类名:方法名:方法入参
return String.join(":", className, methodName, encode);
}
}
自定义缓存策略
package com.yohann.boot.config;
import com.yohann.boot.common.annotation.Cache30Minutes;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
@EnableCaching
@Configuration
public class CacheManagerConfig {
@Bean(Cache30Minutes.CACHE_MANAGER)
public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
//创建序列化器
RedisSerializationContext.SerializationPair<String> keySerializer = RedisSerializationContext
.SerializationPair
.fromSerializer(new StringRedisSerializer());
RedisSerializationContext.SerializationPair<Object> valueSerializer = RedisSerializationContext
.SerializationPair
.fromSerializer(new GenericJackson2JsonRedisSerializer());
//配置序列化器和超时时间
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(keySerializer)
.serializeValuesWith(valueSerializer)
.entryTtl(Duration.ofMinutes(30L));
return new RedisCacheManager(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory), configuration);
}
}