【发布时间】:2017-10-08 01:46:08
【问题描述】:
我想知道是否只有在从特定方法调用时才能获取缓存数据,例如我有一个注释为@Cacheable 的方法,我希望它只返回特定方法调用它时缓存的内容,否则,正常执行该方法。
【问题讨论】:
标签: java spring caching spring-boot ehcache
我想知道是否只有在从特定方法调用时才能获取缓存数据,例如我有一个注释为@Cacheable 的方法,我希望它只返回特定方法调用它时缓存的内容,否则,正常执行该方法。
【问题讨论】:
标签: java spring caching spring-boot ehcache
使用 Spring 缓存抽象。 https://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html
@Cacheable({"users"})
public User getUser(String userId) {
// Logic goes here
}
有了这个,任何 Spring 都会缓存从方法返回的返回对象,方法输入作为键。所以这个方法被调用,如果找到,则从缓存中返回用户,否则调用实际方法,缓存并返回响应。
这可以由任何第三方缓存支持,例如 redis、ehcache、hazelcast。
【讨论】:
是的,您可以使用@Cacheable Annotation 的条件参数来做到这一点。在这里查看http://docs.spring.io/spring/docs/3.1.0.RC1/spring-framework-reference/html/cache.html 第 28.3.1.3 节
【讨论】: