⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 easycache.java

📁 简易java框架开源论坛系统,简 易java框架开源论坛系统
💻 JAVA
字号:
package com.easyjf.cache;

/**
 * Easy
 */
import java.util.Set;
import com.easyjf.cache.store.*;
import org.apache.log4j.Logger;

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 long DEFAULT_EXPIRY_SECONDS = 120;	
	private static final int DEFAULT_STORE_POLICY = StorePolicy.LRU;
	private static final int DEFAULT_MAX_ELEMENTS=100;
	private String name;//缓存名称
	private int status;	//状态 
	private int storePolicy;//存储策略
	private final int maxElements;//最大元素
	private final long expiredInterval;//超时间隔,秒
	private int hitCount;//命中次数
	private int missCountNotFound;	//查找失败次数
	private Store store;	//缓存数据存诸	
	public EasyCache()
	{
		this(DEFAULT_CACHE_NAME,DEFAULT_STORE_POLICY,DEFAULT_MAX_ELEMENTS,DEFAULT_EXPIRY_SECONDS*1000);
	}
	
	public EasyCache(String name,int storePolicy,int maxElements,long expiredInterval) {		
		this.storePolicy=storePolicy;
		this.maxElements=maxElements;
		this.expiredInterval=expiredInterval*1000*60;
		this.name=name;		
		this.store=MemoryStore.create(this);
	}	
	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);
		}
	}

	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);
		}
	}

	public void remove(Object key) throws CacheException {
		try {
			store.remove(key);
		} catch (Exception e) {
			throw new CacheException(e);
		}

	}

	public void clear() throws CacheException {
		try {
			store.removeAll();
		} catch (Exception e) {
			throw new CacheException(e);
		}
	}

	public void destroy() throws CacheException {
		try {
			store.dispose();
			//CacheManager.getInstance().removeCache(this.name);	
			
		} catch (Exception e) {
			throw new CacheException(e);
		}
	}

	public void lock(Object key) throws CacheException {
		// TODO Auto-generated method stub

	}

	public void unlock(Object key) throws CacheException {
		// TODO Auto-generated method stub

	}
	
	public long getSize() {
		try {
			return store.getSize();
		} catch (Throwable t) {
			return -1;
		}
	}
	
	public int getStatus() {
		return status;
	}

	public long getExpiredInterval() {
		return expiredInterval;
	}

	public int getElementCount() {
		// TODO Auto-generated method stub
		return store.getSize();
	}

	public int getMaxElemnetCount() {
		// TODO Auto-generated method stub
		return this.maxElements;
	}

	public String getName() {
		// TODO Auto-generated method stub
		return this.name;
	}

	public void setName(String name) {
		this.name=name;
	}

	public int getStorePolicy() {
		// TODO Auto-generated method stub
		return this.storePolicy;
	}
	public boolean isExpired(Element e) {
		// TODO Auto-generated method stub
		return (System.currentTimeMillis()-e.getLastAccessTime())>expiredInterval;
	}
	public Object clone() throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		return super.clone();
	}
	
	public Set getKeySet() {
		// TODO Auto-generated method stub
		return store.getKeySet();
	}

	public void removeElement(Object value)  {
		try{
		store.removeElement(value);
		}
		catch(Exception e)
		{
			logger.error("从缓存中删除无素出现错误:"+e);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -