最牛别缓存,咖啡因
@Documented
public @interface Cacheable {
// 缓存名称 可以写多个~
@AliasFor("cacheNames")
String[] value() default {};
@AliasFor("value")
String[] cacheNames() default {};
// 支持写SpEL,切可以使用#root
String key() default "";
// Mutually exclusive:它和key属性互相排斥。请只使用一个
String keyGenerator() default "";
//要用于的自定义{@link.org.springframework.cache.CacheManager}的bean名称
//如果没有,则创建默认的{@link org.springframework.cache.interceptor.CacheResolver}
String cacheManager() default "";
//自定义{@link.org.springframework.cache.interceptor.CacheResolver}的bean名称使用
String cacheResolver() default "";
// SpEL,可以使用#root。 只有true时,才会作用在这个方法上
String condition() default "";
// 可以写SpEL #root,并且可以使用#result拿到方法返回值~~~
String unless() default "";
// true:表示强制同步执行。(若多个线程试图为**同一个键**加载值,以同步的方式来进行目标方法的调用)
// 同步的好处是:后一个线程会读取到前一个缓存的缓存数据,不用再查库了~~~
// 默认是false,不开启同步one by one的
// @since 4.3 注意是sync而不是Async
// 它的解析依赖于Spring4.3提供的Cache.get(Object key, Callable<T> valueLoader);方法
boolean sync() default false;
和上面一样的解释
public @interface CachePut {
@AliasFor("cacheNames")
String[] value() default {};
@AliasFor("value")
String[] cacheNames() default {};
String key() default "";
/**
* The bean name of the custom {@link org.springframework.cache.interceptor.KeyGenerator}
* to use.
* <p>Mutually exclusive with the {@link #key} attribute.
* @see CacheConfig#keyGenerator
*/
String keyGenerator() default "";
String cacheManager() default "";
/**
* The bean name of the custom {@link org.springframework.cache.interceptor.CacheResolver}
* to use.
* @see CacheConfig#cacheResolver
*/
String cacheResolver() default "";
String unless() default "";
}
public @interface CacheEvict {
@AliasFor("cacheNames")
String[] value() default {}; //缓存名称
@AliasFor("value")
String[] cacheNames() default {}; //缓存名称
String key() default ""; //通过key删除缓存结果
String keyGenerator() default ""; //key的生成规则
String cacheManager() default "";
String cacheResolver() default "";
String condition() default "";
boolean allEntries() default false; //清除所有缓存
boolean beforeInvocation() default false; //在方法执行之前删除缓存,默认是方法执行之后删除缓存
}
使用案例
// 添加缓存
@Cacheable(value = Constant.FIVE_SECOND,key ="#userId")
public Object getUser(long userId){
return null;
}
// 更新缓存
@CachePut(value =Constant.TEN_SECOND,key ="#userId")
public Object updateUser(long userId){
return null;
}
// 添删除缓存
@CacheEvict(value =Constant.THIRTY_SECOND,key ="#userId")
public Object removeUser(long userId){
return null;
}
咖啡因尽然是LRU,LFU的组合,乖乖,最近组少使用原则和不常用原则的组合怪咖
大佬介绍说明; https://mp.weixin.qq.com/s/uNVB1sgXYccSWHBdTdvL-A
本文来自博客园,作者:余生请多指教ANT,转载请注明原文链接:https://www.cnblogs.com/wangbiaohistory/p/17023613.html

浙公网安备 33010602011771号