休眠:使二级缓存失效 [英] Hibernate : Invalidating Second Level cache

查看:159
本文介绍了休眠:使二级缓存失效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如果在持久存储中发生任何更新,L2高速缓存不能自行更新。那么,我应该在DAO层的每个写入或更新方法之后调用evict2ndLevelCache()方法吗?或者有更好的策略?



http://howtodoinjava.com/2013/07/02/how-hibernate-second-level-cache-works/


如果某个用户或进程直接在数据库中进行更改,那么
无法让该二级高速缓存自动更新,直到该高速缓存区域的
timeToLiveSeconds持续时间结束。在这个
的情况下,使整个缓存失效并让hibernate
再次构建它的缓存是个好主意。您可以使用下面的代码片段来
使整个hibernate二级缓存失效。


解决方案

如果你正在修改Hibernate实体本身,你不需要做任何事情来确保二级缓存的一致性,Hibernate会照顾它的。



如果您通过本机查询进行更改,则明确提及哪些实体会受到影响,否则Hibernate将使整个二级缓存失效,如所述在这里

如果您要从另一个进程更改数据库中的数据,那么Hibernate并不知道它,您将不得不定义一个策略最适合您的要求(过期政策,从应用程序外部调用显式失效等)。


I understood that L2 cache can't update itself if any update happens in the persistent store. So, Should I call evict2ndLevelCache() method after every write or update methods at DAO layer? Or there is a better strategy?

http://howtodoinjava.com/2013/07/02/how-hibernate-second-level-cache-works/

If some user or process make changes directly in database, the there is no way that second level cache update itself until "timeToLiveSeconds" duration has passed for that cache region. In this case, it is good idea to invalidate whole cache and let hibernate build its cache once again. You can use below code snippet to invalidate whole hibernate second level cache.

解决方案

If you are doing changes in Hibernate entities itself, you don't have to do anything else to ensure the consistency of L2 cache, Hibernate will take care of it.

If you are doing changes via native queries, then explicitly mention which entities are affected, otherwise Hibernate will invalidate the entire second-level cache, as explained here.

If you are changing data in the database from another process, then Hibernate is not aware of it, and you will have to define a strategy that best suits your requirements (expiration policies, explicit invalidation called from the outside of the application, etc).

这篇关于休眠:使二级缓存失效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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