java - Ehcache 和 Hibernate

标签 java ehcache

我有一个场景,我使用 Spring MVC 在网页上显示数据库中经常更改(由外部应用程序更改)的数据,有点类似于股票监控应用程序。目前我正在使用一个守护线程,它在 Web 容器启动时触发,每 45 秒查询一次数据库,并将数据存储在应用程序范围的 HashMap 对象中。 Web应用程序从hashmap(而不是数据库)读取数据来显示数据。

我读过有关第三方缓存 API 的内容,例如 EhcacheOSCache。我已经阅读了 Ehcache 的文档,似乎我可以使用 Hibernate 查询缓存技术而不是守护线程。

现在我的查询是,如果我使用 hibernate 并启用查询缓存并将 timetoidle 设置为 45 秒,缓存中的数据会自动刷新以反射(reflect)数据库中的最新数据,还是需要强制刷新(再次查询数据库并重新填充缓存)缓存,您还可以解释一下什么是自填充缓存。

最佳答案

Ehcache docs SelfPopulatedCache 被描述为:

A selfpopulating decorator for Ehcache that creates entries on demand.

这意味着当向 SelfPopulatedCache 请求一个值并且该值不在缓存中时,它将为您创建该值。 This blog article提供了很多细节和代码(包括自动更新)。

对我来说,听起来 Ehcache SelfPopulatedCache 最适合您的需求。所以我建议仔细看看。

据我了解,Hibernate 二级缓存肯定有助于提高系统性能,但不能解决您的问题。当使用 Ehcache 并设置 timeToIdleSeconds 时,缓存会在该时间后过期,但不会自动刷新。

看看什么 Hibernate docs write about query cache :

The query cache does not cache the state of the actual entities in the cache; it caches only identifier values and results of value type. For this reason, the query cache should always be used in conjunction with the second-level cache for those entities expected to be cached as part of a query result cache (just as with collection caching).

最后,OSCache 已经过时了。

关于java - Ehcache 和 Hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7821877/

相关文章:

java - 以线程安全的方式重置Ehcache

java - 如何出于测试目的关闭 ehcache

java - intellij 在我的 Windows 笔记本电脑和 Macbook 之间共享工作区

Java:更改二进制数(如 1010)并反转位。 1010 = 0101。0's to 1' 和 1's to 0'

java - 在没有 GUI 的情况下在退出的 Java 程序中设置热键

java - 通过用户输入键盘Java从mysql数据库中选择记录

java - ehcache 或 Spring MVC 的 Spring 缓存中的最佳缓存实践是什么?

java - Ehcache 与 Spring + Jersey

java - ehcache中是否必须有cache.xml?

Java日历: Subtract two Dates to get difference in days between the two