简体   繁体   English

查询和二级缓存是否同步工作?

[英]Does query and second level cache works in synchronization?

i am using both second level cache and query cache. 我正在使用二级缓存和查询缓存。 Here is the code snippet 这是代码片段

//first block
session = factory.openSession();
company1=(Company)session.get(Company.class, 1);
session.close();

//second block
session = factory.openSession();
tx = session.beginTransaction();
Query updateQuery=session.createQuery("update Company set companyName = 'newCompany' where companyId=1");
updateQuery.setCacheable(true);
updateQuery.executeUpdate();
tx.commit();
session.close();

//Third block
session = factory.openSession();
company1=(Company)session.get(Company.class, 1); // line 1
session.close();

In second block i did update in query . 在第二个块我确实更新了查询。 In third block i am getting the company record thru second level cache. 在第三个块中,我通过二级缓存获得公司记录。 What i was expecting i will get the same result(in 3rd block) what i got in first block but i got the updated record (done by query update in 2nd block) ie "newCompany" at line 1 我期待的是,我将获得相同的结果(在第3块)我在第一个块中获得但是我得到了更新的记录(通过第二个块中的查询更新完成),即第1行的“newCompany”

So looks like query cache and second level cache are in synch with each other as update done by query cache is picked by second level cache. 因此查询缓存和二级缓存看起来相互同步,因为查询缓存完成的更新被二级缓存选中。

UPDATE:- So How does query and second level cahe works in synch? 更新: -那么查询和二级cahe如何同步工作? I mean does query cache first check under second level cache whether there has been any update for the given query parameter? 我的意思是查询缓存首先在二级缓存下检查是否有给定查询参数的任何更新?

The query cache stores IDs returned by previous executions of a cacheable select query. 查询缓存存储先前执行的可缓存选择查询返回的ID。

Let's say you execute the following cacheable query: 假设您执行以下可缓存查询:

select line from OrderLine line join line.order order 
where line.status = ? and order.date = ?

If you execute it once, Hibernate will store the IDs of the lines returned by the query in its query cache. 如果执行一次,Hibernate会将查询返回的行的ID存储在其查询缓存中。 And it will store the lines themselves in the second-level cache. 它会将行本身存储在二级缓存中。

If you execute the same query a second time, with the same parameters, Hibernate will extract the IDs from the query cache, without executing the select query. 如果第二次执行相同的查询,使用相同的参数,Hibernate将从查询缓存中提取ID,而不执行select查询。 Then it will get every line by ID (which should be fast since the lines are in the second-level cache) 然后它将按ID获取每一行(由于行在二级缓存中,因此应该很快)

If you insert, update or delete a line or an order, Hibernate will detect it. 如果您插入,更新或删除一行或一个订单,Hibernate将检测到它。 Since this modification could affect the result of the cached query, the cache entries associated with this query in the query cache will be evicted. 由于此修改可能会影响缓存查询的结果,因此将逐出与查询缓存中与此查询关联的缓存条目。 So the nexttime you execute this query again, it will be executed against the database, and the results will be stored again in the query cache. 因此,下次再次执行此查询时,将对数据库执行该查询,结果将再次存储在查询缓存中。

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

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