博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
EHCache工具类
阅读量:7225 次
发布时间:2019-06-29

本文共 10323 字,大约阅读时间需要 34 分钟。

  hot3.png

/**

 *
 * @className:EHCacheConfig.java
 *
 */

public class EHCacheConfig {

 /**

  * 元素最大数量
  */

 public static int MAXELEMENTSINMEMORY = 50000;

 /**

  *
  * 是否把溢出数据持久化到硬盘
  */

 public static boolean OVERFLOWTODISK = true;

 /**

  *
  * 是否会死亡
  */

 public static boolean ETERNAL = false;

 /**

  *
  * 缓存的间歇时间
  */

 public static int TIMETOIDLESECONDS = 600;

 /**

  *
  * 存活时间(默认一天)
  */

 public static int TIMETOlIVESECONDS = 86400;

 /**

  *
  * 需要持久化到硬盘否
  */

 public static boolean DISKPERSISTENT = false;

 /**

  *
  * 内存存取策略
  */

 public static String MEMORYSTOREEVICTIONPOLICY = "LFU";

}

 

import java.util.List;

import net.sf.ehcache.Cache;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;

import org.apache.commons.lang.StringUtils;

/**

 * ehcache工具类
 */

public class EHCacheUtil {

 private static CacheManager cacheManager = null;
 private static Cache cache = null;

 static{

  EHCacheUtil.initCacheManager();
  EHCacheUtil.initCache("cache");
 }
 /**
  *
  * 初始化缓存管理容器
  */
 public static CacheManager initCacheManager() {
  try {
   if (cacheManager == null)
    cacheManager = CacheManager.getInstance();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return cacheManager;
 }

 /**

  *
  * 初始化缓存管理容器
  *
  * path
  *            ehcache.xml存放的路徑
  */
 public static CacheManager initCacheManager(String path) {
  try {
   if (cacheManager == null) {
    cacheManager = CacheManager.getInstance().create(path);
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return cacheManager;
 }

 /**

  * 初始化cache
  */

 public static Cache initCache(String cacheName) {

  checkCacheManager();
  if (null == cacheManager.getCache(cacheName)) {
   cacheManager.addCache(cacheName);
  }
  cache = cacheManager.getCache(cacheName);
  return cache;
 }

 /**

  *
  * 添加缓存
  *
  * key
  *            关键字
  * value
  *            值
  */
 public static void put(Object key, Object value) {
  checkCache();
  // 创建Element,然后放入Cache对象中
  Element element = new Element(key, value);
  cache.put(element);
 }

 /**

  * 获取cache
  *
  * key
  *            关键字
  * @return
  */
 public static Object get(Object key) {
  checkCache();
  Element element = cache.get(key);
  if (null == element) {
   return null;
  }
  return element.getObjectValue();
 }

 /**

  * 初始化缓存
  *
  * @param cacheName
  *            缓存名称
  * @param maxElementsInMemory
  *            元素最大数量
  * @param overflowToDisk
  *            是否持久化到硬盘
  * @param eternal
  *            是否会死亡
  * @param timeToLiveSeconds
  *            缓存存活时间
  * @param timeToIdleSeconds
  *            缓存的间隔时间
  * @return 缓存
  * @throws Exception
  */
 public static Cache initCache(String cacheName, int maxElementsInMemory, boolean overflowToDisk, boolean eternal,
   long timeToLiveSeconds, long timeToIdleSeconds) throws Exception {
  try {
   CacheManager singletonManager = CacheManager.create();
   Cache myCache = singletonManager.getCache(cacheName);
   if (myCache != null) {
    CacheConfiguration config = cache.getCacheConfiguration();
    config.setTimeToLiveSeconds(timeToLiveSeconds);
    config.setMaxElementsInMemory(maxElementsInMemory);
    config.setOverflowToDisk(overflowToDisk);
    config.setEternal(eternal);
    config.setTimeToIdleSeconds(timeToIdleSeconds);
   }
   if (myCache == null) {
    Cache memoryOnlyCache = new Cache(cacheName, maxElementsInMemory, overflowToDisk, eternal, timeToLiveSeconds,
      timeToIdleSeconds);
    singletonManager.addCache(memoryOnlyCache);
    myCache = singletonManager.getCache(cacheName);
   }
   return myCache;
  } catch (Exception e) {
   throw new Exception("init cache " + cacheName + " failed!!!");
  }
 }

 /**

  * 初始化cache
  *
  * @param cacheName
  *            cache的名字
  * @param timeToLiveSeconds
  *            有效时间
  * @return cache 缓存
  * @throws Exception
  */
 public static Cache initCache(String cacheName, long timeToLiveSeconds) throws Exception {
  return EHCacheUtil.initCache(cacheName, EHCacheConfig.MAXELEMENTSINMEMORY, EHCacheConfig.OVERFLOWTODISK,
    EHCacheConfig.ETERNAL, timeToLiveSeconds, EHCacheConfig.TIMETOIDLESECONDS);
 }

 /**

  * 初始化Cache
  *
  * @param cacheName
  *            cache容器名
  * @return cache容器
  * @throws Exception
  */
 public static Cache initMyCache(String cacheName) throws Exception {
  return EHCacheUtil.initCache(cacheName, EHCacheConfig.TIMETOlIVESECONDS);
 }

 /**

  * 修改缓存容器配置
  *
  * @param cacheName
  *            缓存名
  * @param timeToLiveSeconds
  *            有效时间
  * @param maxElementsInMemory
  *            最大数量
  * @throws Exception
  */

 public static boolean modifyCache(String cacheName, long timeToLiveSeconds, int maxElementsInMemory) throws Exception {

  try {
   if (StringUtils.isNotBlank(cacheName) && timeToLiveSeconds != 0L && maxElementsInMemory != 0) {
    CacheManager myManager = CacheManager.create();
    Cache myCache = myManager.getCache(cacheName);
    CacheConfiguration config = myCache.getCacheConfiguration();
    config.setTimeToLiveSeconds(timeToLiveSeconds);
    config.setMaxElementsInMemory(maxElementsInMemory);
    return true;
   } else {
    return false;
   }
  } catch (Exception e) {
   throw new Exception("modify cache " + cacheName + " failed!!!");
  }
 }

 /**

  *
  * 向指定容器中设置值
  *
  * @param vesselName
  *            容器名
  *
  * @param key
  *            键
  *
  * @param value
  *            值
  *
  * @return 返回真
  *
  * @throws Exception
  *             异常
  */

 public static boolean setValue(String cacheName, String key, Object value) throws Exception {

  try {
   CacheManager myManager = CacheManager.create();
   Cache myCache = myManager.getCache(cacheName);
   if (myCache == null) {
    myCache = initCache(cacheName);
   }
   myCache.put(new Element(key, value));
   return true;
  } catch (Exception e) {
   throw new Exception("set cache " + cacheName + " failed!!!");
  }
 }

 /**

  *
  * 向指定容器中设置值
  *
  * @param cacheName
  *            容器名
  *
  * @param key
  *            键
  *
  * @param value
  *            值
  *
  * @param timeToIdleSeconds
  *            间歇时间
  *
  * @param timeToLiveSeconds
  *            存活时间
  *
  * @return 真
  *
  * @throws Exception
  *             抛出异常
  */

 public static boolean setValue(String cacheName, String key, Object value, Integer timeToLiveSeconds) throws Exception {

  try {
   CacheManager myManager = CacheManager.create();
   Cache myCache = myManager.getCache(cacheName);
   if (myCache == null) {
    initCache(cacheName, timeToLiveSeconds);
    myCache = myManager.getCache(cacheName);
   }
   myCache.put(new Element(key, value, EHCacheConfig.ETERNAL, EHCacheConfig.TIMETOIDLESECONDS, timeToLiveSeconds));
   return true;
  } catch (Exception e) {
   throw new Exception("set cache " + cacheName + " failed!!!");
  }
 }

 /**

  *
  * 从ehcache的指定容器中取值
  *
  * @createTime 2012-4-23
  *
  * @param key
  *            键
  *
  * @return 返回Object类型的值
  *
  * @throws Exception
  *             异常
  */

 public static Object getValue(String cacheName, String key) throws Exception {

  try {
   CacheManager myManager = CacheManager.create();
   Cache myCache = myManager.getCache(cacheName);
   if (myCache == null) {
    myCache = initMyCache(cacheName);
   }
   return myCache.get(key).getValue();
  } catch (Exception e) {
   return null;
  }
 }

 /**

  *
  * 删除指定的ehcache容器
  *
  * @param vesselName
  *
  * @return 真
  *
  * @throws Exception
  *             失败抛出异常
  */

 public static boolean removeEhcache(String cacheName) throws Exception {

  try {
   CacheManager myManager = CacheManager.create();
   myManager.removeCache(cacheName);
   return true;
  } catch (Exception e) {
   throw new Exception("remove cache " + cacheName + " failed!!!");
  }
 }

 /**

  *
  * 删除所有的EHCache容器
  *
  * @param cacheName
  *            容器名
  *
  * @return 返回真
  *
  * @throws Exception
  *             失败抛出异常
  */

 public static boolean removeAllEhcache(String cacheName) throws Exception {

  try {
   CacheManager myManager = CacheManager.create();
   myManager.removalAll();
   return true;
  } catch (Exception e) {
   throw new Exception("remove cache " + cacheName + " failed!!!");
  }
 }

 /**

  *
  * 删除EHCache容器中的元素
  *
  * @param cacheName
  *            容器名
  *
  * @param key
  *            键
  *
  * @return 真
  *
  * @throws Exception
  *             失败抛出异常
  */

 public static boolean removeElment(String cacheName, String key) throws Exception {

  try {
   CacheManager myManager = CacheManager.create();
   Cache myCache = myManager.getCache(cacheName);
   myCache.remove(key);
   return true;
  } catch (Exception e) {
   throw new Exception("remove cache " + cacheName + " failed!!!");
  }
 }

 /**

  *
  * 删除指定容器中的所有元素
  *
  * @param cacheName
  *            容器名
  *
  * @param key
  *            键
  *
  * @return 真
  *
  * @throws Exception
  *             失败抛出异常
  */

 public static boolean removeAllElment(String cacheName, String key) throws Exception {

  try {
   CacheManager myManager = CacheManager.create();
   Cache myCache = myManager.getCache(cacheName);
   myCache.removeAll();
   return true;
  } catch (Exception e) {
   throw new Exception("remove cache " + cacheName + " failed!!!");
  }
 }

 /**

  * 释放CacheManage
  */

 public static void shutdown() {

  cacheManager.shutdown();
 }

 /**

  * 移除cache
  *
  * @param cacheName
  */

 public static void removeCache(String cacheName) {

  checkCacheManager();
  cache = cacheManager.getCache(cacheName);
  if (null != cache) {
   cacheManager.removeCache(cacheName);
  }
 }

 /**

  * 移除cache中的key
  *
  * @param cacheName
  */

 public static void remove(String key) {

  checkCache();
  cache.remove(key);
 }

 /**

  * 移除所有cache
  */

 public static void removeAllCache() {

  checkCacheManager();
  cacheManager.removalAll();
 }

 /**

  *
  * 移除所有Element
  */

 public static void removeAllKey() {

  checkCache();
  cache.removeAll();
 }

 /**

  *
  * 获取所有的cache名称
  *
  * @return
  */

 public static String[] getAllCaches() {

  checkCacheManager();
  return cacheManager.getCacheNames();
 }

 /**

  *
  * 获取Cache所有的Keys
  *
  * @return
  */

 public static List getKeys() {

  checkCache();
  return cache.getKeys();
 }

 /**

  *
  * 检测cacheManager
  */

 private static void checkCacheManager() {

  if (null == cacheManager) {
   throw new IllegalArgumentException("调用前请先初始化CacheManager值:EHCacheUtil.initCacheManager");
  }
 }

 private static void checkCache() {

  if (null == cache) {
   throw new IllegalArgumentException("调用前请先初始化Cache值:EHCacheUtil.initCache(参数)");
  }
 }

 public static void main(String[] arg) {

  // 初始化--必须
  EHCacheUtil.initCacheManager();
  EHCacheUtil.initCache("cache");
  EHCacheUtil.put("A", "AAAAA");
  EHCacheUtil.put("B", "BBBBB");
  EHCacheUtil.put("F", "FFFFF");
  System.out.println(EHCacheUtil.get("F"));
  List keys = EHCacheUtil.getKeys();
  for(int i=0;i<keys.size();i++){
   System.out.println(keys.get(i));
  }
  EHCacheUtil.shutdown();
 }
}

 

<?xml version="1.0" encoding="UTF-8"?>

<ehcache xmlns:xsi=""
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="true" monitoring="autodetect"
         dynamicConfig="true">

    <diskStore path="java.io.tmpdir"/>

    <defaultCache

            maxEntriesLocalHeap="10000"
            eternal="true"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            diskSpoolBufferSizeMB="30"
            maxEntriesLocalDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </defaultCache>

    <cache name="cache"

           maxEntriesLocalHeap="10000"
           maxEntriesLocalDisk="1000"
           eternal="true"
           diskSpoolBufferSizeMB="20"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           memoryStoreEvictionPolicy="LFU"
           transactionalMode="off">
        <persistence strategy="localTempSwap"/>
    </cache>
</ehcache>

转载于:https://my.oschina.net/glenxu/blog/624481

你可能感兴趣的文章
boost bind使用指南 - Make Progress Everyday! - 博客频道 - CSDN.NET
查看>>
第二部分:开发简要指南-第七章 与其他应用程序交互
查看>>
Javascript this 的一些学习总结
查看>>
POJ 1637 Sightseeing tour
查看>>
一位温州大商人的酒后真言:在中国想要成功的22条秘诀
查看>>
JQuery EasyUI 最简单的左右布局实现及tab的右键菜单实现
查看>>
UIBotton UIlabel Ios 下拉框
查看>>
优先队列实现n路归并算法O(n * lgK)
查看>>
PING 确认网络连接情况
查看>>
腾讯的云计算平台构建工具 开源
查看>>
POJ 1274 The Perfect Stall
查看>>
常用命令
查看>>
用PowerDesigner将DB2数据字典导成WORD
查看>>
1,300萬像素Xperia TX K.O.相機 - 東方日報
查看>>
网站群
查看>>
网页设计欣赏:20个带给你灵感的国外网站作品
查看>>
Android基础:SQLites数据库事物处理的优越性
查看>>
highCharts API
查看>>
Qt之布局管理——(1)基本布局管理
查看>>
Oracle 连接玩我!ORA-12514及ORA-28547错误解决
查看>>