Hibernate cache: Configuration instructions for the two-level cache using Ehcache as Hibernate

Source: Internet
Author: User

Ehcache Introduction

Ehcache is a fast, lightweight Java application cache. Ehcache is supported in Hibernate.

Http://ehcache.org/documentation/integrations/hibernate

Hibernate and Ehcache Integration

To complete the integration of Hibernate and Ehcache, just follow the steps below.

1. Download Ehcache-core Package

You can enter the URL in the browser: http://sourceforge.net/projects/ehcache/files/ehcache-core/Select the appropriate version to download.

You can also use Maven dependencies:

<Dependency>      <groupId>Net.sf.ehcache</groupId>      <Artifactid>Ehcache-core</Artifactid>      <version>2.6.9</version></Dependency>

2. Add Hibernate-ehcache Package

Because you want to match hibernate, download the Ehcache plugin package based on the hibernate version .

<Dependency>      <groupId>Org.hibernate</groupId>      <Artifactid>Hibernate-ehcache</Artifactid>      <version>${hibernate-version}</version></Dependency>

3. Configure Ehcache as Level two cache in Hibernate.cfg.xml

Ehcache is used as a level two cache in Hibernate.

For versions above Hibernate3.3:

You can also use:

<property name= "Hibernate.cache.provider_class" >org.hibernate.cache.EhCacheProvider</property>

For hibernate 4.X Versions:

Because the hibernate4.x version integrates Hibernate-ehcache.jar into the Hibernate-core.jar. So the above configuration has changed:

You can also use:

<property name= "Hibernate.cache.provider_class" >org.hibernate.cache.EhCacheProvider</property>

4. Turn on level two cache or query cache
<!--Level Two cache -< Propertyname= "Hibernate.cache.use_second_level_cache">True</ Property><!--Query Cache -< Propertyname= "Hibernate.cache.use_query_cache">True</ Property>
5. Configuring multiple caches in the Ehcache.xml configuration

If Ehcache.xml is not configured in our project, the default configuration file is used: Ehcache-failsafe.xml, which can be found in Ehcache-core.jar.

In a mapper, you can configure various cache policies. You can configure caching for an entity, or you can configure a cache for a collection in an entity, and you can, of course, configure caching for the query.

To show these three cache configurations, put them all in one file. It just shows that there are three ways to configure the cache. Next, see how Ehcache is configured for these three cache configurations.

There are two common elements in Ehcache.xml:<diskstore>, <cache>

<diskstore path= "Java.io.tmpdir"/>

Used to configure the location of the disk when using disk storage.

<Cache> Configuration Instructions

Before you know the Ehcache configuration, let's look at the properties of Ehcache

The necessary properties:

Name: is the name of the cache and is unique in the Ehcache.xml file. Named constraints are different depending on the three levels of caching.

Maxelementsinmemory: The maximum number of objects in memory.

Maxelementsondisk: The maximum number of objects stored on disk.

Eternal: Permanent. If set permanently, the limit of the object cache time is ignored.

Overflowtodisk: Sets whether the number of objects in memory is stored on disk when the maximum value is exceeded.

Optional properties:

Timetoidleseconds: Sets the idle time before the object expires. The default value is 0

Timetoliveseconds: Sets the time to live before an object expires. The default value is 0

Diskpersistent: Sets whether the object is persisted on disk when the JVM restarts.

DISKSPOOLBUFFERSIZEMB: Sets the buffer size on the disk.

Memorystoreevictionpolicy: Sets the purge policy when memory reaches Maxelementsinmemory. The default policy is LRU (least recently used), and there are other policies, such as FIFO (first-in-LFU policy), and the minimum usage policy.

Next, let's talk about these three levels of caching separately.

Three levels of <cache> 's Name property set 1) Entity-level caching

Entity-level caching, the Name property of the <cache> element in Ehcache.xml is named: The full name of the class. For example, there is a class in the project: Com.fjn.other.hibernateCache.entity.Event

Then it should be configured as:

<namecom.fjn.other.hibernateCache.entity.Event "Other properties />

2) Collection-level caching

The collection-level cache, which is said here, is actually a collection of attributes in an entity. For example:

Public CALSS customer{    private String name;    Private List<Address>  adds;}

Such a class, if it is the property of the customer adds using the cache, that is, using the collection-level cache, its mapper file should be a configuration like this:

<name= "adds">    <usage= " Read-only "/></list>

In this case, we can do the following configuration in Ehcache.xml:

<nameCom.fjn.other.hibernateCache.entity.Customer.adds "other properties  />

3) query-level caching

This is actually the query Cache that I learned in the previous section.

When you use query cache, you can set the name or not set name. If you do not set name, the default name is: Org.hibernate.cache.StandardQueryCache, or Org.hibernate.cache.UpdateTimestampsCache.

This can be seen by using debug on your own test.

Above is the default value for which the name is not specified. What if I have named name?

If you want to specify the Name property of the query cache yourself, you typically use query. Yourcachename. That is, you will typically use query.

This is used in the program:

Query query=session.createquery (HQL); Query.setcacheable (true); Query.setcacheregion ("Query . Yourcachename ");

6 , in Hibernate.cfg.xml in the configuration Ehcache.xml the location of the configuration file

Configure the location of the Ehcache.xml in Hibernate.cfg.xml, for example:

<name= "Cache.provider_configuration_file_resource_path"> Config/hibernate/ehcache/ehcache.xml</Property>


Hibernate cache: Configuration instructions for the two-level cache using Ehcache as Hibernate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.