更新二级缓存而不是失效
Update 2nd Level Cache instead of Invalidating
我们将 Hibernate 3.5.6 与 Hazelcast 3.6.1 一起用作二级缓存。我们有以下问题。我们有一个带有非惰性集合的 Hibernate 实体。在同一节点上的两个连续会话中向集合添加元素时,总是从数据库重新加载整个集合。我本来希望二级缓存可以在将元素添加到集合中时更新,而不是将它从缓存中完全逐出并每次重新加载它以添加另一个条目。这是Hibernate二级缓存的原理性问题,还是我们这边的配置问题?
hibernate 缓存,默认情况下以 PK 为键,所以当你执行 findAll() 时,它不会使用 hibernate 缓存,当你执行 findOne(id) 时它会。
如果你想缓存 findAll() 我会使用 ehcache 和 @Cache annoations,注意它使用什么键作为缓存。参见 http://www.ehcache.org/documentation/2.8/get-started/key-classes-methods.html
我想我可以自己回答这个问题。事实上,Hibernate 不会 更新集合,但总是使它无效。我在这里找到了以下解释:http://planet.jboss.org/post/collection_caching_in_the_hibernate_second_level_cache:
What are the caching semantics? Well, the key one is that collections are never updated in the cache; they are only invalidated out of the cache and then potentially cached again later as the result of another database read. So, if an application called Group.addMember(), Hibernate will remove that group’s membership collection from the cache. If JBoss Cache is the second level cache implementation, that removal will be propagated around the cluster; the collection will be removed from the cache on all nodes in the cluster.
If later the application needs to access the members from that group, another database read will occur and the current set of primary keys for the members will be put into the cache.
我们将 Hibernate 3.5.6 与 Hazelcast 3.6.1 一起用作二级缓存。我们有以下问题。我们有一个带有非惰性集合的 Hibernate 实体。在同一节点上的两个连续会话中向集合添加元素时,总是从数据库重新加载整个集合。我本来希望二级缓存可以在将元素添加到集合中时更新,而不是将它从缓存中完全逐出并每次重新加载它以添加另一个条目。这是Hibernate二级缓存的原理性问题,还是我们这边的配置问题?
hibernate 缓存,默认情况下以 PK 为键,所以当你执行 findAll() 时,它不会使用 hibernate 缓存,当你执行 findOne(id) 时它会。
如果你想缓存 findAll() 我会使用 ehcache 和 @Cache annoations,注意它使用什么键作为缓存。参见 http://www.ehcache.org/documentation/2.8/get-started/key-classes-methods.html
我想我可以自己回答这个问题。事实上,Hibernate 不会 更新集合,但总是使它无效。我在这里找到了以下解释:http://planet.jboss.org/post/collection_caching_in_the_hibernate_second_level_cache:
What are the caching semantics? Well, the key one is that collections are never updated in the cache; they are only invalidated out of the cache and then potentially cached again later as the result of another database read. So, if an application called Group.addMember(), Hibernate will remove that group’s membership collection from the cache. If JBoss Cache is the second level cache implementation, that removal will be propagated around the cluster; the collection will be removed from the cache on all nodes in the cluster.
If later the application needs to access the members from that group, another database read will occur and the current set of primary keys for the members will be put into the cache.