当前位置:   article > 正文

springboot2.x中redis配置 @Cacheable注解增加默认过期时间30分钟_@cacheable 默认过期时间

@cacheable 默认过期时间

配置文件

  1. # REDIS (RedisProperties)
  2. # Redis数据库索引(默认为0
  3. spring.redis.database=16
  4. # Redis服务器地址
  5. spring.redis.host=127.0.0.1
  6. # Redis服务器连接端口
  7. spring.redis.port=6379
  8. # Redis服务器连接密码(默认为空)
  9. spring.redis.password=
  10. # 连接池最大连接数(使用负值表示没有限制)
  11. spring.redis.lettuce.pool.max-active=8
  12. # 连接池最大阻塞等待时间(使用负值表示没有限制)
  13. spring.redis.lettuce.pool.max-wait=-1ms
  14. # 连接池中的最大空闲连接
  15. spring.redis.lettuce.pool.max-idle=8
  16. # 连接池中的最小空闲连接
  17. spring.redis.lettuce.pool.min-idle=0
  18. # 连接超时时间(毫秒)
  19. spring.redis.timeout=0ms

config

  1. package com.zhy.db.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.beans.factory.annotation.Value;
  6. import org.springframework.cache.CacheManager;
  7. import org.springframework.cache.annotation.*;
  8. import org.springframework.cache.interceptor.KeyGenerator;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. import org.springframework.data.redis.cache.RedisCacheConfiguration;
  12. import org.springframework.data.redis.cache.RedisCacheManager;
  13. import org.springframework.data.redis.cache.RedisCacheWriter;
  14. import org.springframework.data.redis.connection.RedisConnectionFactory;
  15. import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
  16. import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
  17. import org.springframework.data.redis.core.RedisTemplate;
  18. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  19. import org.springframework.data.redis.serializer.RedisSerializationContext;
  20. import org.springframework.data.redis.serializer.RedisSerializer;
  21. import org.springframework.data.redis.serializer.StringRedisSerializer;
  22. import org.springframework.transaction.annotation.EnableTransactionManagement;
  23. import java.time.Duration;
  24. /**
  25. * redis缓存配置
  26. *
  27. * @author zhy
  28. */
  29. @Configuration
  30. @EnableTransactionManagement
  31. @CacheConfig
  32. @EnableCaching
  33. public class RedisConfig {
  34. @Value("${spring.redis.host}")
  35. private String host;
  36. @Value("${spring.redis.port}")
  37. private Integer port;
  38. @Value("${spring.redis.password}")
  39. private String password;
  40. /**
  41. * redis连接工厂
  42. *
  43. * @return LettuceConnectionFactory
  44. */
  45. @Bean
  46. public LettuceConnectionFactory redisConnectionFactory() {
  47. return new LettuceConnectionFactory(new RedisStandaloneConfiguration(host, port));
  48. }
  49. @Bean
  50. public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
  51. RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>();
  52. redisTemplate.setConnectionFactory(factory);
  53. RedisSerializer redisSerializer = new StringRedisSerializer();
  54. redisTemplate.setKeySerializer(redisSerializer);
  55. redisTemplate.setHashKeySerializer(redisSerializer);
  56. Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = createJacksonRedisSerializer();
  57. redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
  58. redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
  59. redisTemplate.afterPropertiesSet();
  60. return redisTemplate;
  61. }
  62. /**
  63. * 缓存管理器
  64. */
  65. @Bean
  66. public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
  67. RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(
  68. Duration.ofMinutes(30)).serializeKeysWith(RedisSerializationContext.SerializationPair
  69. .fromSerializer(new StringRedisSerializer())).serializeValuesWith(RedisSerializationContext
  70. .SerializationPair.fromSerializer(createJacksonRedisSerializer())).disableCachingNullValues();
  71. RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
  72. return new RedisCacheManager(redisCacheWriter, config);
  73. }
  74. private Jackson2JsonRedisSerializer<Object> createJacksonRedisSerializer() {
  75. Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
  76. ObjectMapper om = new ObjectMapper();
  77. om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  78. om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  79. jackson2JsonRedisSerializer.setObjectMapper(om);
  80. return jackson2JsonRedisSerializer;
  81. }
  82. @Bean
  83. public KeyGenerator keyGenerator() {
  84. return (target, method, params) -> {
  85. StringBuilder sb = new StringBuilder();
  86. String[] value = new String[1];
  87. Cacheable cacheable = method.getAnnotation(Cacheable.class);
  88. if (cacheable != null) {
  89. value = cacheable.value();
  90. }
  91. CachePut cachePut = method.getAnnotation(CachePut.class);
  92. if (cachePut != null) {
  93. value = cachePut.value();
  94. }
  95. CacheEvict cacheEvict = method.getAnnotation(CacheEvict.class);
  96. if (cacheEvict != null) {
  97. value = cacheEvict.value();
  98. }
  99. sb.append(value[0]);
  100. for (Object obj : params) {
  101. sb.append(":").append(obj.toString());
  102. }
  103. return sb.toString();
  104. };
  105. }
  106. }
@Cacheable(value = "signIn", key = "#root.methodName +':'+ T(java.time.LocalDate).now()+':'+ #username")
@EnableCaching
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/353161
推荐阅读
相关标签
  

闽ICP备14008679号