Cacheable

CachePut

CacheEvict

参数

value  

缓存的名称,必须指定至少一个。例如:

Cacheable(value=”mycache”)或者

Cacheable(value={”cache1”,”cache2”}

key  

缓存的key,可为空,按照SpEL表达式编写。例如:

Cacheable(value=”testcache”,key=”#id”)

keyGenrator

key的生成策略,默认情况下将参数值作为键,但是可能会导致key重复出现,因此一般需要自定义key的生成策略。

condition  

缓存的条件,可为空,使用SpEL编写,bool。例如:

Cacheable(value=”testcache”,condition=”#userName.length()2”)

unless  

否定缓存。当条件结果为TRUE时,就不会缓存。

Cacheable(value=”testcache”,unless=”#name.length()2”)

allEntries  

是否清空所有缓存,缺省为false,如果true,则方法调用后将立即清空所有缓存。例如:

CachEvict(value=”testcache”,allEntries=true)

sync

Spring4.3新增。当设置它为true时,只有一个线程的请求会去到数据库,其他线程都会等待直到缓存可用。这个设置可以减少对数据库的瞬间并发访问。

SpEL上下文数据

摘自Spring官方文档:

methodName  

root对象,当前被调用的方法名#root.methodname

method  

root对象,当前被调用的方法#root.method.name

target  

root对象,当前被调用的目标对象实例#root.target

targetClass  

root对象,当前被调用的目标对象的类#root.targetClass

args  

root对象,当前被调用的方法的参数列表#root.args[0]

caches  

root对象,当前方法调用使用的缓存列表#root.caches[0].name

ArgumentName  

执行上下文,当前被调用的方法的参数,如findUser(Useruser),可以通过#user.id获得参数id

result  

执行上下文,方法执行后的返回值,#result

例子:

#id为参数

Cacheable(value="users",key="#id")#p0为参数索引,第一个参数

Cacheable(value="users",key="#p0")#条件

Cacheable(cacheNames="demo",condition="#name.length()32")#条件

Cacheable(cacheNames="demo",unless="#result.id.contains(1)")#条件

CachePut(value="demo",key="#result==null")

.

.

预览时标签不可点收录于合集#个上一篇下一篇


转载请注明地址:http://www.luofumua.com/lfmcz/9624.html