Detailed Spring MVC integration EHCache cache _java - JAVA

Source: Hi learning network sensitive and eager Forum www.piaodoo.com welcome to learn from each other

Without further ado, directly on the code:

ehcache.xml file

<?xml version="1.0" encoding="UTF-8"?>
<ehcache dynamicConfig="false" monitoring="off" updateCheck="false" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"> 

  <! - defined cache policy 
    eternal = "false" // elements are eternal, and if it never expires (must be set) 
    maxEntriesLocalHeap = "1000" // maximum heap memory cache object 0 None (must be set) 
    overflowToDisk = "false" // When the cache reaches maxElementsInMemory value is whether to allow the overflow to disk (must be set) 
    diskPersistent = "false" // disk cache when the VM is restarted whether to keep (the default is false) 
    timeToIdleSeconds = "0" // elements leading to expire access interval (seconds). When the eternal is false, this property is valid, 0 can never idle, default 0 
    timeToLiveSeconds = "600" // elements present in the cache time (in seconds). 0 means not expire forever 
    memoryStoreEvictionPolicy = "LFU" // When reached maxElementsInMemory, how to force the expulsion of default "Recently Used (LRU)" strategy, as well as other first-in first-out FIFO, least used LFU, less use of LRU 
  --> 

  <!--
    1) maxElementsInMemory (a positive integer): The maximum number of objects cached in memory
    2) maxElementsOnDisk (positive integer): the maximum number of objects on the disk cache, the default value is 0, not to limit. 
    3) eternal: the object stored in the cache permanently set the properties, default is false. When it is true timeToIdleSeconds, timeToLiveSeconds failure.
    4) timeToIdleSeconds (unit: seconds): Object idle time refers to how long the object will fail in not being accessed. Only the eternal is false is valid. The default value is 0, which means you can have access.
    5) timeToLiveSeconds (unit: seconds): the survival time of the object, the object refers to the time from creation to fail needs. Only the eternal is false is valid. The default value is 0, which means you can have access.
    6) overflowToDisk: If the data memory exceeds the memory limit, if you want to cache to disk. 
    7) diskPersistent: whether persisted on disk. Jvm refers to restart after the data is valid. The default is false.
  8) diskSpoolBufferSizeMB (Unit: MB): disk size DiskStore use the default value of 30MB. Each cache using their DiskStore.
    9) memoryStoreEvictionPolicy: If the data memory exceeds the memory limit, when the policy of the disk cache. Default LRU, optional FIFO, LFU.
    FIFO (first in first out): FIFO
    LFU (Less Frequently Used): the least used, there is a cache hit attribute element, the value will be clear the cache hit minimal.
    LRU (Least Recently Used) default policy: a least recently used cache elements have a time stamp, when the cache capacity is full, but need to make room for the new cache element, then the time stamp from the existing cache elements current time is farthest element will clear the cache.
  10) The maximum number of objects maxEntriesLocalHeap cache heap memory  
  -->
    <diskStore path="java.io.tmpdir"></diskStore>
  <defaultCache 
    eternal="false" 
    maxEntriesLocalHeap="0" 
    timeToIdleSeconds="120" 
    timeToLiveSeconds="120"
    maxElementsInMemory="10000"
    overflowToDisk="true"
    diskPersistent="true"
  /> 

  <cache 
    name="userCache" 
    maxEntriesLocalHeap="10000" 
  />  
  <cache
    name="studentCache"
    maxEntriesLocalHeap="10000"
  />

</ehcache>

The need to increase the JAR package

Hi learning network

springmvc.xml need to add the following in beans

xmlns:cache="http://www.springframework.org/schema/cache"
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd

Increase bean

<! - Enable caching annotation functions (configure it in Spring main configuration file) ->
<cache:annotation-driven cache-manager="cacheManager"/>  
<-! Spring offer based on the realization of Ehcache Cache Manager ->  
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
  <property name="configLocation" value="classpath:config/ehcache.xml"/>  
</bean>  
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">  
  <property name="cacheManager" ref="cacheManagerFactory"/>  
</bean>

Operation class EHCacheUtils

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * Operating cache class
 * 
 * @author jiangadam
 */

public class EhcacheUtils {

  private static final String path = "/config/ehcache.xml"; // EHCache profile address

  private CacheManager manager;

  private static EhcacheUtils ehCache;

  private EhcacheUtils(String path) {
    manager = CacheManager.create(getClass().getResource(path));
  }

  public static EhcacheUtils getInstance() {
    if (ehCache == null) {
      ehCache = new EhcacheUtils(path);
    }
    return ehCache;
  }

  /**
   * Cache object
   * 
   * @Param cacheName
   * Cache name
   * @Param key
   * Cache KEY
   * @param value
   * Cache value
   */
  public void put(String cacheName, String key, Object value) {
    Cache cache = manager.getCache (cacheName);
    Element element = new Element(key, value);
    cache.put(element);
  }

  /**
   * Get a cached object, did not return NULL
   * 
   * @Param cacheName
   * @Param key
   * @return
   */
  public Object get(String cacheName, String key) {
    Cache cache = manager.getCache (cacheName);
    Element element = cache.get(key);
    return element == null ? null : element.getObjectValue();
  }

  public Cache get(String cacheName) {
    return manager.getCache(cacheName);
  }

  public void remove(String cacheName, String key) {
    Cache cache = manager.getCache (cacheName);
    cache.remove(key);
  }

}

PUT write cache

Hi learning network

GET get cached data

Hi learning network

That's all for this article, I want to be helpful to learn, I hope you will support sensitive and eager Forum / Hi learning network.

The original address is: http: //www.piaodoo.com/thread-13248-1-2.html stockings control www.txdah.com 131 outside www.buzc.org enjoyable learning can help to learn better!

Guess you like

Origin www.cnblogs.com/txdah/p/12093846.html