zoukankan      html  css  js  c++  java
  • Ehcache的配置与使用

    Ehcache是JAVA内制的一个缓存框架!

    目的:缓解频繁读取数据库的压力;

    初步配置如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <ehcache updateCheck="false"  name="shirocache">
     3     <diskStore path="java.io.tmpdir"/>
     4     <defaultCache    
     5         maxElementsInMemory="10000"    
     6         maxElementsOnDisk="0"    
     7         eternal="true"    
     8         overflowToDisk="true"    
     9         diskPersistent="false"    
    10         timeToIdleSeconds="0"    
    11         timeToLiveSeconds="0"    
    12         diskSpoolBufferSizeMB="50"    
    13         diskExpiryThreadIntervalSeconds="120"    
    14         memoryStoreEvictionPolicy="LFU"    
    15     />      
    16     <cache name="shiro_cache"
    17            maxElementsInMemory="2000"
    18            maxEntriesLocalHeap="2000"
    19            eternal="false"
    20            timeToIdleSeconds="0"
    21            timeToLiveSeconds="0"
    22            maxElementsOnDisk="0"
    23            overflowToDisk="true"
    24            memoryStoreEvictionPolicy="FIFO"
    25            statistics="true">
    26     </cache>
    27 </ehcache>

    使用如下:

    1、存储数据;

    1                    CacheManager cacheManager = CacheManager.create(url);
    2                    Cache cache=cacheManager.getCache("shiro_cache");
    3                    Element element = new Element("pwd", password);
    4                    cache.put(element);

    2、读取数据 

    1             URL url = getClass().getResource("/ehcache/ehcache.xml");
    2             CacheManager cacheManager = CacheManager.create(url);
    3             Cache cache=cacheManager.getCache("shiro_cache");
    4             Element element = cache.get("pwd");
    5             System.out.println(element.toString());
  • 相关阅读:
    51Nod 1052/1053/1115 最大M子段和V1/V2/V3
    51Nod1207 内存管理
    51Nod1207 内存管理
    51Nod1444 破坏道路
    51Nod1444 破坏道路
    51Nod1349 最大值
    51Nod1349 最大值
    51nod1485 字母排序
    aspx页面中的html标签中的值传到aspx.cs文件中的方法
    C#属性的使用
  • 原文地址:https://www.cnblogs.com/XinHuai/p/6866216.html
Copyright © 2011-2022 走看看