java缓存EHCache Spring MVC项目配置ehcache

  1. package com.test.util;  
  2.   
  3. import net.sf.ehcache.Cache;  
  4. import net.sf.ehcache.CacheManager;  
  5. import net.sf.ehcache.Element;  
  6.   
  7. /** 
  8.  * ehcache 缓存工具类 
  9.  *  
  10.  * cacheName在ehcache.xml中配置 
  11.  */  
  12. public class EhcacheUtil {  
  13.   
  14.     public static CacheManager manager = CacheManager.create();  
  15.   
  16.     public static Object get(String cacheName, Object key) {  
  17.         Cache cache = manager.getCache(cacheName);  
  18.         if (cache != null) {  
  19.             Element element = cache.get(key);  
  20.             if (element != null) {  
  21.                 return element.getObjectValue();  
  22.             }  
  23.         }  
  24.         return null;  
  25.     }  
  26.   
  27.     public static void put(String cacheName, Object key, Object value) {  
  28.         Cache cache = manager.getCache(cacheName);  
  29.         if (cache != null) {  
  30.             cache.put(new Element(key, value));  
  31.         }  
  32.     }  
  33.   
  34.     public static boolean remove(String cacheName, Object key) {  
  35.         Cache cache = manager.getCache(cacheName);  
  36.         if (cache != null) {  
  37.             return cache.remove(key);  
  38.         }  
  39.         return false;  
  40.     }  
  41.   
  42.     public static void main(String[] args) {  
  43.         String key = "key";  
  44.         String value = "hello";  
  45.         EhcacheUtil.put("mytest", key, value);  
  46.         System.out.println(EhcacheUtil.get("mytest", key));  
  47.     }  
  48.   
  49. }  
[java]   view plain  copy
  1. <pre class="html" name="code">ehcache.xml配置</pre><pre class="html" name="code"> </pre><pre class="html" name="code"><?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:noNamespaceSchemaLocation="ehcache.xsd"  
  5.          updateCheck="false" monitoring="autodetect"  
  6.          dynamicConfig="true">  
  7.   
  8.     <diskStore path="java.io.tmpdir"/>  
  9.       
  10.     <defaultCache  
  11.             maxElementsInMemory="10000"  
  12.             eternal="true"  
  13.             timeToIdleSeconds="120"  
  14.             timeToLiveSeconds="120"  
  15.             overflowToDisk="false"  
  16.             diskPersistent="false"  
  17.             diskExpiryThreadIntervalSeconds="120"  
  18.             memoryStoreEvictionPolicy="LRU"  
  19.             />  
  20.               
  21.     <cache name="mytest"   
  22.         maxElementsInMemory="100000"   
  23.         eternal="true"   
  24.         overflowToDisk="false"   
  25.         memoryStoreEvictionPolicy="LRU">  
  26.     </cache>  
  27.   
  28.     <cache name="userCache"   
  29.         maxElementsInMemory="50000"   
  30.         eternal="false"   
  31.         timeToIdleSeconds="300"  
  32.         timeToLiveSeconds="300"  
  33.         overflowToDisk="false"   
  34.         memoryStoreEvictionPolicy="LRU"  
  35.     />  
  36.   
  37. </ehcache></pre>  
  38. <pre></pre>  
  39.     http://www.cnblogs.com/warmingsun/p/3328077.html
  40. 理论介绍:http://blog.csdn.net/huaweitman/article/details/42078901
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值