cache策略(三)基类

cache策略(三)基类
SimpleCache.java如下:
package com.sillycat.easyview.plugin.cache.base;

import com.sillycat.easyview.plugin.commons.exceptions.CacheException;

/**
* 一个最简单的CACHE的接口,适合只读型CACHE使用
*/
public interface SimpleCache {
Object get(Object key) throws CacheException;

void put(Object key, Object value) throws CacheException;
}

工具类Timestamper.java 这个类估计也是从hibernate的源码里面搞出来的
package com.sillycat.easyview.plugin.cache.base;

public class Timestamper {
private static short counter = 0;
private static long time;
private static final int BIN_DIGITS = 12;
public static final short ONE_MS = 1<<BIN_DIGITS;

public static long next() {
synchronized(Timestamper.class) {
long newTime = System.currentTimeMillis() << BIN_DIGITS;
if (time<newTime) {
time = newTime;
counter = 0;
}
else if (counter < ONE_MS - 1 ) {
counter++;
}

return time + counter;
}
}

private Timestamper() {}
}


ReadOnlyCache.java如下:
package com.sillycat.easyview.plugin.cache.base;

import java.util.Comparator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sillycat.easyview.plugin.commons.exceptions.CacheException;

public class ReadOnlyCache implements BaseCache, SimpleCache {

private static final Log log = LogFactory.getLog(ReadOnlyCache.class);

private Cache cache;

public Object get(Object key, long txTimestamp) throws CacheException {
Object result = cache.get(key);
if (result != null && log.isDebugEnabled()) {
log.debug("Cache hit: " + key);
}
return result;
}

public boolean put(Object key, Object value, long txTimestamp,
Object version, Comparator versionComparator, boolean minimalPut)
throws CacheException {
if (minimalPut && (cache.get(key) != null)) {
if (log.isDebugEnabled()) {
log.debug("item already cached: " + key);
}
return false;
}
if (log.isDebugEnabled()) {
log.debug("Caching: " + key);
}
cache.put(key, value);
return true;
}

public SoftLock lock(Object key, Object version) throws CacheException {
log.error("Application attempted to edit read only item: " + key);
throw new UnsupportedOperationException(
"Can't write to a readonly object");
}

public void evict(Object key) throws CacheException {
}

public boolean update(Object key, Object value) throws CacheException {
log.error("Application attempted to edit read only item: " + key);
throw new UnsupportedOperationException(
"Can't write to a readonly object");
}

public boolean insert(Object key, Object value) throws CacheException {
return false;
}

public void release(Object key, SoftLock lock) throws CacheException {
log.error("Application attempted to edit read only item: " + key);
}

public boolean afterUpdate(Object key, Object value, Object version,
SoftLock lock) throws CacheException {
log.error("Application attempted to edit read only item: " + key);
throw new UnsupportedOperationException(
"Can't write to a readonly object");
}

public boolean afterInsert(Object key, Object value, Object version)
throws CacheException {
if (log.isDebugEnabled())
log.debug("Caching after insert: " + key);
cache.update(key, value);
return true;
}

public void remove(Object key) throws CacheException {
cache.remove(key);
}

public void clear() throws CacheException {
this.cache.clear();
}

public void destroy() {
try {
cache.destroy();
} catch (CacheException e) {
log.warn("could not destroy cache", e);
}
}

public void setCache(Cache cache) {
this.cache = cache;
}

public String getRegionName() {
return cache.getRegionName();
}

public Cache getCache() {
return this.cache;
}

public Object get(Object key) throws CacheException {
return cache.get(key);
}

public void put(Object key, Object value) throws CacheException {
cache.put(key, value);

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值