六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 20|回复: 0

cache策略(六)hashmap

[复制链接]

升级  34.8%

548

主题

548

主题

548

主题

探花

Rank: 6Rank: 6

积分
1696
 楼主| 发表于 2013-2-3 14:19:24 | 显示全部楼层 |阅读模式
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;
}

}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表