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

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值