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. android studio 报错,google后无果

    你可能在环境变量中配置了adk的环境变量,同时eclipse可studio公用一个avd,在两个之间切换时过出错

  2. VC动态轨迹画线

    分类: 2.4 线程/图形学2010-04-30 22:14 1878人阅读 评论(0) 收藏 举报 文档null 这是一个绘制直线的简单绘图程序,能过实现动态轨迹画线,在拖动时产生临时线来表示可能画 ...

  3. Leetcode: Median of Two Sorted Arrays. java.

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  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. Server Tomcat v7.0 Server at localhost was unable

    在eclipse启动tomcat时遇到超时45秒的问题: Server Tomcat v7.0 Server at localhost was unable to start within 45 se ...

  6. 微软vs2015先行,Visual Studio 2015正式版离线iso及在线下载(附key)附带百度云盘地址

    win10正式版发布之前我们迎来了vs2015正式版,迫不及待要下载朋友可以看看 Visual Studio Community 2015简体中文版(社区版,针对个人免费): 在线安装exe:http ...

  7. OpenSSL---堆栈

    堆栈是一种先进后出的数据结构.是一种只允许在其一端进行插入或者删除的线性表.允许插入或删除操作的一端为栈顶,另一端称为栈底.对堆栈的插入和删除操作称为入栈和出栈. 1.1     概述 OpenSSL ...

  8. OnClick事件的Sender参数的前世今生——TWinControl.WinProc优先捕捉到鼠标消息,然后使用IsControlMouseMsg函数进行消息转发给图形子控件(意外发现OnClick是由WM_LBUTTONUP触发的)

    这是一个再普通不过的Button1Click执行体: procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage('I am B ...

  9. Spring中的p标签(转)good

    Spring的p标签是基于XML Schema的配置方式,目的是为了简化配置方式. 在XML文件头部添加xmlns:p="http://www.springframework.org/sch ...

  10. JENKINS 打包发布脚本

    #!/bin/bash #nohup bash check_new_pkgs_dev.sh & #steps below: ##发布的机器上运行这个脚本 #定时遍历发布包存放路径 #1.遍历所 ...