Ehcache Learning (5) cache Configuration

Source: Internet
Author: User

EhcacheCache ConfigurationHttp://blog.csdn.net/mgoann/archive/2009/04/17/4087714.aspx

Introduction

The cache configuration is flexible. There are several official cache configuration methods. You can declare the configuration, configure in XMLProgramWhen you configure or call the constructor in.

You can configure the cache fromCodeYou can also use the runtime configuration. The so-called runtime configuration is nothing more than the configuration in the code. The following are the benefits of runtime configuration:

· Configure all the caches in the same place, which makes it easy to manage the memory and disk consumption of the cache.

· You can change the cache configuration during release.

· Check the configuration error information during the installation phase to avoid running errors.

This article will describe the ehcache. xml configuration file in detail. You can copy an existing ehcache. xml file during configuration. If not, click here to download it.

Ehcache-failsafe.xml

If you call the default constructor of cachemanager to create an instance of cachemanager, this method will go to classpath to find the ehcache. xml file, otherwise it will find the ehcache-failsafe.xml file under the class path. While the ehcache-failsafe.xml is included in the jar package, all it can certainly find.

The ehcache-failsafe.xml provides a very simple default configuration that allows you to use ehcache without creating ehcache. xml.

In this case, the user is reminded to create a correct ehcache configuration.

Ehcache. xml fragment:

 
<Ehcache>
 
<Diskstore Path = "Java. Io. tmpdir"/>
 
<Defaultcache
 
Maxelementsinmemory = "10000"
 
Eternal = "false"
 
Timetoidleseconds = "120"
 
Timetoliveseconds = "120"
 
Overflowtodisk = "true"
 
Maxelementsondisk= "10000000"
Diskpersistent = "false"
 
Diskexpirythreadintervalseconds = "120"
 
Memorystoreevictionpolicy = "LRU"
 
/>
 
</Ehcache>

Ehcache. xmlAnd other configuration files

In versions earlier than the Ehcache-1.6, only ASCII-encoded ehcache. xml configuration files are supported. In Versions later than the Ehcach-1.6, The ehcache. xml configuration file that supports utf8 encoding is supported. Because of backward compatibility, there is no need to convert all configuration files using ASCII encoding to utf8.

A cachemanager must have an xml configuration. An error occurs when multiple cachemanager instances use the same configuration file because of the disk path or listening port.

The following describes the specific ehcache. XML instance and Configuration Guide.

<Ehcache xmlns: xsi = http://www.w3.org/2001/XMLSchema-instance

· Cachemanager Configuration

Dmulticastgroupport = 4446 to configure the listening port.

· Diskstore Configuration

If you are using diskstore (disk cache), you must configure the diskstore configuration item. If not configured, ehcache uses Java. Io. tmpdir.

The "path" attribute of diskstroe is used to configure the physical path used by the disk cache. The file suffix used by the ehcache disk cache is. Data and. index.

<Disstore Path = "Java. Io. tmpdir"/>

· Cachemanagereventlistener Configuration

We can instantiate a cachemanagerpeerprovider through the handler. When we added and removed cache from cachemanager, we will notify cachemanagerpeerprovider. In this way, we can make some statistics on the cache in cachemanager.

The event Listening Classes registered with cachemanager are adding a cache and removing a cache.

<Cachemanagereventlistenerfacow.class = "properties ="/>

· Cachemanagerpeerprovider Configuration

In the cluster, configure cachemanager to configure cachemanagerpeerproviderfactory to create cachemanagerpeerprovider. The specific example is as follows:

<Cachemanagerpeerproviderfactoryclass = "net. SF. ehcache. distribution.

Rmicachemanagerpeerproviderfactory"

Properties = "peerdiscovery = manual, rmiurls = // server1: 40000/samplecache1 | // server2: 40000/samplecache1 | // server1: 40000/samplecache2 | // server2: 40000/samplecache2"

Propertyseparator = ","/>

· Cachemanagerpeerlistener Configuration

The cachemanagerpeerlistener configuration is used to monitor the distribution of cached messages in the cluster.

<Cachemanagerpeerlistenerfactory

Class = "net. SF. ehcache. Distribution. rmicachemanagerpeerlistenerfactory"

Properties = "hostname = fully_qualified_hostname_or_ip,

Port = 40001,

Soetettimeoutmillis = 120000"

Propertyseparator = ","/>

· Cache Configuration

· Name: the unique identifier of the cache.

· Maxelementsinmemory: Maximum number of cached objects in the memory.

· Maxelementsondisk: Maximum number of cached objects on the disk. If it is 0, it indicates infinity.

· Eternal: whether the element is permanently valid. Once set, timeout does not work.

· Overflowtodisk: This attribute is configured. When the number of elements in the memory reaches maxelementsinmemory, ehcache writes the elements to the disk.

· Timetoidleseconds: sets the allowed idle time before the element expires. This attribute is optional only when the element is not permanently valid. The default value is 0, that is, the idle time is infinite.

· Timetoliveseconds: sets the allowed survival time of the element before it expires. The maximum time is between the creation time and the expiration time. Used only when the element is not permanently valid. The default value is 0. That is, the element survival time is infinite.

· Diskpersistent: Indicates whether to cache data during the VM restart period. (This virtual machine refers to a virtual machine that has never been clear about what it is. Some people hope to give some advice ).

· Diskexpirythreadintervalseconds: The Running interval of the disk failure thread. The default value is 120 seconds.

· Diskspoolbuffersizemb: this parameter sets the cache size of diskstore (disk cache. The default value is 30 mb. Each cache should have its own buffer.

· Memorystoreevictionpolicy: When the maxelementsinmemory limit is reached, ehcache clears the memory according to the specified policy. The default policy is LRU (least recently used ). You can set it to FIFO (first-in-first-out) or LFU (rarely used ). It is a pity that ehcache does not provide an interface for customizing policies. It only supports three specified policies and does not feel ideal.

· Cache Exception Handling Configuration

<Cacheexceptionhandlerfactory class = "com. example. exampleexceptionhandlerfactory" properties = "loglevel = Fine"/>

Summary

The general cache configuration is described in detail here. For RMI cache and cluster cache, refer to here.

Below are several configuration examples:

· Ehcache default cache Configuration

<Defaultcache

Maxelementsinmemory = "10000"

Eternal = "false"

Timetoidleseconds = "120"

Timetoliveseconds = "120"

Overflowtodisk = "true"

Diskspoolbuffersizemb = "30"

Maxelementsondisk= "10000000"

Diskpersistent = "false"

Diskexpirythreadintervalseconds = "120"

Memorystoreevictionpolicy = "LRU"

/>

· Samplecache1 Configuration

Simple configuration: This configuration is available in the ehcache. xml file. You 'd better delete it and configure it yourself before using ehcache.

Cache name samplecache1. Up to 10000 elements can be cached in the memory, and the elements are idle.

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.