使用二级缓存时最佳的查询策略和实体配置是什么 [英] What is the best query strategy and entities configuration when using a second level cache

查看:134
本文介绍了使用二级缓存时最佳的查询策略和实体配置是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

激活二级缓存后,对于我的实体而言,最佳查询策略和最佳配置是什么?

What is the best query strategy and the best configuration for my entities when the second level cache is activated.

例如,我有两个具有以下关系的实体User和Group:

For example I have two entities User and Group with these relations :

一个用户到多个组:用户拥有的组.这里是User类中Group的集合和Group类中的User属性(所有者).

One User to Many Group : the groups owned by the user. Here a collection of Group in the User class and a User attribute (the owner) in the Group class.

具有属性(状态,日期等)的许多用户到许多组":组的成员.由于附加组件的存在,因此有一个特定的类可以映射称为UserGroup的中间表.在User类中有一个UserGroup的oneToMany集合,在Group类中有另一个.

Many User to Many Group with attributes (status, date etc.) : the members of the groups. Because of the additionnals attibutes there is a specific class to map the intermediate table called UserGroup. There is a oneToMany collection of UserGroup in the User class and another one in the Group class.

相关属性或集合被配置为以惰性模式检索.所有实体类和集合都是可缓存的.

The related attributes or collections are configured to be retrieved in Lazy mode. All the entities class and collections are cacheable.

到目前为止,如果我希望与他们的成员一起检索用户的网上论坛,我正在发出这种类型的请求:

So far if I wanted the user's groups to be retrieved with their member I was making a request of this type :

select g from Group g left join fetch UserGroup ug left join fetch ug.user u where g.group.owner = :idOwner

现在具有用作键值存储的第二级缓存,难道不是更容易有效地从ID中获取用户,然后让Hibernate在Lazy模式下恢复组和成员以最终保存数据吗?在第二级缓存中? 下一个查询将使用缓存,那么使用联接有什么好处? 另外,我不必配置要缓存的任何优化查询.

Now with the second level cache that works as a key-value store, isn't it easier and effective to get the user from his id and then let hibernate recover the groups and the members in Lazy mode to finally save the data in the second level cache ? The next query will use the cache, so what is the benefit of using a join ? Also I wouldn't have to configure any optimized queries to be cached.

谢谢.

推荐答案

我不能对连接的成本发表评论(Hibernate/JPA专家在那里可能会提供更好的答案),但是我可以发表评论缓存自身.

I can't really comment on the cost of joins (Hibernate/JPA experts out there will probably have better answers in that regard), but I can comment on caching itself.

真正重要的是缓存这些实体和集合的更新频率.如果您的查询跨越多个实体和集合,那么即使其中一个实体或集合被修改(即使以最小的可能方式),查询也会无效.因此,我强烈建议您缓存大多数阅读的实体和集合.这些无效性是按类型(btw)进行跟踪的,因此,即使添加新实体(不属于查询结果集的一部分)也会使查询无效.

What matters to caching is really how often these entities and collections are updated. If you have a query that spans multiple entities and collections, the moment one of those entities, or the collections are modified, even in the smallest possible way, the query is invalidated. So, I'd strongly recommend caching entities and collections that are read mostly. These invalidations are tracked per type btw, so even adding a new entity (which is not part of the query resultset) would invalidate the query.

即使不使用查询,如果仅检索实体和集合引用,则在修改集合中的实体或以某种方式修改集合(添加/删除实体)之后,集合也会失效.

Even if not using queries, if just retrieving an entity and a collection reference, the moment an entity in the collection is modified, or the collection is modified somehow (entity added/remove), then the collection gets invalidated.

我赞成选择性地启用缓存,而不是仅获取所有实体和集合并使它们立即可缓存.除了无效之外,您还需要考虑缓存占用了内存和资源.

I'd favour selectively enabling caching rather than just taking all entities and collections and making them cacheable right away. Besides invalidations, you also need to consider that caches take up memory and resources.

这篇关于使用二级缓存时最佳的查询策略和实体配置是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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