Cache is located in the application and physical data sources, for temporary storage of memory copy of data, the purpose is to reduce the application of the visits of physical data sources to enhance the application's operating performance.
Hibernate query data, the first to go find the cache, if found to be used directly, can not find the time will be retrieved from a physical data source, so the frequent use of the data loaded into the cache, you can greatly reduce application of the physical data sources, making the program significantly improve operating performance.
Hibernate cache Category:
Session cache, a cache.
SessionFactory cache is divided into built-in cache, and external cache. Built-in cache is stored in some collection of attributes SessionFactory object contains data (mapping elements and, according to a predefined SQL statements, etc.), for the application, it is only Reading. external cache data stored in the database, a copy of its role and a similar cache. 2 cache in addition to memory as a storage medium, it can use hard drives and other external storage devices.
Hibernate cache scope
Hibernate in a cache and secondary cache are located are located in the persistence layer and database data are used to store a copy of, the biggest difference is the scope of the different cache.
The scope of the cache is divided into three categories:
1. Cache scope of services range of services can only be access to the current transaction, each transaction has its own cache, the cache data inside the object usually interrelated forms. Cache of the life cycle depends on the transaction life cycle, and only when the transaction the end of the life cycle of the cache will end. Service-wide use of memory as the storage media cache, a cache belonging to the scope of services.
2. Application cache application can be applied to all matters within the scope of shared access. Cached life cycle depends on the application life cycle, only when applied at the end of the life cycle before the end of the cache. Applications can use the cache memory or hard disk as a storage medium, the secondary cache on the fall application.
3. Cluster range of cluster environment, the cache is a machine or multiple machines in the process of sharing, the cache data is copied to the cluster environment, each process node, inter-process communication to ensure the remote data cache line , the cache data objects commonly used form of loose data.
Hibernate's cache management
Level cache management:
evit (Object obj) to the specified persistent object removed from a cache, the release of the memory object resources specified object from the persistent state into a detached state, thus becoming free object.
clear () to a cache of all persistent objects removed, release the memory resources of its
contains (Object obj) check whether the specified object exists in a cache.
flush () to refresh a cache of content to make it sync with the database data.
2 cache management:
evict (Class arg0, Serializable arg1) to a specified class ID of the persistent object cache from 2 to clear, release the resources occupied by the object.
- sessionFactory.evict (Customer. class, new Integer (1));
sessionFactory.evict(Customer.class, new Integer(1));
evict (Class arg0) all of the specified class of persistent objects removed from the secondary cache, the release of its occupation of the memory resources.
- sessionFactory.evict (Customer. class);
evictCollection (String arg0) all of the specified class of the specified set of persistent objects removed from the secondary cache, the release of its occupation of the memory resources.
- sessionFactory.evictCollection ("Customer.orders");
Hibernate second-level cache configuration
First, not all data fits in the secondary cache, look at what kind of data to fit on the secondary cache? What kind of data do not fit secondary to the cache?
The following is not suitable for these types of cases loaded into the secondary cache:
1. Frequently modified data
2. Absolutely does not allow for concurrent access to data
3. And other applications to share data in the following types of cases that have been loaded into the secondary cache in the right:
1. Low frequency of data update
2. To allow occasional non-critical data concurrency issues
3. Will not be concurrent access to data
4. Constant data
5. Will not be third-party modification of data
Hibernate second-level cache functionality is configured by plug-ins to achieve the secondary cache, Hibernate to integrate these plug-ins, Hibernate provides a org.hibernate. Cache.CacheProvider excuse, it acts as a buffer between the adapter plug and Hibernate.
Commonly used secondary cache plug-in
EHCache org.hibernate. Cache.EhCacheProvider
OSCache org.hibernate. Cache.OSCacheProvider
SwarmCahe org.hibernate. Cache.SwarmCacheProvider
JBossCache org.hibernate. Cache.TreeCacheProvider
The configuration of a brief EHCache
- <! - Set 2 cache plug-in EHCache the Provider class ->
- <Property name = "hibernate. Cache.provider_class">
- org.hibernate. cache.EhCacheProvider
- </ Property>
- <! - Start "query cache" ->
- <Property name = "hibernate. Cache.use_query_cache">
- </ Property>
- </ Session-factory>
- </ Hibernate-configuration>
<hibernate -configuration> <session-factory> <!-- Set the L2 cache plugin EHCache Provider Class --> <property name="hibernate .cache.provider_class"> org.hibernate .cache.EhCacheProvider </property> <!-- Start the query cache " --> <property name="hibernate .cache.use_query_cache"> true </property> </session-factory> </hibernate -configuration>
- <! - MaxElementsInMemory maximum number of objects to cache, eternal forever, but settings are on, timeToIdleSeconds object is idle for the most number of seconds, timeToLiveSeconds object in a state of maximum number of seconds the cache ->
- <DiskStore path = "java.io.tmpdir" />
- <DefaultCache maxElementsInMemory = "10000" eternal = "false" timeToIdleSeconds = "300" timeToLiveSeconds = "600" overflowToDisk = "true" />
- </ Ehcache>
<ehcache> <!-- maxElementsInMemory To cache an object, the maximum number of eternal settings are never expired ,timeToIdleSeconds The object is in the idle state of the maximum number of seconds, the timeToLiveSeconds object is in the cache status of the maximum number of seconds --> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true"/> </ehcache>
- <? Xml version = "1.0" encoding = 'UTF-8'?>
- <! DOCTYPE hibernate-mapping PUBLIC
- "- / / Hibernate / Hibernate Mapping DTD 3.0 / / EN"
- "Http://hibernate .sourceforge.net / hibernate-mapping-3.0.dtd">
- <! - Set the persistent classes of secondary cache concurrency strategy for read-only read-write nonstrict-read-write transactional ->
- <Cache usage = "read-write" />
- </ Class>
- </ Hibernate-mapping>
<?xml version="1.0" encoding='UTF-8'?> <!DOCTYPE hibernate -mapping PUBLIC "-//Hibernate /Hibernate Mapping DTD 3.0//EN" "http://hibernate .sourceforge.net/hibernate -mapping-3.0.dtd" > <hibernate -mapping> <class> <!-- Set the persistent classes of L2 cache concurrent access policy read-only read-write nonstrict-read-write transactional--> <cache usage="read-write"/> </class> </hibernate -mapping>