EhCache封装类

package com.leg3s.comm.utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * 类说明   缓存管理
 * @author 杨秋实
 * @version 创建时间:Dec 5, 2009 11:30:07 AM
 *
 */
public class AppCacheManager {
 
 public final static String confilename="sessionehcache.xml";
 private static CacheManager manager;
 private static AppCacheManager appManager = new AppCacheManager();
 
 protected static Logger logger = Logger.getLogger(AppCacheManager.class);
 
 static {
  String path = AppCacheManager.class.getResource("/").getPath()+confilename;
  CacheManager tmp = new CacheManager(path);
  appManager.setManager(tmp);
 }
 
// public static  AppCacheManager getInstance(){
//  
//  return appManager;
// }
 
 /**
  * 一般的应用可以用此方法,来保存对象
  *
  * @param key
  * @param value
  */
 public static void objAddToSession(CacheKey key, Object value, String cacheName) {
  insert(key, value, cacheName);
 }

 public static void objAddToSession(CacheKey key, Object value) {
  insert(key, value);
 }

 /**
  * 一般的应用可以用此方法,来获取对象
  *
  * @param key
  * @param value
  */
 public static Object getObjfromSession(CacheKey key, String cacheName) {
  return get(key, cacheName);
 }

 public static Object getObjfromSession(CacheKey key) {
  return get(key);
 }
 
 public static List<Object> getObjsfromSession(String cacheName) {
  List<Object> list=new ArrayList<Object>();
  int length=manager.getCache(cacheName).getKeys().size();
  for(int i=0;i<length;i++){
   Object object=get(manager.getCache(cacheName).getKeys().get(i));
   list.add(object);
  }
  return list;
 }

 /**
  * 特殊的应用可以用此方法,来保存对象
  *
  * @param key
  * @param value
  */
 public static void objAddToSession(Object key, Object value) {
  insert(key, value);
 }
 
 /**
  * 特殊的应用可以用此方法,来删除对象
  *
  * @param key
  * @param value
  */
 public static void objDelToSession(CacheKey key,String cacheName) {
    
  manager.getCache(cacheName).remove(key);


 }

 /**
  * 特殊的应用可以用此方法,来保存对象
  *
  * @param key
  * @param value
  */
 public static Object getObjfromSession(Object key) {
  return get(key);
 }
 
 /**
  * 默认存放在SessionCache中
  * @param key
  * @param value
  */
 private static void insert(Object key,Object value){
  insert(key, value,"sessionCache");
 }
 
 private static void insert(Object key,Object value,String cacheName){
  Element element=new Element(key,value);
  manager.getCache(cacheName).put(element);
  
 }
 /**
  * 查找2个都查
  * @param key
  * @return
  */
 private static Object get(Object key){
  Element element = manager.getCache("sessionCache").get(key);
  if(null==element){
   return null;
  }
  
  return element.getValue();
 }
 
 private static Object get(Object key,String cacheName){
  Element element=null;
  
  element = manager.getCache(cacheName).get(key);
  if(null==element){
   return null;
  }
  return element.getValue();
 }

 public static void addWithWriterBehind(Object key,Object value,String cacheName)
 {
  Element element=new Element(key,value);
  manager.getCache(cacheName).putWithWriter(element);
 }
 
 public void setManager(CacheManager manager) {
  this.manager = manager;
 }
 
 
}

 

public class CacheKey extends BaseObject{

 private String clazzName;
 private String fieldone;
 private String fieldtwo;
 
 private String fieldother;
 
 
 public CacheKey(String clazzName, String fieldone, String fieldtwo,
   String fieldother) {
  this.clazzName = clazzName;
  this.fieldone = fieldone;
  this.fieldtwo = fieldtwo;
  this.fieldother = fieldother;
 }

 public CacheKey(String clazzName, String fieldone, String fieldtwo) {
  this.clazzName = clazzName;
  this.fieldone = fieldone;
  this.fieldtwo = fieldtwo;
 }

 public CacheKey(String fieldone, String fieldtwo) {
  this.fieldone = fieldone;
  this.fieldtwo = fieldtwo;
 }

 public CacheKey(String fieldone) {
  this.fieldone = fieldone;
 }

 public String getClazzName() {
  return clazzName;
 }

 public void setClazzName(String clazzName) {
  this.clazzName = clazzName;
 }

 public String getFieldone() {
  return fieldone;
 }

 public void setFieldone(String fieldone) {
  this.fieldone = fieldone;
 }

 public String getFieldtwo() {
  return fieldtwo;
 }

 public void setFieldtwo(String fieldtwo) {
  this.fieldtwo = fieldtwo;
 }

 public String getFieldother() {
  return fieldother;
 }

 public void setFieldother(String fieldother) {
  this.fieldother = fieldother;
 }
 
 
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值