ConcurrentHashMap 模拟缓存,增加过期机制(惰性+定期删除)

import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class ConcurrentMapCache {

	/**
	 * 缓存对象
	 */
	private ConcurrentMap<String, CacheObject> cache = new ConcurrentHashMap<String, CacheObject>();

	/**
	 * 定时删除器
	 */
	private Timer deleteTimer = null;

	/**
	 * 默认删除时间(秒)
	 */
	private static final int DEFAULT_DELETE_SECONDS = 5 * 60;

	public ConcurrentMapCache(int deleteSeconds) {
		this.initTimer(deleteSeconds);
	}

	public ConcurrentMapCache() {
		this.initTimer(0);
	}

	/**
	 * 初始化定时器
	 * 
	 * @param deleteSeconds
	 */
	private void initTimer(int deleteSeconds) {

		if (deleteSeconds == 0)
			deleteSeconds = DEFAULT_DELETE_SECONDS;

		deleteTimer = new Timer();
		deleteTimer.schedule(new DeleteTask(), 0, deleteSeconds * 1000);
	}

	/**
	 * 设置缓存
	 * 
	 * @param key
	 * @param value
	 * @param expire 过期时间毫秒为单位
	 */
	public void set(String key, Object value, long expire) {
		
		// ObjectUtils.isAnyEmpty 参数中只要存在empty即返回true
		if ( ObjectUtils.isAnyEmpty(key , expire) || ( expire < 0L && expire != -1 ) )
			return ;
		
		CacheObject cacheObject = new CacheObject(value, expire == -1L ? expire : System.currentTimeMillis() + expire);

		this.cache.put(key, cacheObject);
	}

	/**
	 * 设置过期时间
	 * @param key
	 * @param expire -1L长期有效
	 */
	public void put(String key,long expire) {
		
		if ( ObjectUtils.isAnyEmpty(key , expire) || ( expire < 0L && expire != -1 ) )
			return ;
		
		CacheObject cacheObject = this.cache.get(key);
		cacheObject.setTtl(expire == -1L ? expire : System.currentTimeMillis() + expire);
	}

	/**
	 * 设置缓存
	 * 
	 * @param key
	 * @param value
	 */
	public void set(String key, Object value) {

		this.set(key, value, -1L);

	}

	/**
	 * 获取缓存
	 */
	public Object get(String key) {

		if (this.check(key)) {
			return this.cache.get(key).getValue();
		}
		return null;
	}

	/**
	 * 删除某个缓存
	 */
	public void delete(String key) {
		
		this.cache.remove(key);
		
	}
	
	

	/**
	 * 判断缓存是否有效(过期、不存在均返回false)
	 */
	private boolean check(String key) {

		CacheObject cacheObject = this.cache.get(key);
		
		if (cacheObject == null)
			return false;
		
		if (cacheObject.getTtl() == -1L)
			return true;
		
		if (cacheObject.getTtl() < System.currentTimeMillis()) {
			delete(key);
			return false;
		}
		
		return true;
	}

	/**
	 * 定时删除任务
	 */
	class DeleteTask extends TimerTask {

		@Override
		public void run() {
			
			// 循环Map中数据,找到已经过期的数据进行删除
		    for(Map.Entry<String, CacheObject> entry : cache.entrySet()) {
		    	
		    	if (entry.getValue().getTtl() < System.currentTimeMillis() && entry.getValue().getTtl() != -1L) {
		    		delete(entry.getKey());
		    	}
		    }
			
		}
	}
}

CacheObject

package com.itc.core.cache;

/**
 * 缓存对象
 * @author ss
 */
public class CacheObject {

	/**
	 * 缓存对象
	 */
	private Object value;
	
	/**
	 * 生存时间
	 */
	private long ttl;

	/**
	 * 构造函数
	 * 
	 * @param value 缓存对象
	 * @param ttl 生存时间毫秒
	 */
	public CacheObject(Object value, long ttl) {
		this.value = value;
		this.ttl = ttl;
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	public long getTtl() {
		return ttl;
	}

	public void setTtl(long ttl) {
		this.ttl = ttl;
	}
}
  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值