我可以为@Cacheable 设置 TTL [英] Can I set a TTL for @Cacheable

查看:39
本文介绍了我可以为@Cacheable 设置 TTL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 Spring 3.1 的 @Cacheable 注释支持,想知道是否有任何方法可以通过设置 TTL 一段时间后清除缓存数据?现在,从我所见,我需要使用 @CacheEvict 自己清除它,并且通过将它与 @Scheduled 一起使用,我可以自己进行 TTL 实现,但是这么简单的任务似乎有点过分?

I am trying out the @Cacheable annotation support for Spring 3.1 and wondering if there is any way to make the cached data clear out after a time by setting a TTL? Right now from what I can see I need to clear it out myself by using the @CacheEvict, and by using that together with @Scheduled I can make a TTL implementation myself but it seems a bit much for such a simple task?

推荐答案

参见 http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#cache-specific-config:

如何设置 TTL/TTI/驱逐策略/XXX 功能?

How can I set the TTL/TTI/Eviction policy/XXX feature?

直接通过您的缓存提供程序.缓存抽象是...好吧,抽象而不是缓存实现

Directly through your cache provider. The cache abstraction is... well, an abstraction not a cache implementation

所以,如果你使用EHCache,使用EHCache的配置来配置TTL.

So, if you use EHCache, use EHCache's configuration to configure the TTL.

您也可以使用 Guava 的 CacheBuilder 构建缓存,并将该缓存的 ConcurrentMap 视图传递给 ConcurrentMapCacheFactoryBean 的 setStore 方法.

You could also use Guava's CacheBuilder to build a cache, and pass this cache's ConcurrentMap view to the setStore method of the ConcurrentMapCacheFactoryBean.

这篇关于我可以为@Cacheable 设置 TTL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆