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