Ehcache cache configuration and basic usage

Source: Internet
Author: User

Objective

In the extensive use of Java projects. It is an open source, designed to increase the high cost of data removed from the RDBMS, high latency to take a caching scheme.

Just because Ehcache is robust (Java-based development), certified (with Apache 2.0 license), full of features (detailed later),

So it is used in the various nodes of the large complex distributed Web application.

features

1, fast enough
Ehcache's release has been a long time, after several years of effort and countless performance tests, Ehcache was eventually designed in large, high concurrency systems.
2. Simple enough
The interface provided by the developer is very simple and straightforward, and it only takes a few minutes of your time to build from Ehcache to use. In fact, many developers do not know that they use Ehcache,ehcache is widely used in other open source projects
For example: Hibernate
3, enough pocket
On this feature, the official gave a very cute name small foot print, the general Ehcache release version will not go to 2m,v 2.2.3 668KB.
4, enough light quantity
The core program relies only on SLF4J this one package, not one!
5, good expansion
Ehcache provides memory and hard disk storage for big data, recent versions allow multiple instances, save objects with high flexibility, LRU, LFU, FIFO culling algorithms, base properties to support hot provisioning, multiple plugins supported
6. Listener
Cache Manager Listener (Cachemanagerlistener) and Cache Listener (Cacheevenlistener), do some statistics or data consistency broadcast very useful

How to use Maven dependencies
1 <!--add Cache--2 <dependency>3     <groupid>net.sf.ehcache</ Groupid>4     <artifactId>ehcache-core</artifactId>5     <version>2.6.6 </version>6 </dependency>
Configuration file

Under the Resources Resource Directory, create a ehcache-config.xml file that reads as follows:

1<?xml version= "1.0" encoding= "UTF-8"?>2<ehcache xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"3xsi:nonamespaceschemalocation= "Http://ehcache.org/ehcache.xsd"4Updatecheck= "false" >5<!--Ehcache to the Ehcache website each time it is launched check the new version using the updatecheck= "false" above to disable this check new version--6    7<!--8 Name:cache Unique identification9 Eternal: Whether the cache is permanently validTen maxelementsinmemory: Maximum number of cached objects in memory OneOverflowtodisk (true,false): After the cache object reaches the maximum number, writes the cache to the hard disk A diskpersistent: Hard Drive persistence - timetoidleseconds: Cache purge Time - timetoliveseconds: Cache Survival Time the diskexpirythreadintervalseconds: Disk cache cleanup thread run interval - memorystoreevictionpolicy: Cache emptying policy -1. Fifo:first in First out FIFO -2. Lfu:less frequently used has been the least used since +3. Lru:least recently used the least recently used -- +     A<diskstore path= "Java.io.tmpdir/ehcache"/> at     -<Defaultcache -Maxelementsinmemory= "10000" -Eternal= "false" -Timetoidleseconds= "120" -Timetoliveseconds= "120" inOverflowtodisk= "true" -maxelementsondisk= "10000000" toDiskpersistent= "false" +Diskexpirythreadintervalseconds= "120" -memorystoreevictionpolicy= "FIFO"/> the     *<cache name= "Normal_cache" $maxelementsinmemory= "200"Panax NotoginsengEternal= "false" -timetoidleseconds= "7200" thetimetoliveseconds= "7200" +Overflowtodisk= "true" Amaxelementsondisk= "1000" theDiskpersistent= "false" +Diskexpirythreadintervalseconds= "120" -memorystoreevictionpolicy= "FIFO"/> $</ehcache>
Spring Consolidated Configuration

Note the following must be registered in spring's master configuration file

  1  <!--cache Profile interface--> 2  <cache: Annotation-driven cache-manager= "CacheManager"/> 3  <!--Create Cache Manager Factory-->< Span style= "COLOR: #008080" > 4  <bean id= "cachemanagerfactory" class  = " Org.springframework.cache.ehcache.EhCacheManagerFactoryBean "> 5  < Property Name= "Configlocation" value= "Classpath:ehcache-config.xml" ></property>  6  </bean> 7  <!--Create a cache manager-->< Span style= "COLOR: #008080" > 8  <bean id= "CacheManager" class  = " Org.springframework.cache.ehcache.EhCacheCacheManager "> 9  <property Name= "CacheManager" ref= "cachemanagerfactory" ></property>10  </ Bean> 
How to use

Here you can use annotations @Cacheable (value = "Cache_pos_codes") where value is the configuration name of the configuration file ehcache-config.xml that is set, the import is important to note org.springframework.cache.annotation.Cacheable;

 1  @RequestMapping (value = "/date", method = Requestmethod.get,produces = Mediatype.application_json_value + CHARSET)  2    @ResponseBody  3  @Cacheable (value = "Cache_pos_codes" )  4  public   String getDate () { 5  SimpleDateFormat SimpleDateFormat = new  simpledateformat ("Yyyy-mm-dd HH:mm:ss" Span style= "color: #000000");  6  return  Simpledateformat.format (new   Date ());  7 } 

Reprint http://www.zhoutao123.com/?p=106

Ehcache cache configuration and basic usage

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.