📄 easycache.java
字号:
package com.easyjf.cache;
import com.easyjf.cache.store.*;
import org.apache.log4j.*;
/**
* Easy
*/
import java.util.*;
/**
*
* <p>
* Title: EasyCache核心引擎
* </p>
*
* <p>
* Description: EasyCache实现ICache接口,提供具体的Cache功能
* </p>
*
* <p>
* Copyright: Copyright (c) 2006
* </p>
*
* <p>
* Company:EasyJF开源团队-EasyDBO项目组
* </p>
*
* @author 大峡
* @version 1.0
*/
public class EasyCache implements ICache, Cloneable {
public static final String DEFAULT_CACHE_NAME = "default";
private final static Logger logger = Logger.getLogger(EasyCache.class);
private static final int DEFAULT_STORE_POLICY = StorePolicy.LRU;
private static final int DEFAULT_MAX_ELEMENTS = 100;
private static final long DEFAULT_EXPIRY_SECONDS = 120;
private static final String NULL_KEY="EASYDBOCACHE";
// ~--- fields -------------------------------------------------------------
private final long expiredInterval; // 超时间隔,秒
private int hitCount; // 命中次数
private final int maxElements; // 最大元素
private int missCountNotFound; // 查找失败次数
private String name; // 缓存名称
private int status; // 状态
private Store store; // 缓存数据存诸
private int storePolicy; // 存储策略
// ~--- constructors -------------------------------------------------------
public EasyCache() {
this(DEFAULT_CACHE_NAME, DEFAULT_STORE_POLICY, DEFAULT_MAX_ELEMENTS,
DEFAULT_EXPIRY_SECONDS * 1000);
}
/**
*
* @param name
* String
* @param storePolicy
* int
* @param maxElements
* int
* @param expiredInterval
* long
*/
public EasyCache(String name, int storePolicy, int maxElements,
long expiredInterval) {
this.storePolicy = storePolicy;
this.maxElements = maxElements;
this.expiredInterval = expiredInterval * 1000 ;
this.name = name;
this.store = MemoryStore.create(this);
}
// ~--- methods ------------------------------------------------------------
/**
* 清除缓存中所有元素,但保留缓存在可用状态
*
* @throws CacheException
* 缓存异常
*/
public void clear() throws CacheException {
try {
store.removeAll();
} catch (Exception e) {
throw new CacheException(e);
}
}
/**
*
* @return Object
* @throws CloneNotSupportedException
*/
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
/**
* 清除缓存,并使之不可用
*
* @throws CacheException
* 缓存异常
*/
public void destroy() throws CacheException {
try {
store.dispose();
// CacheManager.getInstance().removeCache(this.name);
} catch (Exception e) {
throw new CacheException(e);
}
}
/**
* 未实现方法 Calls to this method should perform there own synchronization. It
* is provided for distributed caches. Because EHCache is not distributed
* this method does nothing.
*
* @param key
* 元素键值
* @throws CacheException
* 缓存异常
*/
public void lock(Object key) throws CacheException {
// TODO Auto-generated method stub
}
/**
* 放置对象到缓存中
*
* @param key
* 可序列化的键值
* @param value
* 可序列化的元素值
* @throws CacheException
* 如果参数不可序列化,CacheManager停止或者出现其他异常
*/
public void put(Object key, Object value) throws CacheException {
try {
Element element = new Element(key, value);
store.put(element);
} catch (Exception e) {
throw new CacheException(e);
}
}
/**
* 清除匹配键值的元素值
* <p>
* 如果没有元素匹配,则没有清除动作且不抛出任何异常
*
* @param key
* 需要清除的元素键值
* @throws CacheException
* 缓存异常
*/
public void remove(Object key) throws CacheException {
try {
store.remove(key);
} catch (Exception e) {
throw new CacheException(e);
}
}
public void removeElement(Object value) {
try {
store.removeElement(new Element(NULL_KEY,value));
} catch (Exception e) {
logger.error("从缓存中删除无素出现错误:" + e);
}
}
/**
* 未实现方法 Calls to this method should perform there own synchronization. It
* is provided for distributed caches. Because EHCache is not distributed
* this method does nothing.
*
* @param key
* 元素键值
* @throws CacheException
* 缓存异常
*/
public void unlock(Object key) throws CacheException {
// TODO Auto-generated method stub
}
// ~--- get methods --------------------------------------------------------
/**
* 获得匹配指定键值的元素值
*
* @param key
* 返回元素的键值
* @return 之前放入缓存的元素值,如果找不到或者过期则为null
* @throws CacheException
* 缓存异常
*/
public Object get(Object key) throws CacheException {
try {
if (key == null) {
return null;
} else {
Element element = store.get(key);
if (element == null) {
logger.debug("缓存元素 for " + key + " is null");
missCountNotFound++;
return null;
} else {
hitCount++;
return element.getValue();
}
}
} catch (Exception e) {
throw new CacheException(e);
}
}
/**
* 获得内存容纳元素的最大数量
*
* @return 内存容纳元素的最大数量
*/
public int getElementCount() {
// TODO Auto-generated method stub
return store.getSize();
}
/**
*
* @return long
*/
public long getExpiredInterval() {
return expiredInterval;
}
/**
*
* @return Set
*/
public Set getKeySet() {
// TODO Auto-generated method stub
return store.getKeySet();
}
/**
*
* @return int
*/
public int getMaxElemnetCount() {
// TODO Auto-generated method stub
return this.maxElements;
}
/**
*
* @return String
*/
public String getName() {
// TODO Auto-generated method stub
return this.name;
}
/**
* 获得内存中元素数量
*
* @return 内存中元素数量,如果不明异常返回-1
*/
public long getSize() {
try {
return store.getSize();
} catch (Throwable t) {
return -1;
}
}
/**
*
* @return int
*/
public int getStatus() {
return status;
}
/**
*
* @return int
*/
public int getStorePolicy() {
// TODO Auto-generated method stub
return this.storePolicy;
}
/**
*
* @param e
* Element
* @return boolean
*/
public boolean isExpired(Element e) {
return (System.currentTimeMillis() -(e.getLastAccessTime()==0?e.getCreationTime():e.getLastAccessTime())) > expiredInterval;
}
// ~--- set methods --------------------------------------------------------
public void setName(String name) {
this.name = name;
}
public void setStorePolicy(int storePolicy) {
// TODO Auto-generated method stub
this.storePolicy=storePolicy;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -