dynamically configure redis cache ttl
<dependency>
<groupId>io.github.gravitymatrix</groupId>
<artifactId>redis-cache-ttl-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
@EnableSpringCacheRedisTtl
There are two ways to configure cache keys, Configuration files take precedence over annotations, if specify overrides as true in the configuration file, the value of the annotation is used
spring:
redis:
ttl:
//The default validity period of all keys
global: 60s
config:
// default false, if ture the value of the annotation overrides the current value
override: ture
//Cache Key Configuration
cache:
user: 30s
org: 30s
/**
* @author WangChen
* @since 2021-12-25 14:01
**/
@Service
public class MyService {
@Ttl("1h")
@Cacheable(cacheNames = "user", key = "#id")
public User getUser(String id){
return new User("user");
}
@Ttl("100s")
@Cacheable(cacheNames = "org", key = "#id")
public Org getOrg(String id){
return new Org("org");
}
}
Joda-time is used as the parsing library
d = day
h = hours
m = minutes
s = seconds
Use in configuration files or annotations, 1day or 1hours custom config