简体   繁体   English

使用二级缓存时最好的查询策略和实体配置是什么

[英]What is the best query strategy and entities configuration when using a second level cache

What is the best query strategy and the best configuration for my entities when the second level cache is activated.激活二级缓存时,我的实体的最佳查询策略和最佳配置是什么。

For example I have two entities User and Group with these relations:例如,我有两个具有这些关系的实体用户和组:

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.这里是用户 class 中的组集合和组 class 中的用户属性(所有者)。

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.由于附加的属性,有一个特定的 class 到 map 中间表称为 UserGroup。 There is a oneToMany collection of UserGroup in the User class and another one in the Group class.用户 class 和组 class 中有一个用户组的 oneToMany 集合。

The related attributes or collections are configured to be retrieved in Lazy mode.相关属性或 collections 配置为在惰性模式下检索。 All the entities class and collections are cacheable.所有实体 class 和 collections 都是可缓存的。

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

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?现在有了二级缓存作为key-value存储,从用户的id中获取用户,然后让hibernate在Lazy模式下恢复组和成员,最终在二级保存数据不是更简单有效吗?一级缓存? 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.此外,我不必配置任何要缓存的优化查询。

Thanks.谢谢。

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.我不能真正评论连接的成本(那里的 Hibernate/JPA 专家可能会在这方面有更好的答案),但我可以评论缓存本身。

What matters to caching is really how often these entities and collections are updated.对缓存重要的是这些实体和 collections 的更新频率。 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.如果您有一个跨越多个实体和 collections 的查询,那么当其中一个实体或 collections 被修改时,即使以最小的方式修改,查询也会失效。 So, I'd strongly recommend caching entities and collections that are read mostly.所以,我强烈推荐缓存实体和 collections 主要被读取。 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.我倾向于有选择地启用缓存,而不是仅仅获取所有实体和 collections 并立即使它们可缓存。 Besides invalidations, you also need to consider that caches take up memory and resources.除了失效,您还需要考虑缓存占用 memory 和资源。

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

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