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());
  • 相关阅读:
    PAT 字符串-02 删除字符串中的子串
    带滚动条的文本文件
    PAT IO-04 混合类型数据格式化输入(5)
    PAT IO-03 整数均值
    PAT IO-02 整数四则运算
    linux 之shell
    linux 软件包安装-脚本安装
    Linux 关闭防火墙命令
    linux RPM包管理-yum在线管理
    linux 软件包管理
  • 原文地址:https://www.cnblogs.com/XinHuai/p/6866216.html
Copyright © 2011-2022 走看看