没有参数的Spring 3.1缓存抽象 [英] Spring 3.1 Cache Abstraction without parameters

查看:96
本文介绍了没有参数的Spring 3.1缓存抽象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring 3.1中了解新的缓存抽象,我想将此功能应用于我的项目.

reading about the new Cache Abstraction in Spring 3.1, I wanted to apply this feature to my project.

我可以缓存对没有参数的方法的调用吗?

Can I cache the call to a method that has no parameters?

@Cacheable("xCache")
public List<X> loadAllX() {
    ...
}

链接的博客文章指出

使用方法参数作为键执行缓存查找

a cache lookup is performed using as key the method parameters

所以应该不可能缓存此方法,对吧?

so it should not be possible to cache this method, right?

简短的回答:是的,没有任何参数的方法将像其他方法一样被缓存.我想该方法在缓存中将只有一个条目.

Short answer: Yes, methods without any arguments will get cached just like any other methods. I guess there will be exactly one entry in the cache for that method.

推荐答案

您可以使用" Cache SpEL可用元数据"来覆盖此行为,如下所述:

You can override this behavior by using "Cache SpEL available metadata" as described here:

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/cache.html#cache-spel-context

在您的示例中,您可以指定以下内容:

In your example, you could specify the following:

@Cacheable(value = "xCache", key = "#root.methodName") 
public List<X> loadAllX() { 
    ... 
} 

哪些键"loadAllX"将X的列表缓存在"xCache"中

Which would cache the list of X in the "xCache" with key "loadAllX"

这篇关于没有参数的Spring 3.1缓存抽象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆