CodeWeblog.com » storage memory,second level cache,secondary cache » Hibernate caching mechanisms

Hibernate caching mechanisms

Cache is located in applications and physical data sources for the copy data temporary storage memory region, the purpose of the application in order to reduce the physical data sources for the number of visits in order to improve application performance.
Hibernate in the query data, the first to go search the cache, if found on the direct use, can not find the time will be from the physical data source retrieval, therefore, put frequently used data loaded into the buffer zone, on the can significantly reduce application to visit physical data sources, making the procedure significantly improved operating performance.
Hibernate cache Category:

Session cache, a cache.

SessionFactory cache built into the cache and external cache. Built-in cache SessionFactory objects are stored in a collection of some property that contains the data (mapping elements and the pre-defined SQL statements, etc.), for applications for It is read-only. external cache are stored in a copy of database data, its role and a similar cache. apart from the secondary cache to memory as a storage medium, but also can choose to use external hard disk storage devices.

Hibernate cache scope

Hibernate second level cache and cache are located are located in persistence layer and database data are used to store a copy of The biggest difference is the scope of the cache not the same.

The scope of the cache is divided into 3 categories:

1. Service scope
Affairs of the cache can only be the scope of current affairs to visit, each Service has its own cache, data cache usually the object of interrelated forms. Cached life cycle depends on the services life cycle, only when the Service At the end of the cache until the end of the life cycle. Service-wide use of cache memory as a storage medium, a cache on the scope of Belong to Affairs.
2. Applications
Application cache can be applied in all matters within the scope of shared access. Cached life cycle depends on the application life cycle, only when the application of the end of the cache until the end of the life cycle. Application of the cache can be the use of memory or hard disk as a storage medium, secondary cache on belong to the scope of application.
3. Cluster scope
At the cluster environment, the cache by a machine or multiple machines in the process of sharing the data in the cache is copied to the cluster environment, each process node, inter-process communication through remote to ensure that the data in the cache consistency, cache the data in the usual form of loose objects.

Hibernate's cache management

Level cache management:

evit (Object obj) will be designated a persistent object from the cache to clear, release the memory occupied by the target resource, the specified object from the persistent state into a detached state, thus becoming the object free.
clear () will be a cache of all the persistent object removal, the release of its occupied memory resources
contains (Object obj) to determine whether the specified object exists in a cache.
flush () to refresh the contents of a buffer zone to keep pace with the database data.

Secondary cache management:

evict (Class arg0, Serializable arg1) will be designated a class of persistent object ID from the secondary cache to clear, release the resources occupied by objects.
1.sessionFactory.evict(Customer.class, new Integer(1));

evict (Class arg0) category will be assigned all the persistent object to remove from the secondary cache, the release of its occupied memory resources.
sessionFactory.evict(Customer.class);

evictCollection (String arg0) will be assigned to all categories of persistent object designated collection removed from the secondary cache, the release of its occupied memory resources.
sessionFactory.evictCollection("Customer.orders");

Hibernate configuration of the secondary cache

First of all, not all data fit on the secondary cache, look at what kind of data fit on the secondary cache come from? What kind of data does not fit on the secondary cache come from?
The following situations do not fit into the secondary cache is loaded:
1. Frequently modified data
2. Absolutely does not allow concurrent access of the data appear
3. With other applications to share data
The following kinds of situations has been loaded into the appropriate secondary cache:
1. Data Update frequency low
2. Permit occasional problem complicated by the non-essential data
3. Will not be concurrent accesses data
4. Constant data
5. Will not be third-party modified data

Hibernate cache secondary function is to rely on plug-in to configure the secondary cache of the implementation, Hibernate for integration of these plug-ins, Hibernate provides a org.hibernate.cache.CacheProvider an excuse, it acts as a buffer between the plug and Hibernate adapter.

Secondary cache commonly used plug-ins
EHCache org.hibernate.cache.EhCacheProvider
OSCache org.hibernate.cache.OSCacheProvider
SwarmCahe org.hibernate.cache.SwarmCacheProvider
JBossCache org.hibernate.cache.TreeCacheProvider

EHCache a brief profile
hibernate.cfg.xml
<hibernate-configuration>  
       <session-factory>  
          <!-- 设置二级缓存插件EHCache的Provider类-->  
          <property name="hibernate.cache.provider_class">  
             org.hibernate.cache.EhCacheProvider  
          </property>  
          <!-- 启动"查询缓存" -->  
          <property name="hibernate.cache.use_query_cache">  
             true  
         </property>  
      </session-factory>  
    </hibernate-configuration>

ehcache.xml
# <ehcache>  
   <!-- maxElementsInMemory为缓存对象的最大数目, eternal设置是否永远不过期,timeToIdleSeconds对象处于空闲状态的最多秒数,timeToLiveSeconds对象处于缓存状态的最多秒数 -->  
   <diskStore path="java.io.tmpdir"/>  
     <defaultCache maxElementsInMemory="10000" eternal="false"  timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true"/>  
 </ehcache>

****. hbm.xml
# <?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>  
        <!-- 设置该持久化类的二级缓存并发访问策略 read-only read-write nonstrict-read-write transactional-->  
        <cache usage="read-write"/>      
    </class>  
   
 </hibernate-mapping>
Digg Technorati StumbleUpon Mixx del.icio.us Reddit BlinkList Furl YahooMyWeb feedburner

Tags: storage memory (RSS), second level cache (RSS), secondary cache (RSS), persistence layer (RSS), sql statements (RSS), buffer zone (RSS), data cache (RSS), cache memory (RSS), query data (RSS), cache data (RSS), service scope (RSS), external hard disk (RSS), source retrieval (RSS), memory region (RSS), external cache (RSS), disk storage (RSS), temporary storage (RSS), data mapping (RSS), storage medium (RSS), storage devices (RSS)

Permalink: http://www.codeweblog.com/hibernate-caching-mechanisms/

Leave a reply