一般cache的做法

其实比较简单,直接来个例子,e.g.

TryCache.java

public class InforoneCache<K, V> implements Cache<K, V> {

	class CacheObject<K2, V2> {
		final K2 key;
		final V2 value;
		long lastModified; // 最后添加时间
		long expire; // 对象存活时间

		CacheObject(K2 key, V2 value, long expire) {
			this.key = key;
			this.value = value;
			this.lastModified = System.currentTimeMillis();
			this.expire = expire;
		}

		boolean isExpired() {
			if (expire == 0) {
				return false;
			}
			return lastModified + expire < System.currentTimeMillis();
		}

		V2 getObject() {
			return value;
		}
	}

	protected Map<K, CacheObject<K, V>> cacheMap;

	private final ReentrantReadWriteLock cacheLock = new ReentrantReadWriteLock();
	private final Lock readLock = cacheLock.readLock();
	private final Lock writeLock = cacheLock.writeLock();

	protected int cacheSize; // 缓存大小 , 0 -> 无限制

	protected boolean existCustomExpire; // 是否设置默认过期时间

	public int getCacheSize() {
		return cacheSize;
	}

	protected long defaultExpire; // 默认过期时间, 0 -> 永不过期

	public TryCache(int cacheSize, long defaultExpire) {
		this.cacheSize = cacheSize;
		this.defaultExpire = defaultExpire;
		cacheMap = new HashMap<K, CacheObject<K, V>>(cacheSize + 1);
	}

	public long getDefaultExpire() {
		return defaultExpire;
	}

	public void put(K key, V value) {
		put(key, value, defaultExpire);
	}

	public void put(K key, V value, long expire) {
		writeLock.lock();

		try {
			CacheObject<K, V> co = new CacheObject<K, V>(key, value, expire);
			if (expire != 0) {
				existCustomExpire = true;
			}
			cacheMap.put(key, co);
		} finally {
			writeLock.unlock();
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public V get(K key) {
		readLock.lock();

		try {
			CacheObject<K, V> co = cacheMap.get(key);
			if (co == null) {
				return null;
			}
			if (co.isExpired() == true) {
				cacheMap.remove(key);
				return null;
			}

			return co.getObject();
		} finally {
			readLock.unlock();
		}
	}

	public boolean isFull() {
		if (cacheSize == 0) {// o -> 无限制
			return false;
		}
		return cacheMap.size() >= cacheSize;
	}

	public void remove(K key) {
		writeLock.lock();
		try {
			cacheMap.remove(key);
		} finally {
			writeLock.unlock();
		}
	}

	public void clear() {
		writeLock.lock();
		try {
			cacheMap.clear();
		} finally {
			writeLock.unlock();
		}
	}

	public int size() {
		return cacheMap.size();
	}

	public boolean isEmpty() {
		return size() == 0;
	}

}

GetData.java

	static final InforoneCache<String, DatumMining> cache = new InforoneCache<String, DatumMining>(0, 30 * 60 * 1000L);
public getData() {
		Xxx xxx = cache.get(key);
		if (DatumMining == null) {
...
		}
		cache.put(key, xxx);
}


跨节点情况请参考:一致性hash算法: cache、负载均衡应用

http://blog.csdn.net/textboy/article/details/46004117

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值