Hibernate 的二级缓存是否可以用于 COUNT() 操作?
当使用 Hibernate 和 Ehcache 作为二级缓存 (2LC) 实现时,在使用 WHERE 子句执行 COUNT 操作时是否(或可以)使用此缓存?
用 SQL 术语来说,我正在执行的查询是 SELECT COUNT(id) FROM table WHERE someColumn > 100 。在某些情况下,每次传递的值都会不同,有时它总是相同。
我假设这超出了 2LC 的范围,而是需要“手动”管理(缓存查询结果,并在底层数据更改时使该缓存无效)。
When using Hibernate and Ehcache as a second-level cache (2LC) implementation, is (or can) this cache used when doing COUNT operations with a WHERE clause?
In SQL terms the query I'm performing is SELECT COUNT(id) FROM table WHERE someColumn > 100
. In some instances the value passed will be different each time, sometimes it will always be the same.
I'm assuming this is outside of the scope of 2LC, and instead would need to be managed 'manually' (cache the result of the query, and invalidate that cache whenever the underlying data change).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你是对的,这超出了二级缓存所能提供的范围。但是查询缓存将做这个伎俩。
基本上,Hibernate 会在名为
org.hibernate.cache.StandardQueryCache
的缓存中缓存命名参数(示例中的[100]
元组)和查询结果之间的映射。当对表
(更准确地说:查询中使用的任何表)进行任何更改时,它也会使缓存失效。每个表的最后修改时间存储在org.hibernate.cache.UpdateTimestampsCache
缓存中。另请参阅:
You are right, this is beyound what L2 cache offers. However query cache will do the trick.
Basically Hibernate will cache a mapping between named parameters (
[100]
tuple in your example) and a query result in a cache namedorg.hibernate.cache.StandardQueryCache
. It will also invalidate the cache when any changes are made totable
(more precisely: any table that is used in a query). The last modification time of each table is stored inorg.hibernate.cache.UpdateTimestampsCache
cache.See also: