Before tried it springboot integrated springcahce: https://www.cnblogs.com/a565810497/p/10931426.html
And try to set up an effective long springcahce with the Guava: https://www.cnblogs.com/a565810497/p/10932149.html
But eventually feel less flexible, because guava long just to set the default settings, you can not set more active, and springcahce not cached on the database, then redis very suitable for the collection together and springcahce
First, we want to use springcahce integrated redis first, refer to the article: https://blog.battcn.com/2018/05/13/springboot/v2-cache-redis/
Add dependent
Add rely redis in porm.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
Configuration Properties
## Redis server address spring.redis.host = 127.0.0.1 ## Redis server port spring.redis.port = 6379 ## Redis server connection password (blank by default) spring.redis.password = # is generally not configuration, Spring Cache based packet-dependent self assembly spring.cache.type = Redis # connection time (ms) spring.redis.timeout = 10000 # 16 has the Redis fragments default case, this particular configuration points used sheet spring.redis.database = 0 # connection pool maximum number of connections (negative values no limit) by default . 8 spring.redis.lettuce.pool.max -active =. 8 # latency connection pool maximum blocking (negative values indicating no limit) default -1 spring.redis.lettuce.pool.max -wait = -1 # connection pool default maximum idle connection . 8 spring.redis.lettuce.pool.max=. 8 -idle # minimum connection pool idle connections default 0 spring.redis.lettuce.pool.min -idle = 0
Then we can set redis based on the effective time of the springcache, refer to the article: https://blog.csdn.net/weixin_42047790/article/details/84189275
When startup class configuration effective length:
@Bean public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { return new RedisCacheManager( RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory), this.getRedisCacheConfigurationWithTtl(30*60), // 默认策略,未配置的 key 会使用这个 this.getRedisCacheConfigurationMap() // 指定 key 策略 ); } private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() { Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>(); //DayCache和SecondsCache进行过期时间配置 redisCacheConfigurationMap.put("DayCache", this.getRedisCacheConfigurationWithTtl(24*60*60)); redisCacheConfigurationMap.put("SecondsCache", this.getRedisCacheConfigurationWithTtl(2)); return redisCacheConfigurationMap; } private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) { Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig(); redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith( RedisSerializationContext .SerializationPair .fromSerializer(jackson2JsonRedisSerializer) ).entryTtl(Duration.ofSeconds(seconds)); return redisCacheConfiguration; } @Bean public KeyGenerator wiselyKeyGenerator() { return new KeyGenerator() { @Override public Object generate(Object target, Method method, Object... params) { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append("." + method.getName()); if(params==null||params.length==0||params[0]==null){ return null; } String join = String.join("&", Arrays.stream(params).map(Object::toString).collect(Collectors.toList())); String format = String.format("%s{%s}", sb.toString(), join); //log.info("缓存key:" + format); return format; } }; }
They were used at the same time not long cache:
@Override @Cacheable(value = "SecondsCache") public TestTime getTestTime(){ return new TestTime(new Date()); } @Override @Cacheable(value = "DayCache") public TestTime getTestTime1(){ return new TestTime(new Date()); }
Secondscache is to set up two seconds of the life cycle.
DayCache is set for a day of the life cycle.
Why so complicated settings does, in fact redis already set up an effective time to add caching sometimes, but we want to use annotations invasive way @cacheable do not need to implement the code annotations, so he combined redis and springcahce.
This article Source: https://gitee.com/Hiro-D/Java/tree/master/springcache-redis