CacheHelper工具类的使用
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工具类的使用的更多相关文章
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- Android—关于自定义对话框的工具类
开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...
- [转]Java常用工具类集合
转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...
- js常用工具类.
一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...
- Guava库介绍之实用工具类
作者:Jack47 转载请保留作者和原文出处 欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 本文是我写的Google开源的Java编程库Guava系列之一,主要介 ...
- Java程序员的日常—— Arrays工具类的使用
这个类在日常的开发中,还是非常常用的.今天就总结一下Arrays工具类的常用方法.最常用的就是asList,sort,toStream,equals,copyOf了.另外可以深入学习下Arrays的排 ...
- .net使用正则表达式校验、匹配字符工具类
开发程序离不开数据的校验,这里整理了一些数据的校验.匹配的方法: /// <summary> /// 字符(串)验证.匹配工具类 /// </summary> public c ...
- WebUtils-网络请求工具类
网络请求工具类,大幅代码借鉴aplipay. using System; using System.Collections.Generic; using System.IO; using System ...
- JAVA 日期格式工具类DateUtil.java
DateUtil.java package pers.kangxu.datautils.utils; import java.text.SimpleDateFormat; import java.ut ...
随机推荐
- B树的实现与源代码二(删除源代码)
int BTreeMaximum( BNode *x ) { if ( x->leaf ) { return x->key[x->size - 1]; } else { return ...
- document.documentElement 和document.body 以及其属性
js中document.documentElement 和document.body 以及其属性 (原来HTML里是document.body --XHTML里是document.documentE ...
- 全方位深度剖析--性能测试之LoardRunner 介绍
一.介绍 LoardRunner是一种预测系统行为和性能负载的测试工具.通过模拟上千万用户实施并发负载及实时性能监控的方式来确认和查找系统的瓶颈,LoardRunner能够对整个企业架构进行测试.通过 ...
- Android KitKat 4.4 Wifi移植之Wifi driver
本文讲述在Linux 3.10下Realek RTL8723A Linux Wifi 驱动的移植. Prerequisites 硬件平台:Atmel SAMA5 软件平台:Linux 3.10 + A ...
- Python pip 安装包
Python 第三方包可以使用pip 更容易地安装,和管理 pip 的下载地址 https://pypi.python.org/pypi/pip/ pip的使用以及相关文档参考 https://pip ...
- 在程序异常中记录堆栈信息(使用ExWatcher)
在我们编写程序的时候可通过IDE自带的调试环境捕捉到异常(Except)错误,并能查看到相关的信息以便我们修正程序中的问题.但当软件被发布出去后,因为所部署运行的环境与我们的调试环境有很大区别,即使在 ...
- metasploit学习之ms03_026
傻瓜式利用ms03_026_dcom: Matching Modules ================ Name Disclosure Date Rank Description ---- --- ...
- 医院API免费接口的公布
医院通网(http://hospital.yi18.net) 站点上站快两个月了,基本已经稳定,尽管还有非常多小bug,但还是不影响大局.抱着数据开放和共享的理念,医院大全API接口 (http:// ...
- DotNetBar.Bar控制Y顺序控制方向
DotNetBar.Bar控件Y方向上的顺序控制 老帅 控件DevComponents.DotNetBar.Bar是能够有多种用途的.能够作为容器,也能够作为工具条,不管做什么,在Y方向上 ...
- 框架基础JNI
转载请标明出处: http://blog.csdn.net/yujun411522/article/details/46342793 本文出自:[yujun411522的博客] 2.1 概述 JNI ...