Ehcache cache configuration and basic usage

foreword

  Widely used in java projects. It is an open source caching scheme designed to improve the cost and latency of data retrieval from RDBMS.

Just because Ehcache is robust (based on java development), certified (with apache 2.0 license), and full of features (more on this later),

Therefore, it is used in various nodes of large and complex distributed web applications.

Features

1. Fast enough
  Ehcache has been released for a long time. After several years of hard work and countless performance tests, Ehcache was finally designed for large, high concurrency systems.
2. Simple enough
  The interface provided by developers is very simple and clear, from It only takes a few minutes of your precious minutes to get Ehcache up and running. In fact, many developers don't know that they are using Ehcache, Ehcache is widely used in other open source projects ,
  such as: hibernate
3, pocket-sized enough
  For this feature, the official gave a very cute name small foot print, generally Ehcache The release version will not reach 2M, and V 2.2.3 is only 668KB.
4. Light enough
  The core program only depends on the slf4j package, there is no one!
5. Good extension
  Ehcache provides memory and hard disk storage for large data. The latest version allows multiple instances, high flexibility of saving objects, LRU, LFU, FIFO elimination algorithms, basic properties support hot configuration, and more plug-ins are supported.
6, Listeners
  Cache Manager Listener (CacheManagerListener) and CacheEvenListener (CacheEvenListener) are useful for doing some statistics or data consistency broadcasting

how to use

Maven dependencies

1 <!--加入缓存-->
2 <dependency>
3     <groupId>net.sf.ehcache</groupId>
4     <artifactId>ehcache-core</artifactId>
5     <version>2.6.6</version>
6 </dependency>

configuration file

Create an ehcache-config.xml file in the resources resource directory with the following content:

1 <?xml version="1.0" encoding="UTF-8"?>
 2 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3         xsi:noNamespaceSchemaLocation="http: //ehcache.org/ehcache.xsd"
 4         updateCheck="false">
 5     <!-- EhCache should connect to the ehcache website every time it starts to check for the new version Use updateCheck="false" as above to disable this Check the new version -->
 6     
7     <!--  
 8          name:cache unique identifier   
 9          eternal: whether the cache is permanently valid   
 10          maxElementsInMemory: the maximum number of cached objects in memory  
 11          overflowToDisk( true ,false ): After the maximum number of cached objects is reached, the cache is written to the hard disk  
 12         diskPersistent: Hard disk persistence  
 13          timeToIdleSeconds: Cache clearing time   
 14          timeToLiveSeconds: Cache survival time
 15          diskExpiryThreadIntervalSeconds: Disk cache cleanup thread running interval
 16          memoryStoreEvictionPolicy: Cache clearing policy
 17          1 .FIFO: first in first out  
 18          2 .LFU: Less Frequently Used  
 19          3 .LRU: Least Recently Used   
 20      -->
 21     
22     <diskStore path="java.io.tmpdir/ehcache" />
 23     
24     < defaultCache 
 25        maxElementsInMemory=" 10000" 
26       eternal="false" 
27       timeToIdleSeconds="120"
28       timeToLiveSeconds="120" 
29       overflowToDisk="true" 
30       maxElementsOnDisk="10000000"
31       diskPersistent="false"
32       diskExpiryThreadIntervalSeconds="120" 
33       memoryStoreEvictionPolicy="FIFO" />
34    
35    <cache name="normal_cache"
36       maxElementsInMemory="200"
37       eternal="false"
38       timeToIdleSeconds="7200"
39       timeToLiveSeconds="7200"
40       overflowToDisk="true"
41       maxElementsOnDisk="1000"
42       diskPersistent="false"
43       diskExpiryThreadIntervalSeconds="120"
44       memoryStoreEvictionPolicy="FIFO" />
45 </ehcache> 

spring integration configuration

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

1 <!--Cache configuration file interface-->
 2 <cache:annotation-driven cache-manager="cacheManager"/>
 3 <!--Create cache manager factory-->
 4 <bean id="cacheManagerFactory" class ="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
 5      <property name="configLocation" value="classpath:ehcache-config.xml"></property>
 6 </bean>
 7 <!--Create cache management Handler -->
 8 <bean id="cacheManager" class ="org.springframework.cache.ehcache.EhCacheCacheManager">
 9      <property name="cacheManager" ref="
cacheManagerFactory"></property>
10 </bean>

Instructions

Here you can use the annotation method @Cacheable(value = "cache_pos_codes") where value is the configuration name of the set configuration file ehcache-config.xml, it should be noted that import 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");
6         return simpleDateFormat.format(new Date());
7     }

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


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325569756&siteId=291194637