Level 1 cache:
Session cache. When the program calls the SAVE (), upadte (), saveorupdate (), get (), load (), and list () that calls the query interface (), iterator (), filter (). If the object does not exist in the session cache, Hibernate will add the object to the first-level cache,
In addition, the session interface provides two methods:
Evict (Object OBJ): clears the persistence object specified in the cache.
Clear (): clears all cached objects.
Secondary cache:
It is a pluggable cache plug-in managed by sessionfactory, but note that concurrency problems may occur.
Usage:
1. Rarely modified data
2. Not very important data, allowing occasional Concurrent Data
3. data that will not be accessed concurrently
4. Limited instances are referenced in many places
Cache plug-ins:
Cache plug-in |
Hibernate implementation class |
Save type |
Cluster |
Query |
Ehcach |
Org. hibernate. cache. ehcachprovider |
Memory, Hard Disk |
N |
Y |
Oscach |
Org. hibernate. cache. oscachprovider |
Memory, Hard Disk |
N |
Y |
Swarmcach |
Org. hibernate. cache. swarmcacheprovider |
Cluster |
Y |
N |
JBoss treecach |
Org. hibernate. cache. treecacheprovider |
Cluster |
Y |
Y |
Cache plug-in |
Read-Only Type |
Non-strict read/write type |
Read/write type |
Transaction type |
Ehcach |
Y |
N |
Y |
N |
Oscach |
Y |
N |
Y |
N |
Swarmcach |
Y |
Y |
N |
N |
JBoss treecach |
Y |
N |
N |
Y |
Second-level Cache Usage:
1. Set cache granularity and concurrent Access Policy
Hibernate allows you to set a level-2 cache at the granularity of classes and sets.
Xxx. HBM. xml file
Class:
<Class name = "" table = "" catalog = "hbql">
<Cache Usage = "read-write">
....
</Class>
Set:
<Class name = "A" table = "" catalog = "hbql">
<Cache Usage = "read-write"> <! -- Set a concurrent access policy for usage -->
<Set name = "B" cascade = "Save-Update" inverse = "true">
<Cache Usage = "read-write"> with this part added, the B object associated with object A will not be placed in the second-level cache, but can be normally set.
....
</Set>
....
</Class>
2. Select the second-level cache plug-in
Hibernate. cfg. xml
<Session-factory>
<Property name = "hibernate. cache. provider_class">
Org. hibernate. cache. ehcachprovider |
</Property>
.....
</Session-factory>
Cache attributes
Attribute name |
Description |
Hibernate. cache. provider_class |
Cache Class Name |
Hibernate. cache. use_minimal_puts |
Cluster useful |
Hibernate. cache. use_query_cache |
Query Cache |
Hibernate. cache. use_second_level_cache |
Enable Level 2 cache? |
Hibernate. cache. region_prefix |
Prefix of the second-level cache region name |
Hibernate. cache. use_structured_entries |
Force hibernate to store the second-level cache in a certain format |
Level 3 cache:
Also called query Cache
When the first query is performed, the results are placed in the second-level cache.
Usage:
1. Frequently queried query statements
2. Rarely modify the query-related data
Usage:
Load () method: Execution Process: Level 1 cache-> Level 2 Cache-> delayed loading-> database (with delayed loading configured, a proxy class is returned)
Get () does not use query Cache
List (): The first query will be filled. Three Levels of cache will return the value. If the obtained value is ID, the value will be queried by ID for a long time... data changes cache automatically cleared... it may cause n queries
Iterator (): first obtain the ID, and then use load ()
NOTE: If some columns of the select object, the entire result set will be cached... if the query result is a collection of object objects, only the id value of the object will be cached.
1. Configure the second-level cache
2. Start query Cache
<Property name = "cache. use_query_cache"> true </property>
3. Use in the program
Query. setcacheable (true );
Query. setcacheregion ("mycacheregion") // if not set, the standard region is used by default.