Second-level cache
First, the introduction
1. Hibernate-Provided cache
There is a first-level cache, a level two cache. The purpose is to reduce the number of accesses to the database and to improve the efficiency of program execution.
2, first-level cache:
1) session-based cache, cached content only valid at the current session, session closed, cache content is invalid.
2) Features:
A) The scope of the action is small. Cache time is short.
b) The cache effect is not obvious.
Ii. Overview
1, Level Two cache: Hibernate provides application-level caching that can access cached data across multiple sessions, that is, different sessions of the session. This swap is also called a level two cache.
2, Hibernate provides a level two cache with a default implementation, and is a pluggable caching framework. If the user wants to use level two cache, only need to configure in Hibernate.cfg.xml, do not want to use, directly remove, do not affect the code.
3, if the user feels hibernate provides the caching framework is not good, you can switch to other open source cache framework or implement the cache framework.
Third, use level two cache
1, view hibernate.properties configuration file, level two cache how to configure:
##########################
# # # Second-level Cache # # #
##########################
#hibernate. Cache.use _second_level_cache false "level two cache is not turned on by default and needs to be turned on manually, False->true"
#hibernate. Cache.use_query_cache true "turn on query caching "
# # Choose a cache implementation " Implementation of the two-level caching framework "
#hibernate. Cache.provider_class Org.hibernate.cache.EhCacheProvider
#hibernate. Cache.provider_class Org.hibernate.cache.EmptyCacheProvider
hibernate.cache.provider_class Org.hibernate.cache.HashtableCacheProvider default implementation
# Hibernate.cache.provider_class org.hibernate.cache.TreeCacheProvider
#hibernate. Cache.provider_class Org.hibernate.cache.OSCacheProvider
#hibernate. Cache.provider_class Org.hibernate.cache.SwarmCacheProvider
2, level Two cache use steps:
1) Turn on level two cache.
2) Specify the caching framework.
3) Specify those classes to join level two cache.
4) Test level two cache
A) Do not use level two cache:
b) Use level two cache:
Iv. Caching policies
1. Classification
The object that is placed in level two cache, the state is read-only;
<class-cache usage= "Read-only"/>
non-strict read and write, the efficiency will be higher <class-cache usage=
"Nonstrict-read-write"/>
objects that are placed in level two cache can be read and written;
<class-cache usage= "Read-write"/>
(transaction-based policy)
<class-cache usage= " Transactional "/>
2. Application:
1) Policy is read-only:
Operation Result:
2) Policy for Read and write: