hibernate如何确保使用数据库中的最新数据更新二级缓存 [英] How hibernate ensures second level cache is updated with latest data in database

查看:470
本文介绍了hibernate如何确保使用数据库中的最新数据更新二级缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过使用hibernate的二级缓存,它可以通过减少数据库/对象检索的数据库命中来提高应用程序的性能。

然而,hibernate如何确保二级缓存与数据库中的数据保持同步。



例如:

假设

  @Entity 
class用户{
Id
private int id;
private String str;

$ / code>

现在,如果我们启用了二级缓存,我知道如果我们打开如果数据库中的数据得到了变化(例如,对于id为1的行),说:不同的会话,然后每个会话将打到第二级缓存,通过一些独立的进程/手动修改值,我们尝试访问该值,hibernate如何检测缓存是否具有最新值(对于id = 1)。



一般来说,hibernate如何确保二级缓存中的数据与数据库值一致。



感谢您的帮助。


< Hibernate自己管理缓存,所以当你更新某个实体通过hibernate Session时,它会 invalidate 缓存项与此实体相关联 - 所以缓存总是新鲜的。



如果另一个进程(甚至运行相同的hibernate应用程序的第二个JVM)更新在第一个JVM上的Hibernate没有意识到这个事实,并且在他的缓存中有 stale 对象。

然而,你可以使用任何缓存实现(缓存提供者)你想要的。有许多生产就绪的缓存提供程序允许您配置给定实体将存储在缓存中的时间。例如,您可以配置您的缓存,在30秒后对所有实体进行无效等等。



如果您使用EhCache缓存提供程序,则可以提供此类配置:

 < cache name =com.my.company.Entity
maxElementsInMemory =1000
eternal =false
timeToIdleSeconds =7200
timeToLiveSeconds =7200
overflowToDisk =false
memoryStoreEvictionPolicy =LRU/>

您可以在这里找到更多有关L2缓存的信息:
http://www.tutorialspoint.com/hibernate/hibernate_caching.htm



但是有很多有用的教程。


I have read that using hibernate's second level cache, it can improve applications performance by having less database hits for data / object retrieval.

However, how does hibernate ensure that second level cache is up to date with the data in database.

For, example:

Suppose the below class is entity and persisted into the DB.

@Entity
class User {
    Id
    private int id;
    private String str;
}

Now, if we have enabled second level cache, I understand that if we open different sessions then each session will hit the second level cache for retrieving object value.

Now, if data in database gets changes (for e.g. for row with id=1) say by some independent process/ manually changing the values, and we try to access the value, how does hibernate detect that the cache is having latest value (for id = 1).

In general, how does hibernate ensure that data in second level cache is consistent with the db values.

Thanks for your help.

解决方案

Hibernate manages the cache himself, so when you update some entity thru hibernate Session it will invalidate cache entry assocciated with this entity - so cache is always fresh.

If another process (or even second JVM running the same hibernate application) updates record in database, Hibernate on first JVM is unaware of this fact and has stale object in his cache.

However you can use any cache implementation (cache provider) you want. There are many production-ready cache providers that allow you to configure how long given entity will be stored in cache. For example you can configure your cache to invalide all entities after 30 seconds and so on.

If you use EhCache cache provider you can provide such configuration:

<cache name="com.my.company.Entity" 
   maxElementsInMemory="1000" 
   eternal="false" 
   timeToIdleSeconds="7200" 
   timeToLiveSeconds="7200" 
   overflowToDisk="false" 
   memoryStoreEvictionPolicy="LRU"/>

You can find more information abount L2 cache here: http://www.tutorialspoint.com/hibernate/hibernate_caching.htm

however there is a lot of useful tutorials about this.

这篇关于hibernate如何确保使用数据库中的最新数据更新二级缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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