spring redis 手动刷新缓存功能
背景
当我们在使用redis作为后台数据库缓存时,通常会有一套比较完善的更新策略,比如:按照某个固定时间过期。但是,在某些特定情况下(意料之外),后台数据已经更新,缓存中的数据需要手动同步。
思路
提取出刷新redis缓存的方法,注册在容器中。在需要的时候调用已注册的刷新方法,更新redis缓存。
实现
1. 注解
被注解的方法为刷新Redis的方法:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface RefreshCache {
String value() default "";
}
2. 容器
存储所有刷新缓存的方法:
@Data
@Slf4j
public class CacheInvoker {
private static List<CacheInvoker> invokers = new ArrayList<>();
private Object targetObj;
private Method targetMethod;
public CacheInvoker