Cache is representation of database near to application. when you cache the read-mostly(which will be repeatedly used) data, you can reduce the number of round trip between your application server and database server. Which increase performance of your application.

In general you can have three types of cache scope :
  1. Transactional scope cache.
  2. Process scope cache.
  3. Clustered scope cache.
1. Transactional scope cache : This kind of catch will be created whenever the transaction is started and run as long as transaction is running. Transactional scope cache will be destroyed whenever the transaction end either by commit or by rollback.
2. Process scope cache : This kind of cache can be accessed by multiple transaction running in the application or process.

3. Clustered scope cache : When your enterprise application is large scale application then you must use clustered environment where multiple server is clustered and integrated with LBS(load balancing server) for running the request. In clustered scope cache when one node caches the data then same data will also be replicated to other nodes automatically.
First Level Cache :
  • Session cache is first level cache.
  • Session cache is transnational cache.
  • Session cache is enabled by default and should not be disabled.
  • When ever you insert or update or select or delete the records then those persistence object will be placed automatically in session cache.
  • You can remove the persistence object from session cache using evict(obj)  and clear().
Second Level Cache :
  • Query cache is second level cache.
  • Query cache can be process scope and cluster scope cache.
  • Query cache is disabled by default. You have to enable explicitly if required.
  • When ever you execute the hibernate Queries HQL, QBC etc then all records returned by select statement will be placed in Query cache.
Steps to implements Query cache : 
  1. Enable the query cache by writing the following property in hibernate configuration doc.
    <property name="hibernate.cache.use_query_cache">true</property>
  2. Specify the required cache provider in hibernate configuration document.
    <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
    Note : Cache provider supported by hibernate
    ->HashtableCacheProvider
    ->EhCacheProvider
    ->OSCacheProvider
    ->SwarmCacheProvider
    ->TreeCacheProvider(jboss cache provider)
  3. Specify the required caching concurrency strategies in hibernate mapping doc.
    <cache usage="xx">
    here xx can be one of the following
    ->read-only
    ->nonstrict-read-write
    ->read-write
    ->transactional
    Note : You have to specify caching concurrency strategies separately for class, collections and Associations.
  4. call setCacheable() method with true value on query or criteria or SQLQuery objects.
    Query q=session.createQuery("from customer");
    q.setCachable(true);
    List list=q.list();
  5. Specify the storage implementations and Expiration policies
    Note : this will change from cache provider to provider.

1 comment:

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic