Citation: http://www.blogjava.net/frostwood/archive/2010/01/06/308465.html
Hibernate provides three levels of cache policy: Session cache (Basic transaction level cache), query cache, Seond-level cache (level two cache)
Session cache (First-level cache): Session is the core mechanism that hibernate uses to manage persisted objects, which are transaction-level caches for persistent data. The Persistencecontext includes:
Entitykeys,collectionkeys,
Insertion,updates,deletions,
Collectioncreations,collectionremovals,collectionupdates
Thus, the session does not cache all persisted object entities themselves, but only the identiy values of the cached entities or collection, and the Entities or collection (including inserts, updates, deletions) that have been updated by the state.
When there is too much content cached in the session, it can cause outofmemory problems, and there are two ways to delete the cached content:
- Clear (): Clears all session caches;
- Evict (PersistentObject): Removes a specific persisted object from the session cache.
Query cache:hibernate can cache frequently made queries (same query, same parameter) for increased efficiency. But instead of caching the actual data entities in the result set, the query cache caches only the identiy and result value types, so it should always be used with the level two cache. However, because the exact same frequent queries are rare in the real world, the default cache is disabled. There are two ways to make it work: (Personally, this cache is not very meaningful)
Configuration properties: <prop key= "Hibernate.cache.use_query_cache" >true</prop>
Code: Query.setcacheable (True)
Second-level cache: Level two cache, which caches the actual data of a persisted object entity or collection, to provide a cluster level (cluster level), JVM, or file system level caching mechanism. Hibernate's level two caches are typically provided by third-party open source projects and can be configured to select specific cache implementations, such as Ehcache,oscache,jboss cable.
Configuration properties: <prop key= "Hibernate.cache.provider_class" >org.hibernate.cache.OSCacheProvider</prop>
A secondary cache can also be managed in two ways:
Configuration properties: <prop key= "Hibernate.cache.use_second_level_cache" >false</prop>
Code: Query.setcachemode (Cachemode.ignore) (or Get,normal,put,refresh)
- IGNORE: Disable Level Two cache
- Normal: Enable level two cache, read and write normally
- GET: Read only from level two cache, unless there is data update
- Put: Write to level two cache only
- Refresh: Force a flush of content written to level two cache
Finally, in program debugging, we may need to look at what is actually cached in the various caches and allow hibernate to collect cache statistics through the configuration properties. This approach is especially useful when we encounter problems that might be caused by caching.
Configuration properties: <prop key= "Hibernate.generate_statistics" >true</prop>
Get statistics in your code: Sessionfactory.getstatistics ()
Understanding and application of Hibernate cache strategy