Hibernate Cache
1. First level Cache--Session.
Querying of same object multiple times within a same session will only call DB for once, the rest of times will use cache to retrieve.
Same to openSession()&getCurrentSession()
2. Second level Cache--SessionFactory.
Data that is good for second level cache:
1.limited set of data;
2. Rarely being modified;
3. Won't be access by multiple objects at the same time
Example:
user authorization info. User entries are limited. load()&iterative() use secondary cache; list uses second level cache,however, query doesn't!!
When hibernate accesses data, it first check first level cache, then check second level cache if it's configured, then check DB.
Configuration:
1.add ehcache.xml
2.
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class"> org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
3. @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) ----> use default cache
For query checking using second level:
<property name="cache.use_query_cache">true</property>
xx.createQuery().setCachable(true).list
优质内容筛选与推荐>>