在Ehcache中设置刷新策略
Ehcache 中是否有指定数据刷新策略?我目前正在将应用程序从 OSCache 迁移到 Ehcache,但我似乎找不到任何方法除了设置 timeToIdle
和 timeToLive
之外,还指定元素何时需要刷新。
我想要的是:在从缓存访问元素时,检查其关联资源以查看其更新时间是否晚于缓存元素的 lastUpdateTime
。如果是,则刷新缓存;否则提供缓存中的内容。
在 OSCache 中,这是通过捕获 NeedsRefreshException 并为元素设置自定义刷新策略来完成的。我已经在文档中挖掘了一段时间,但我无法找到任何方法或示例来说明如何在 Ehcache 中实现此目的。
任何帮助将不胜感激:)。
亚历克斯
Is there any to specify a data refresh policy in Ehcache? I am currently migrating an application from OSCache to Ehcache and I can't seem to find any way to specify when an element needs refreshing, besides setting timeToIdle
and timeToLive
.
What I want is: on accessing an element from the cache, check with it's associated resource to see if it was updated later than the lastUpdateTime
of the cache element. If yes, refresh the cache; else serve the content from the cache.
In OSCache this was done by catching NeedsRefreshException
s and setting custom refresh policies for the elements. I've been digging around in the docs for a while now, but I wasn't able to find any methods or examples of how I could accomplish this in Ehcache.
Any help would be appreciated :).
Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OSCache 的
NeedsRefreshException
想法确实有缺陷。首先,这违背了在通常执行流程中使用异常的建议(是的,我确实将过时的缓存命中视为正常流程);其次为此创建异常非常昂贵。现在回到你的问题。如果我处于您的立场,我会评估扩展
net.sf.ehcache.Element
的可能性,或者将您的条目值包装到一个能够检查timeToLive< 的对象中。 /代码>。
但主要问题是,为什么需要这样做?如果您将对象放入缓存并指定 TTL,为什么还要在检索时检查 TTL?缓存应该能够在没有任何外部帮助的情况下驱逐对象。这同样适用于手动删除条目的情况(例如,当对象已更改时)。也许我在这里遗漏了一些东西?
您所询问的功能称为通读。这里解释了如何执行此操作:http://ehcache.org/documentation/concepts。 html#read-through
--
另一个不高兴的 OSCache 用户正在等待迁移到 EHCache :(
The OSCache's idea of
NeedsRefreshException
is really flawed. First, this goes against the advice to use exceptions for usual execution flow (and yes, I do consider stale cache hit as a normal flow); secondly creating exception for this is really expensive.Now back to your question. If I was in your shoes, I would evaluate the possibility either to extend
net.sf.ehcache.Element
, or wrap your entry value into an object which would be able to check thetimeToLive
.But the main question is, why do you need to do that? If you are putting object into the cache and specifying the TTL, why bother checking the TTL on retrieval? The cache should be able to evict the object without any external help. Same applies for cases when you manually remove the entry (e.g. when the object has been changed). Maybe I am missing something here?
The feature you're asking about is called read-through. How to do this is explained here: http://ehcache.org/documentation/concepts.html#read-through
--
another unhappy OSCache user waiting to migrate to EHCache :(