本文介绍了我可以在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 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

在此示例的末尾,缓存将包含

At the end of this example the cache would contain

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

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

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.

是否存在这样的功能?

推荐答案

此模块具有您想要的. (但实际上实施起来非常简单)

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

这篇关于我可以在Spring中将方法注释为可缓存的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 13:18
查看更多