JAVA中缓存的实现

JAVA中缓存的实现             2010-04-23 10:39
package com.tq.pubsecurity.cache;
import java.io.Serializable;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @description 缓存管理
*/
public class CacheManageProxy {
private static final Log logger = LogFactory.getLog(CacheManageProxy.class);
private Cache cache = null;
private CacheManager cacheManager;
public CacheManageProxy(CacheManager cacheManager) {
   this.cacheManager = cacheManager;
   this.cache = cacheManager.getCache("longTimeCache");
}
   

/**
* 缓存存储结构
* @param {Class} 类名
* @param {String} 方法名
* @param {String} 参数集字符串
* @param {Serializable} 序列化结果集
* @return null
* */

public void putValue(Class cls, String method, String key,
    Serializable value) {
   String cacheKey = new StringBuffer(cls.toString()).append(":").append(
     method).append(":").append(key).toString();
   Element element = new Element(cacheKey, value);
   logger.debug("放入cache:" + cacheKey);
   this.cache.put(element);
}

/**
* 取出缓存
* @param {Class} 类名
* @param {String} 方法名
* @param {String} 参数集字符串
* @return {Serializable} 序列化结果集
* */
public Serializable getValue(Class cls, String method, String key) {
   try {
    String cacheKey = new StringBuffer(cls.toString()).append(":")
      .append(method).append(":").append(key).toString();
    Element element = this.cache.get(cacheKey);
    if (element == null)
     return null;
    logger.debug("从cache中取:" + cacheKey);
    return element.getValue();
   } catch (Exception e) {
    return null;
   }
}

    //清空所有缓存
public void clearAllCache() {
   String[] cache = cacheManager.getCacheNames();
   for (int i = 0; i < cache.length; i++) {
    try {
     cacheManager.getCache(cache[i]).removeAll();
    } catch (Exception e) {
    }
   }
}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值