我无法在Ehcache文档的任何位置找到它。如果我的Ehcache配置了TTL,是否需要检查检索到的元素是否已过期?
现在,我使用这个代码来创建和配置我Cache
:
// Groovy syntax
def cacheConfig = new CacheConfiguration('stats', 1)
cacheConfig.timeToLiveSeconds = 2
def cache = new Cache(cacheConfig)
cache.initialise()
这检索数据:
// Groovy syntax
def cachedElement = cache.get('stats')
if (cachedElement != null && ! cachedElement.isExpired()) {
// use the cached data
} else {
// get/generate the data and cache it
}
return cachedElement.value
我前一段时间写了这个,但现在看必须检查Element.isExpired()
似乎有点愚蠢 - 这不应该是必要的,对吧?我的意思是,如果一个元素过期,那么缓存不应该返回 - 对吗?
所以我想我可以删除检查 - 只是希望在这里快速的理智检查。
谢谢!
谢谢!我想投这个答案,但它确实需要一个链接。 – 2011-04-03 04:07:31