View Javadoc
1   package com.foxinmy.weixin4j.cache;
2   
3   import java.util.Map;
4   import java.util.concurrent.ConcurrentHashMap;
5   
6   /**
7    * 用内存保存缓存对象(不推荐使用)
8    *
9    * @className MemoryCacheStorager
10   * @author jinyu(foxinmy@gmail.com)
11   * @date 2016年1月24日
12   * @since JDK 1.6
13   * @see
14   */
15  public class MemoryCacheStorager<T extends Cacheable> implements
16  		CacheStorager<T> {
17  
18  	private final Map<String, T> CONMAP;
19  
20  	public MemoryCacheStorager() {
21  		this.CONMAP = new ConcurrentHashMap<String, T>();
22  	}
23  
24  	@Override
25  	public T lookup(String key) {
26  		T cache = this.CONMAP.get(key);
27  		if (cache != null) {
28  			if ((cache.getCreateTime() + cache.getExpires() - CUTMS) > System
29  					.currentTimeMillis()) {
30  				return cache;
31  			}
32  		}
33  		return null;
34  	}
35  
36  	@Override
37  	public void caching(String key, T cache) {
38  		this.CONMAP.put(key, cache);
39  	}
40  
41  	@Override
42  	public T evict(String key) {
43  		return this.CONMAP.remove(key);
44  	}
45  
46  	@Override
47  	public void clear() {
48  		this.CONMAP.clear();
49  	}
50  }