简体   繁体   English

Ehcache和休眠

[英]Ehcache and Hibernate

I have a scenario where I am displaying the data in the database which changes frequently (changed by outside application) on a webpage using Spring MVC, somewhat similar to a stock monitoring application. 我有一种情况,我正在显示数据库中的数据,该数据使用Spring MVC在网页上经常更改(由外部应用程序更改),有点类似于库存监视应用程序。 Currently i am using a daemon thread which fires on web container startup and queries the database every 45 secs and stores the data in the application wide hashmap object. 目前,我正在使用一个守护程序线程,该线程在Web容器启动时启动,每45秒查询一次数据库并将数据存储在应用程序范围的hashmap对象中。 And the web application reads the data from hashmap (instead of database) for displaying the data. 然后,Web应用程序从哈希图(而不是数据库)读取数据以显示数据。

I have read about third party caching API's like Ehcache and OSCache . 我已经阅读了有关第三方缓存API的信息,例如EhcacheOSCache I have read the documentation on Ehcache and seems like I can use the Hibernate query caching technique instead of a daemon thread. 我已经阅读了有关Ehcache的文档,似乎可以使用Hibernate查询缓存技术代替守护线程了。

Now my query if I use hibernate and enable query caching and set timetoidle to 45 secs will the data in the cache is automatically refreshed to reflect latest data in the database or do i need force refresh (query the database again and repopulate the cache) the cache, also can you explain what a self populating cache is. 现在我的查询(如果我使用休眠模式并启用查询缓存并将timetoidle设置为45秒)将自动刷新缓存中的数据以反映数据库中的最新数据,还是我需要强制刷新(再次查询数据库并重新填充缓存)?缓存,您还能解释什么是自填充缓存。

In the Ehcache docs a SelfPopulatingCache is described as a: Ehcache文档中SelfPopulatingCache被描述为:

A selfpopulating decorator for Ehcache that creates entries on demand. Ehcache的自填充装饰器,可按需创建条目。

That means when asking the SelfPopulatingCache for a value and that value is not in the cache, it will create this value for you. 这意味着当向SelfPopulatingCache请求一个值并且该值不在缓存中时,它将为您创建该值。 This blog article gives a lot of details and also code (inclusively auto-updating). 这篇博客文章提供了很多细节和代码(包括自动更新)。

For me, it sounds that a Ehcache SelfPopulatingCache is what would fit your needs best. 对我来说,听起来最适合您的需求是Ehcache SelfPopulatingCache So I'd recommend to have a closer look at. 因此,我建议您仔细看看。

A Hibernate 2nd level cache would surely help to increase system performance, but not solve your problem, as I understand it. 据我所知,Hibernate 2级缓存肯定会帮助提高系统性能,但不能解决您的问题。 It's true when using Ehcache and setting timeToIdleSeconds the cache expires after that time, but it's not refreshed automatically. 使用Ehcache并设置timeToIdleSeconds时确实如此,缓存在该时间之后过期,但不会自动刷新。

Take a look at what Hibernate docs write about query cache : 看看Hibernate文档写的关于查询缓存的内容

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). 因此,对于那些预期作为查询结果缓存的一部分进行缓存的实体,查询缓存应该始终与二级缓存结合使用(就像集合缓存一样)。

Finally, OSCache is outdated. 最后,OSCache已过时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM