Ehcache call description

Source: Internet
Author: User

Ehcache call description
Introduction to ehcache

Ehcache is an excellent, lightweight, and local cache solution. It can save and access static resources in high concurrency.

Introduce the ehcache jar package

Here we introduce the latest ehcache jar package:

<dependency>    <groupId>net.sf.ehcache</groupId>    <artifactId>ehcache</artifactId>    <version>2.10.0</version>    <type>pom</type></dependency>     
Basic Concepts

1. CacheManager
CacheManager is the cache manager. To use ehcache, you must create a CacheManager. There are two ways to create it:
A. Singleton mode:

private static volatile CacheManager cacheManager = CacheManager.create()

B. ehcache 2.x later allows the presence of multiple cacheManager, you can configure the new cacheManager through. xml, the following demo-ehcache.xml

<ehcache name="DemoCacheManager"></ehcache>

Use the. xml file to initialize the non-singleton mode cacheManager, as shown below:

    private static volatile CacheManager cacheManager = CacheManager.newInstance(this.class.getClassLoader().getResourceAsStream("demo-ehcache.xml"));

2. cache)
One CacheManager can manage multiple caches. Each cache has its own attributes in the memory, such as cache elements) the maximum number of cached instances, the Maximum Cache survival time in the memory, and the cache elimination algorithm. One cache can store multiple cache elements ). The cache must be registered before use. There are two registration methods:
1. Register with the xml configuration file. The name of the cache can be configured in the configuration file, the maximum number of cached files in the heap, And the cache elimination algorithm. There are many parameters. The following is a simple example:

<ehcache>    <cache name="demo"           maxEntriesLocalHeap="10000"           eternal="false"           timeToIdleSeconds="300"           timeToLiveSeconds="600"           memoryStoreEvictionPolicy="LFU"            >    </cache></ehcache>

A cache named demo is configured above. The maximum number of cached files in the memory is 10000, and the maximum survival time is 10 minutes (600 s ), the maximum accessible time is 5 minutes (300 s ). When the number exceeds the maximum number, the LFU algorithm is used for elimination. Eternal indicates whether the cache is permanently invalid. If this value is true, the maximum survival time and maximum accessible time above are invalid.
2. Dynamic registration through code
The following is an example, which is the same as registering an xml file:

Cache newMemCache = new Cache ("demo", 1000, // maximum number of elements in the memory MemoryStoreEvictionPolicy. LFU, // false, // whether to save to disk "/data/appdatas", // fale where a disk exists, // whether it is permanently valid for 600, // timeToLiveSeconds 300, // timeToIdleSeconds false, // whether to save to disk 60*60 when JVM is restarted, // how long the disk timeout thread runs (this parameter is useless) null // register the event); cacheManager. addCache (newMemCache );

3. Element cache Elements
The cache element is the smallest unit of data storage, which consists of its ElementKey and object. It is defined as follows:

Element element = new Element(elementkey, object);

Note: The object here can be any object !!

Call description

1. add operation
Add an Element to the cache as follows:

  Cache cache = cacheManager.getCache("demo");  if (cache != null) {      Element element = new Element("demoElementKey", "demo object");      cache.put(element);  }

2. get operation
Retrieving elements from the cache

  Cache cache = cacheManager.getCache("demo");  if (null != cache) {      Element element = cache.get("demoElementKey");  }
Example

For more information, see ehcahce-client.

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.