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

📄 oscache.java

📁 这是一款最新的野蔷薇论坛源码,有需要的朋友可以尽情下载
💻 JAVA
字号:
/* 
 * Created on 2006-2-25
 * Last modified on 2006-10-28
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.club.cache;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.opensymphony.oscache.base.NeedsRefreshException;
import com.opensymphony.oscache.general.GeneralCacheAdministrator;
/**
 * @author <a href="mailto:m.bogaert@intrasoft.be">Mathias Bogaert</a>
 */
public class OSCache implements Cache {
	
	private static final Logger logger = Logger.getLogger(OSCache.class.getName());

	static GeneralCacheAdministrator cacheadmin;

	private final int refreshPeriod;
	private final String cron;
	private final String regionName;
	
	private String toString(Object key) {
		return String.valueOf(key) + '.' + regionName;
	}
	
	/**
	 * 
	 * @param refreshPeriod //更新时间
	 * @param cron
	 * @param region
	 */
	public OSCache(int refreshPeriod, String cron, String region) {
		this.refreshPeriod = refreshPeriod;
		this.cron = cron;
		this.regionName = region;
		if(cacheadmin==null){
			logger.info("Cache region is "+region);
			cacheadmin = new GeneralCacheAdministrator();
			//cacheadmin.setCacheCapacity(10000);
		}
	}
	
	/**
	 * 设置内存容量
	 * @param cacheCapacity
	 */
	public void setCacheCapacity(int cacheCapacity) {
		cacheadmin.setCacheCapacity(cacheCapacity);
	}

	public Object get(Object key){
		try {
			Object obj = cacheadmin.getFromCache(toString(key), refreshPeriod, cron );
			if(obj==null){
				CacheUtil.cacheMisses++;
			}else{
				CacheUtil.cacheHits++;
			}
			CacheUtil.cacheGets++;
			return obj;
		}
		catch (NeedsRefreshException e) {
			cacheadmin.cancelUpdate( toString(key) );
			return null;
		}
	}

	public Object read(Object key){
		return get(key);
	}
	
	public void update(Object key, Object value) {
		put(key, value);
	}
	
	public void put(Object key, Object value)  {
		CacheUtil.cachePuts++;
		cacheadmin.putInCache( toString(key), value );
	}

	public void remove(Object key)  {
		cacheadmin.flushEntry(toString(key));
	}

	public void clear()  {
		cacheadmin.flushAll();
	}

	public void destroy()  {
		cacheadmin.destroy();
	}

	public void lock(Object key)  {
		// local cache, so we use synchronization
	}

	public void unlock(Object key)  {
		// local cache, so we use synchronization
	}

	public long nextTimestamp() {
		return Timestamper.next();
	}

	public int getTimeout() {
		return Timestamper.ONE_MS * 60000; //ie. 60 seconds
	}

	public String getRegionName() {
		return regionName;
	}

	public long getMemoryStoreSize() {
		return -1;
	}

	public int getElementCountInMemory() {
		return -1;
	}

	public int getElementCountOnDisk() {
		return -1;
	}

	public Map toMap() {
		throw new UnsupportedOperationException();
	}

	public String toString() {
		return "OSCache(" + regionName + ')';
	}

	public HashMap getAll() {
		return null;
	}

	public long getDiskStoreSize() {
		return 0;
	}

	public int getElementTotal() {
		return 0;
	}

}

⌨️ 快捷键说明

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