简体   繁体   中英

Hibernate cache for scalar queries

I have read this post. I cannot get whole objects as suggested in this post. I am getting MIN,MAX,Count values. So a new post. Below is what I want and what I have.

  1. My Spring app does not make any changes to DB(MySQL). It just reads the data. It will not be aware of the changes done to the existing data in DB.
  2. All my queries return scalar data. Not whole objects. (Ex: Select employee.name,office.address,MIN(employee.rec) from ... where.. )

  3. I want my queries to be cached. So I enabled query cache in hibernate configuration.And added cacheable="true" to my named queries.

4.I enabled second level cache.

5.Added the below annotation for the persistence class.

@Cacheable 
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)

6. I want to refresh the cache every 24 hours. So updated timeToLiveSeconds in ehcache.xml defaultCache .

Is this the correct approach? How to know whether the query hits second level cache from query cache?

UPDATE: I added statistics and there was no hit/miss on second level cache.

Turn on SQL logging and statistics generation. If you see that SQL queries logging, that means cache doesn't work.

<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.generate_statistics" value="true"/>

In order to see the hit count on second level cache:

session.getSessionFactory().getStatistics().getSecondLevelCacheHitCount()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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