当前位置: 移动技术网 > IT编程>开发语言>Java > 详解Spring MVC 集成EHCache缓存

详解Spring MVC 集成EHCache缓存

2019年07月22日  | 移动技术网IT编程  | 我要评论
废话少说,直接上代码: ehcache.xml 文件 <?xml version="1.0" encoding="utf-8"?>

废话少说,直接上代码:

ehcache.xml 文件

<?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"> 

  <!-- 定义缓存策略 
    eternal="false"         // 元素是否永恒,如果是就永不过期(必须设置) 
    maxentrieslocalheap="1000"   // 堆内存中最大缓存对象数,0没有限制(必须设置) 
    overflowtodisk="false"     // 当缓存达到maxelementsinmemory值是,是否允许溢出到磁盘(必须设置) 
    diskpersistent="false"     // 磁盘缓存在vm重新启动时是否保持(默认为false) 
    timetoidleseconds="0"      // 导致元素过期的访问间隔(秒为单位). 当eternal为false时,这个属性才有效,0表示可以永远空闲,默认为0 
    timetoliveseconds="600"     // 元素在缓存里存在的时间(秒为单位). 0 表示永远存在不过期 
    memorystoreevictionpolicy="lfu" // 当达到maxelementsinmemory时,如何强制进行驱逐默认使用"最近使用(lru)"策略,其它还有先入先出fifo,最少使用lfu,较少使用lru 
  --> 

  <!--
    1)maxelementsinmemory(正整数):在内存中缓存的最大对象数量
    2)maxelementsondisk(正整数):在磁盘上缓存的最大对象数量,默认值为0,表示不限制。 
    3)eternal:设定缓存对象保存的永久属性,默认为 false 。当为 true 时 timetoidleseconds、timetoliveseconds 失效。 
    4)timetoidleseconds(单位:秒): 对象空闲时间,指对象在多长时间没有被访问就会失效。只对eternal为false的有效。默认值0,表示一直可以访问。
    5)timetoliveseconds(单位:秒): 对象存活时间,指对象从创建到失效所需要的时间。只对eternal为false的有效。默认值0,表示一直可以访问。
    6)overflowtodisk:如果内存中数据超过内存限制,是否要缓存到磁盘上。 
    7)diskpersistent:是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。 
  8)diskspoolbuffersizemb(单位:mb): diskstore使用的磁盘大小,默认值30mb。每个cache使用各自的diskstore。
    9)memorystoreevictionpolicy:如果内存中数据超过内存限制,向磁盘缓存时的策略。默认值lru,可选fifo、lfu。
    fifo(first in first out):先进先出
    lfu(less frequently used):最少被使用,缓存的元素有一个hit属性,hit值最小的将会被清除缓存。
    lru(least recently used)默认策略:最近最少使用,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清除缓存。
  10) maxentrieslocalheap 堆内存中最大缓存对象数  
  -->
    <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>

需要增加的jar包


springmvc.xml 需要在beans增加以下

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

增加bean

<!-- 启用缓存注解功能(请将其配置在spring主配置文件中) -->
<cache:annotation-driven cache-manager="cachemanager"/>  
<!-- spring提供的基于的ehcache实现的缓存管理器 -->  
<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>

ehcacheutils 操作类

import net.sf.ehcache.cache;
import net.sf.ehcache.cachemanager;
import net.sf.ehcache.element;

/**
 * 操作缓存类
 * 
 * @author jiangadam
 */

public class ehcacheutils {

  private static final string path = "/config/ehcache.xml"; // ehcache 的配置文件地址

  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;
  }

  /**
   * 缓存一个对象
   * 
   * @param cachename
   *      缓存的名字
   * @param key
   *      缓存的key
   * @param value
   *      缓存的值
   */
  public void put(string cachename, string key, object value) {
    cache cache = manager.getcache(cachename);
    element element = new element(key, value);
    cache.put(element);
  }

  /**
   * 获取一个缓存的对象,没有返回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 写入缓存

get 获取缓存的数据

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网