问题
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).
回答1:
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:
- Caching with Hibernate + Spring - some Questions!
来源:https://stackoverflow.com/questions/8712066/is-can-hibernates-second-level-cache-be-used-for-count-operations