简体   繁体   English

Hibernate何时从二级缓存读取,何时从DB读取?

[英]When does Hibernate read from second-level cache and when from DB?

As far as I know Hibernate let's you configure entities and collections to be stored in a second-level cache. 据我所知,Hibernate让您配置要存储在二级缓存中的实体和集合。

When does Hibernate try to read these cached entities from the second-level cache and when does it hit the DB? Hibernate什么时候尝试从第二级缓存中读取这些缓存的实体,何时将其命中数据库? Does Hibernate only read from the second-level cache when loading entities by calling Sesssion.get() and when initializing proxies (including collections)? 当通过调用Sesssion.get()加载实体以及初始化代理(包括集合)时,Hibernate是否仅从二级缓存读取? Does Hibernate ever hit the second-level cache when executing HQL- or Criteria-Queries? 执行HQL或Criteria查询时,Hibernate是否曾经命中二级缓存?

Examples? 例子?

2nd level cache contains only entities by their ids, so when retrieving an entity by id (ie get, load or resolving a proxy implicitly) a 2nd level cache may be accessed. 2级缓存仅包含按其ID分配的实体,因此,按ID检索实体(即隐式获取,加载或解析代理)时,可以访问2级缓存。 Any other queries (hal, criteria) will bypass the cache and hit the DB - at least as long as no query cache is used as well. 任何其他查询(hal,条件)将绕过缓存并访问数据库-至少在不使用查询缓存的情况下。

(Note: the easiest way to answer that type of questions is to turn show_sql on and see what queries Hib generates.) (注意:回答此类问题的最简单方法是打开show_sql并查看Hib生成的查询。)

Sometimes query only return PKs of the records (eg for iteration queries) and then Hib can use the cache. 有时查询仅返回记录的PK(例如,用于迭代查询),然后Hib可以使用缓存。

When retrieving linked objects cache can be used too. 检索链接的对象时,也可以使用缓存。

I cannot though give you the precise rule here. 我不能在这里给你确切的规则。 I also suspect the answer depends on capabilities of dialect used. 我也怀疑答案取决于所用方言的功能。

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

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