SpringBoot 2.X @Cacheable,redis-cache 如何根据key设置缓存时间?
分类:软件编程
阅读:30
作者:皇太极
发布:2019-09-14 23:34:50

如题记录下使用spring-cache设置缓存,并对key设置缓存时间

由于全部交由框架来完成,不能做到对缓存数据的有效管理,故又使用自定义注解完成了缓存的处理
见下一篇文章(待续…)

如下代码参考 https://segmentfault.com/q/1010000015203664

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-cache</artifactId>
  4. </dependency>
  1. package cn.majingjing.tm.blog.config;
  2. import com.fasterxml.jackson.annotation.JsonAutoDetect;
  3. import com.fasterxml.jackson.annotation.PropertyAccessor;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import org.springframework.cache.CacheManager;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.data.redis.cache.RedisCacheConfiguration;
  9. import org.springframework.data.redis.cache.RedisCacheManager;
  10. import org.springframework.data.redis.cache.RedisCacheWriter;
  11. import org.springframework.data.redis.connection.RedisConnectionFactory;
  12. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  13. import org.springframework.data.redis.serializer.RedisSerializationContext;
  14. import java.time.Duration;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. /**
  18. * @Author JingjingMa
  19. * @Date 2019/9/15 12:03
  20. */
  21. @Configuration
  22. public class RedisCacheConfig {
  23. // @Bean
  24. // public KeyGenerator simpleKeyGenerator() {
  25. // return (o, method, objects) -> {
  26. // StringBuilder stringBuilder = new StringBuilder();
  27. // stringBuilder.append(o.getClass().getSimpleName());
  28. // stringBuilder.append(".");
  29. // stringBuilder.append(method.getName());
  30. // stringBuilder.append("[");
  31. // for (Object obj : objects) {
  32. // stringBuilder.append(obj.toString());
  33. // }
  34. // stringBuilder.append("]");
  35. //
  36. // return stringBuilder.toString();
  37. // };
  38. // }
  39. @Bean
  40. public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
  41. return new RedisCacheManager(
  42. RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
  43. // 默认策略,未配置的 key 会使用这个
  44. this.getRedisCacheConfigurationWithTtl(600),
  45. // 指定 key 策略
  46. this.getRedisCacheConfigurationMap()
  47. );
  48. }
  49. private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
  50. Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
  51. redisCacheConfigurationMap.put("tmblog:web:blog", this.getRedisCacheConfigurationWithTtl(3000));
  52. redisCacheConfigurationMap.put("tmblog:web:article", this.getRedisCacheConfigurationWithTtl(18000));
  53. return redisCacheConfigurationMap;
  54. }
  55. private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {
  56. Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
  57. ObjectMapper om = new ObjectMapper();
  58. om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  59. om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  60. jackson2JsonRedisSerializer.setObjectMapper(om);
  61. RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
  62. redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
  63. RedisSerializationContext
  64. .SerializationPair
  65. .fromSerializer(jackson2JsonRedisSerializer)
  66. ).entryTtl(Duration.ofSeconds(seconds));
  67. return redisCacheConfiguration;
  68. }
  69. }

参考 https://segmentfault.com/q/1010000015203664