发布于 2018-03-19 01:56:23 | 169 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Spring Framework 开源j2ee框架

Spring是什么呢?首先它是一个开源的项目,而且目前非常活跃;它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层 中必须使用Spring,因为它模块化的很好,允许你根据自己的需要选择使用它的某一个模块;它实现了很优雅的MVC,对不同的数据访问技术提供了统一的接口,采用IOC使得可以很容易的实现bean的装配,提供了简洁的AOP并据此实现Transcation Managment,等等


本篇文章主要介绍了详解Spring MVC 集成EHCache缓存,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

废话少说,直接上代码:

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 获取缓存的数据

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务