开发者

Can I annotate a method as cacheable in Spring?

I would like to use an annotation that marked the result of a method call as cacheable. When provided it would use a caching provider to cache the output for the given input. For example:

@Cacheable
public Bar doExpen开发者_运维技巧siveCalculation(Foo foo) {
    Bar bar = jiggeryPokeryWith(foo);
    return bar;
}

...

Foo foo1 = new Foo(...);
Foo foo2 = new Foo(...);

Bar bar1 = doExpensiveCalculation(foo1);
Bar bar2 = doExpensiveCalculation(foo2);
Bar bar3 = doExpensiveCalculation(foo1);
// no calculation done on previous line, cached result == bar1

At the end of this example the cache would contain

{doExpensiveCalculation(foo1) -> bar1, 
 doExpensiveCalculation(foo2) -> bar2}

I am sure this is possible using AOP. As Spring does both AOP and caching, it seems it would be a natural fit for this use case.

Does such a feature exist?


This module has what you want. (But it is actually pretty straightforward to implement)


the google code is not required anymore as spring has it out of the box: http://static.springsource.org/spring/docs/3.1.0.M1/spring-framework-reference/html/cache.html#cache-store-configuration-ehcache

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜