问题描述
使用Hibernate和Ehcache作为二级缓存(2LC)实现时,使用WHERE子句进行COUNT操作时可以使用此缓存吗?在SQL中,我正在执行的查询是 SELECT COUNT(id)FROM table WHERE someColumn> 100
。在某些情况下,每次传递的值都会有所不同,有时总是相同。
我假设这不在2LC的范围内,而是需要手动管理(缓存查询结果,并在基础数据发生变化时使该缓存失效)。 解决方案
在SQL中,我正在执行的查询是 SELECT COUNT(id)FROM table WHERE someColumn> 100
。在某些情况下,每次传递的值都会有所不同,有时总是相同。
我假设这不在2LC的范围内,而是需要手动管理(缓存查询结果,并在基础数据发生变化时使该缓存失效)。 解决方案
你说得对,这是L2缓存提供的东西。然而将会基本上Hibernate会缓存命名参数之间的映射(在你的例子中 [100]
>元组)并在名为 org.hibernate.cache.StandardQueryCache
的缓存中查询结果。当对 table
(更准确地说:查询中使用的任何表)进行任何更改时,它也会使缓存无效。每个表的最后修改时间存储在 org.hibernate.cache.UpdateTimestampsCache
缓存中。
另请参阅:
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).
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 named org.hibernate.cache.StandardQueryCache
. It will also invalidate the cache when any changes are made to table
(more precisely: any table that is used in a query). The last modification time of each table is stored in org.hibernate.cache.UpdateTimestampsCache
cache.
See also:
这篇关于Hibernate的二级缓存是否可用于COUNT()操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!