Skip to content

Latest commit

 

History

History

redis-v2.0

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

自定义 Expired 注解

  • 自定义 Expired 注解,实现 redis 缓存的过期时间配置
  • 支持 Spring Expression Language (SpEL) ,实现动态配置过期时间。注意:使用 SpEL 配置过期时间时,配合@CacheputCacheEvict等使用时会有问题

环境

  • spring-boot 2.0.x
  • lettuce
  • redis server

使用方式

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @author YL
 */
@Service
public class UserService {
    /**
     * 指定具体过期时间
     */
    @Cacheable(
            value = "redis.v2.test",
            unless = "#result == null")
    @Expired(300)
    public User getUser(Long id, String firstName, String lastName) {
        return new User(id, firstName, lastName);
    }

    /**
     * SpEL 动态配置过期时间
     */
    @Cacheable(
            value = "redis.v2.test.ttl",
            unless = "#result == null")
    @Expired(spEl = "#ttl")
    public User getUser(Long id, String firstName, String lastName, long ttl) {
        return new User(id, firstName, lastName);
    }
}