package com.bbcmart.util;

import net.sf.ehcache.Cache;

import net.sf.ehcache.CacheManager;

import net.sf.ehcache.Element;

//import net.sf.ehcache.store.MemoryStoreEvictionPolicy;

public class CacheHelper {


private static CacheManager cacheManager;


public enum CacheType {


policy, airline, jinri, result, other,


bulletin, linkImage, linkChar, advertisement,waiter


}

public enum OtherType {


str_airline

}

static {


init();

}

public static void init() {


try {


cacheManager = CacheManager.create();

cacheManager.addCache(createCacheByType(CacheType.policy));


cacheManager.addCache(createCacheByType(CacheType.airline));


cacheManager.addCache(createCacheByType(CacheType.jinri));


cacheManager.addCache(createCacheByType(CacheType.result));


cacheManager.addCache(createCacheByType(CacheType.other));


cacheManager.addCache(createCacheByType(CacheType.bulletin));


cacheManager.addCache(createCacheByType(CacheType.linkImage));


cacheManager.addCache(createCacheByType(CacheType.linkChar));


cacheManager.addCache(createCacheByType(CacheType.advertisement));


cacheManager.addCache(createCacheByType(CacheType.waiter));


}


catch (Exception e) {


e.printStackTrace();


}


}

private static Cache createCacheByType(CacheType aCacheType) {


Cache result = null;


if (CacheType.policy.equals(aCacheType))


result = new Cache(CacheType.policy.toString(), 4000, false, true, 0, 0);


if (CacheType.airline.equals(aCacheType))


result = new Cache(CacheType.airline.toString(), 4000, false, false, 300, 300);


if (CacheType.jinri.equals(aCacheType))


result = new Cache(CacheType.jinri.toString(), 4000, false, false, 300, 300);


if (CacheType.result.equals(aCacheType))


result = new Cache(CacheType.result.toString(), 4000, false, false, 300, 300);


if (CacheType.other.equals(aCacheType))


result = new Cache(CacheType.other.toString(), 100, false, true, 0, 0);


if (CacheType.bulletin.equals(aCacheType))


result = new Cache(CacheType.bulletin.toString(), 1000, false, false, 900, 600);


if (CacheType.linkImage.equals(aCacheType))


result = new Cache(CacheType.linkImage.toString(), 1000, false, false, 900, 600);


if (CacheType.linkChar.equals(aCacheType))


result = new Cache(CacheType.linkChar.toString(), 1000, false, false, 900, 600);


if (CacheType.advertisement.equals(aCacheType))


result = new Cache(CacheType.advertisement.toString(), 1000, false, false, 900, 600);


if (CacheType.waiter.equals(aCacheType))


result = new Cache(CacheType.waiter.toString(), 1000, false, false, 900, 600);

//Cache black = new Cache(Const.CACHE_BLACK, 2000, MemoryStoreEvictionPolicy.LFU, false, null, true, 0, 0, false, 0, null);


return result;


}

private static Cache getCacheByType(CacheType aCacheType) {


Cache result = cacheManager.getCache(aCacheType.toString());


if (result==null)


result = createCacheByType(aCacheType);


return result;


}

public static boolean exist(CacheType aType, String aKey) {


boolean result = false;


if (aType != null && aKey != null) {


Cache cache = cacheManager.getCache(aType.toString());


if (cache!=null)


result = cache.isKeyInCache(aKey);


}


return result;


}

public static void put(CacheType aType, String aKey, Object aValue) {


if (aType != null && aKey != null) {


getCacheByType(aType).put(new Element(aKey, aValue));


}


}

public static void remove(CacheType aType, String aKey) {


getCacheByType(aType).remove(aKey);


}

public static Object get(CacheType aType, String aKey) {


Object result = null;


if (aType != null && aKey != null) {


try {


Element element = getCacheByType(aType).get(aKey);


if (element!=null)


result = element.getValue();


}


catch (Exception ex) {


ex.printStackTrace();


}


}


return result;


}

public static String getStrFromCache(CacheType aType, String aKey) {


String result = "";


Object temp = get(aType, aKey);


if (temp != null)


result = temp.toString();


return result;


}

public static void putOther(OtherType aKey, Object aValue) {


if (aKey != null) {


getCacheByType(CacheType.other).put(new Element(aKey.toString(), aValue));


}


}

public static void removeOther(OtherType aKey) {


getCacheByType(CacheType.other).remove(aKey.toString());


}

public static Object getOther(OtherType aType) {


Object result = null;


if (aType != null) {


try {


Element element = getCacheByType(CacheType.other).get(aType.toString());


if (element!=null)


result = element.getValue();


}


catch (Exception ex) {


ex.printStackTrace();


}


}


return result;


}

public static String getStrFromCacheOther(OtherType aKey) {


String result = "";


Object temp = getOther(aKey);


if (temp != null)


result = temp.toString();


return result;


}

public static void destroy() {


try {


CacheManager.getInstance().removalAll();


cacheManager = null;


}


catch (Exception ex){


ex.printStackTrace();


}


}

}

CacheHelper工具类的使用的更多相关文章

  1. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  2. Android—关于自定义对话框的工具类

    开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...

  3. [转]Java常用工具类集合

    转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...

  4. js常用工具类.

    一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...

  5. Guava库介绍之实用工具类

    作者:Jack47 转载请保留作者和原文出处 欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 本文是我写的Google开源的Java编程库Guava系列之一,主要介 ...

  6. Java程序员的日常—— Arrays工具类的使用

    这个类在日常的开发中,还是非常常用的.今天就总结一下Arrays工具类的常用方法.最常用的就是asList,sort,toStream,equals,copyOf了.另外可以深入学习下Arrays的排 ...

  7. .net使用正则表达式校验、匹配字符工具类

    开发程序离不开数据的校验,这里整理了一些数据的校验.匹配的方法: /// <summary> /// 字符(串)验证.匹配工具类 /// </summary> public c ...

  8. WebUtils-网络请求工具类

    网络请求工具类,大幅代码借鉴aplipay. using System; using System.Collections.Generic; using System.IO; using System ...

  9. JAVA 日期格式工具类DateUtil.java

    DateUtil.java package pers.kangxu.datautils.utils; import java.text.SimpleDateFormat; import java.ut ...

随机推荐

  1. CodeForces 446B DZY Loves Modification

    题意: k次操作  每次选择一行或一列  得到所选数字的和  并将所选数字同一时候减去p  问最多得到多少 思路: 重点在消除行列间的相互影响 因为每选一行全部列所相应的和都会-p  那么假设选了i次 ...

  2. QUrl不同版本之间的坑

    在项目中使用了native application + html的方式构建界面. 之前在4.8.4用QUrl直接加载相对路径一点问题都没有.但是切换到5.1编译之后却发现本地的html文件全部没有加载 ...

  3. JSP简单练习-使用JDOM创建xml文件

    注意:在编写代码前,请确保该Web文件夹下的"WEB-INF/lib"下包括jdom.jar包! <%@ page language="java" con ...

  4. Principle of Computing (Python)学习笔记(5) BFS Searching + Zombie Apocalypse

    1 Generators   Generator和list comprehension非常类似 Generators are a kind of iterator that are defined l ...

  5. 开发环境FAQ

    问: Win7下VS2008无法识别WinCE5.0SDK 答:拷贝WinXP环境(安装过VS2008 + WinCE5.0SDK)下C:/Program Files/Microsoft Visual ...

  6. poj 3778

    这就是个超级水题……!!!!写一写来纪念一下自己的错误…… 如果某个学生的的成绩是其他俩个或三个学生成绩的和则给予奖励 直接暴力,所以一开始直接用数组标记两个人或三个人的和,但是忽略了这种情况 20( ...

  7. LeetCode: LRU Cache [146]

    [题目] Design and implement a data structure for Least Recently Used (LRU) cache. It should support th ...

  8. 逻辑与(&&、&)和逻辑或(||、|)

    这四个逻辑运算符,大家都知道,但是有时候会凌乱,这里用程序来解释一下,以免忘了.(根据大家反应说:这文章没有解释清楚它们的区别.用法,其实文章主要说的是,如果将这四个运算符用于条件判断,那么它们会是什 ...

  9. asp.net 检查文件夹和文件是否存在

    原文  asp.net 检查文件夹和文件是否存在 允许 path 参数指定相对或绝对路径信息. 相对路径信息被解释为相对于当前工作目录. 检查该目录是否存在之前,从 path 参数的末尾移除尾随空格. ...

  10. Problem D: Flip Five

    大致题意:3 * 3的黑白格,在翻转的时候会本身和四周的都翻转,问最小翻转几次变成全部是白色解题思路:把3 * 3 = 9 个格子进行全排列,然后穷举然后找翻转的最小次数 #include <i ...