开发者

Can I annotate a method as cacheable in Spring?

开发者 https://www.devze.com 2023-01-16 06:11 出处:网络
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:

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

精彩评论

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