qclucky7 / redis-cache-ttl-spring-boot-starter

dynamically configure the expiration time of different keys

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

redis-cache-ttl-spring-boot-starter

dynamically configure redis cache ttl

中文文档

example

1. Pom file

<dependency>
    <groupId>io.github.gravitymatrix</groupId>
    <artifactId>redis-cache-ttl-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>

2. Add annotations to the startup class

@EnableSpringCacheRedisTtl

3. Config

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

1. Write to the configuration file
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
2. Using annotations
/**
 * @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");
    }
}

4. How to use period

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

About

dynamically configure the expiration time of different keys

License:Apache License 2.0


Languages

Language:Java 100.0%