读书人

cache策略(六)hash地图

发布时间: 2012-11-07 09:56:10 作者: rapoo

cache策略(六)hashmap
cache策略(六)hashmap

这个东东还要多测试测试。按照这个接口写出来的。呵呵。

HashMapCache.java 如下:
package com.sillycat.easyview.plugin.cache.hashmap;

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

import com.sillycat.easyview.plugin.cache.base.Cache;
import com.sillycat.easyview.plugin.cache.base.Timestamper;
import com.sillycat.easyview.plugin.commons.exceptions.CacheException;

/**
* CACHE的内容直接放到HASHMap之中
*/
public class HashMapCache implements Cache {

private final Map hashMap = new HashMap();

private final String regionName;

public HashMapCache(String regionName) {
this.regionName = regionName;
}

public String getRegionName() {
return regionName;
}

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

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

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

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

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

public void clear() throws CacheException {
hashMap.clear();
}

public void destroy() throws CacheException {
hashMap.clear();
}

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

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

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

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

public long getSizeInMemory() {
return -1;
}

public long getElementCountInMemory() {
return hashMap.size();
}

public long getElementCountOnDisk() {
return 0;
}

public Map toMap() {
return Collections.unmodifiableMap(hashMap);
}

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

}

HashMapCacheProvider.java 如下:
package com.sillycat.easyview.plugin.cache.hashmap;

import java.util.Properties;

import com.sillycat.easyview.plugin.cache.base.Cache;
import com.sillycat.easyview.plugin.cache.base.CacheProvider;
import com.sillycat.easyview.plugin.cache.base.Timestamper;
import com.sillycat.easyview.plugin.commons.exceptions.CacheException;

public class HashMapCacheProvider implements CacheProvider {

public Cache buildCache(String regionName)
throws CacheException {
return new HashMapCache(regionName);
}

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

public void start() throws CacheException {
}

public void stop() {
}

public boolean isMinimalPutsEnabledByDefault() {
return false;
}

public Properties getProperties() throws CacheException {
return null;
}

}

读书人网 >软件架构设计

热点推荐