【问题标题】:Can I annotate a method as cacheable in Spring?我可以在 Spring 中将方法注释为可缓存的吗?
【发布时间】:2011-04-12 02:44:08
【问题描述】:

我想使用一个注释,将方法调用的结果标记为可缓存。提供时,它将使用缓存提供程序来缓存给定输入的输出。例如:

@Cacheable
public Bar doExpensiveCalculation(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

在这个例子的最后,缓存将包含

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

我确信使用 AOP 可以做到这一点。由于 Spring 同时进行 AOP 和缓存,因此它似乎很适合这个用例。

这样的功能存在吗?

【问题讨论】:

    标签: java spring caching aop


    【解决方案1】:

    This module 有你想要的。 (但实际上实现起来非常简单)

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 2018-12-12
      • 2012-08-04
      • 2012-12-31
      • 1970-01-01
      • 2016-10-17
      • 2018-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多