package com.yeejoin.amos.fas.business.util;

import com.yeejoin.amos.fas.business.vo.Toke;

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class CacheMap {
	// 缓存Map
	ConcurrentHashMap<String, CacheValue> cacheMap = new ConcurrentHashMap<String, CacheValue>();

	// 保证不能被外界 构造
	CacheMap() {
	}

	public void setex(String key, Toke value, long times) {
		CacheValue cValue = new CacheValue();
		cValue.setTimes(System.currentTimeMillis() + times * 1000);
		cValue.setValue(value);
		cacheMap.put(key, cValue);
	}

	public Toke getValue(String key) {
		CacheValue cValue = cacheMap.get(key);
		if (cValue != null && cValue.getTimes() > System.currentTimeMillis()) {
			return cValue.getValue();
		} else if (cValue != null && cacheMap.containsKey(key)) {
			// 清理一次
			cacheMap.remove(key);
		}
		return null;
	}

	/**
	 * 获取key集合
	 * 
	 * @return
	 */
	public Set getKeys() {
		return cacheMap.keySet();
	}
}
