Ehcache First Experience

Source: Internet
Author: User

First, Introduction

EhCache is a pure Java in-process caching framework, which has the characteristics of fast and lean. Ehcache is a widely used, open source Java distributed cache. Primarily for general purpose caches, Java EE and lightweight containers. It features memory and disk storage, cache loaders, cache extensions, cache exception handlers, a gzip cache servlet filter, and support for rest and soap APIs.

The main features are:

    1. Fast
    2. Simple
    3. Multiple cache policies
    4. Cache data has two levels: memory and disk, so there's no need to worry about capacity issues
    5. Cached data is written to disk during a virtual machine restart
    6. Distributed caching can be done via RMI, pluggable APIs, and more
    7. Listening interface with cache and cache manager
    8. Supports multi-cache manager instances, as well as multiple cache areas for one instance
    9. Provides a cache implementation of Hibernate
Second, detailed configuration

EhCache provides us with a rich set of configurations to configure cache settings.

Properties of the cache element:

Name: Cache names

Maxelementsinmemory: Maximum number of cached objects in memory

Maxelementsondisk: The maximum number of cache objects on the hard disk, if 0 means infinity

Eternal:true indicates that the object never expires, the Timetoidleseconds and Timetoliveseconds properties are ignored, and the default is False

Overflowtodisk:true indicates that when the number of objects in the memory cache reaches the maxelementsinmemory limit, the overflow object is written to the hard disk cache. Note: If the cached object is to be written to the hard disk, the object must implement the Serializable interface.

DISKSPOOLBUFFERSIZEMB: Disk buffer size, default is 30MB. Each cache should have its own cache.

Diskpersistent: Whether to cache virtual machine Restart period data

Diskexpirythreadintervalseconds: Disk failed thread run time interval, default is 120 seconds

Timetoidleseconds: Sets the maximum time, in seconds, that an object is allowed to be idle. When an object has been idle for longer than the Timetoidleseconds property value since the last time it was accessed, the object expires and Ehcache will empty it from the cache. This property is valid only if the Eternal property is false. If the property value is 0, the object can be idle indefinitely

Timetoliveseconds: The maximum amount of time, in seconds, that a set object is allowed to exist in the cache. When an object has been stored in the cache for more than the Timetoliveseconds property value, the object expires and Ehcache clears it from the cache. This property is valid only if the Eternal property is false. If the property value is 0, the object can exist indefinitely in the cache. Timetoliveseconds must be greater than timetoidleseconds attribute to make sense

Memorystoreevictionpolicy: When the maxelementsinmemory limit is reached, Ehcache will clean up the memory according to the specified policy. The optional policies are LRU (least recently used, default policy), FIFO (first in, out), LFU (minimum number of accesses).

Third, persistent to the hard disk

In some systems, the use of Ehcache to cache data, sometimes do not need to persist to the hard disk, but in the crawler system, you need to persist the data to the hard disk, to determine whether the URL is duplicated, that is, to crawl past URLs to filter, avoid repeated crawling.

Reference configuration:

1 <?XML version= "1.0" encoding= "UTF-8"?>2  3 <Ehcache>4    <!-- 5 disk Storage: Transfer objects temporarily unused in the cache to the hard disk, similar to the virtual memory of the Windows system6 Path : Specifies the path to the storage object on the hard disk7     -8    <DiskstorePath= "C:\ehcache" />9     Ten    <!--  One Defaultcache: Default cache configuration information, if not specified, all objects are processed according to this configuration item A maxelementsinmemory: Sets the upper limit of the cache and stores the maximum number of record objects - Eternal: Indicates whether the object never expires - Overflowtodisk: When the number of element in memory reaches Maxelementsinmemory, Ehcache will write the element to disk the     - -    <Defaultcache -       maxelementsinmemory= "+" - Eternal= "true" + Overflowtodisk= "true"/> -   +     <!--  A Maxelementsinmemory set to 1,overflowtodisk set to true, as long as there is a cache element, it is stored directly on the hard disk at Eternal set to true to permanently work on behalf of the object - Maxelementsondisk set to 0 means that the maximum number of cache objects on the hard disk is infinitely large - Diskpersistent set to TRUE indicates cache virtual machine Restart period Data -       - -     <Cache -       name= "a" in maxelementsinmemory= "1"  - Eternal= "true" to Overflowtodisk= "true"  + Maxelementsondisk= "0" - diskpersistent= "true"/> the   * </Ehcache>
Iv. examples

The Ehcache version used here is 2.X, and this version is relatively stable.

1.pom.xml

    <Dependency>        <groupId>Net.sf.ehcache</groupId>        <Artifactid>Ehcache</Artifactid>        <version>2.10.4</version>    </Dependency>

2. Code implementation

1      Public Static voidMain (string[] args) {2String Path = System.getproperty ("User.dir");3         //creating the Cache manager4CacheManager manager = cachemanager.create (path + "\\src\\main\\resources\\ehcache.xml"));5         //gets the specified cache6Cache cache = Manager.getcache ("a");7         //add an element to the cache8Cache.put (NewElement ("name", "Joe"));9         //get cache elements based on keyTenElement ele = cache.get ("name"); OneSystem.out.println ("name==" +Ele.getobjectvalue ()); A          -Cache.flush ();//Refresh Cache -Manager.shutdown ();//Close the cache manager the}

3. View the Ehcache folder in the C drive with the following file, which indicates that the cache is on disk.

Ehcache First Experience

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.