packagedemo.shaw.common;importjava.util.LinkedHashMap;importjava.util.Map;importjava.util.concurrent.ScheduledExecutorService;importjava.util.concurrent.ScheduledThreadPoolExecutor;importjava.util.concurrent.ThreadFactory;importjava.util.concurrent.TimeUnit;importjava.util.concurrent.atomic.AtomicInteger;importjava.util.concurrent.locks.Lock;importjava.util.concurrent.locks.ReadWriteLock;importjava.util.concurrent.locks.ReentrantReadWriteLock;public classLocalCache {private static final Mapmap;private static final ScheduledExecutorService timerService; //定时器
/*** 默认有效时长*/
private static final long DEFAULT_TIMEOUT = 3600;private static final long SECOND_TIME = 1000;/*** 初始化块总是在构造器之前执行
* 静态初始化块执行的优先级高于非静态初始化块,在对象装载到JVM中时执行一次,仅能初始化类成员变量,即static修饰的数据成员
* 静态初始化块是类相关的,系统将在类加载时执行静态初始化块,而不是在创建对象时才执行,因此静态初始化块总是比非静态初始化块先执行*/
static{
map= new LRUMap<>();
timerService= new ScheduledThreadPoolExecutor(1, newLocalCache.DaemonThreadFactory());
}/*** 工具类*/
privateLocalCache() {
}static class LRUMap extends LinkedHashMap{/*** 默认缓存大小*/
private static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;/*** 默认最大缓存大小*/
private static final int DEFAULT_MAX_CAPACITY = 1 << 30;/*** 默认加载因子*/
private static final float DEFAULT_LOAD_FACTOR = 0.75f;/*** 读写锁*/
private ReadWriteLock readWriteLock = newReentrantReadWriteLock();private final Lock rLock =readWriteLock.readLock();private final Lock wLock =readWriteLock.writeLock();publicLRUMap() {super(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
}public LRUMap(intinitialCapacity) {super(initialCapacity, DEFAULT_LOAD_FACTOR);
}/*** 需要重写LinkedHashMap中removeEldestEntry方法;
* 新增元素的时候,会判断当前map大小是否超过DEFAULT_MAX_CAPACITY,超过则移除map中最老的节点;
*@parameldest
*@return
*/
protected boolean removeEldestEntry(Map.Entryeldest) {return size() >DEFAULT_MAX_CAPACITY;
}publicV put(K k, V v) {
wLock.lock();try{return super.put(k, v);
}finally{
wLock.unlock();
}
}publicV get(String k) {
rLock.lock();try{return super.get(k);
}finally{
rLock.unlock();
}
}public void putAll(Map extends K, ? extends V>m) {
wLock.lock();try{super.putAll(m);
}finally{
wLock.unlock();
}
}publicV remove(Object k) {
wLock.lock();try{return super.remove(k);
}finally{
wLock.unlock();
}
}public booleancontainKey(K k) {
rLock.lock();try{return super.containsKey(k);
}finally{
rLock.unlock();
}
}public intsize() {
rLock.lock();try{return super.size();
}finally{
rLock.unlock();
}
}public voidclear() {
wLock.lock();try{super.clear();
}finally{
wLock.unlock();
}
}
}/*** 清除缓存的任务类*/
static class CleanWorkerTask implementsRunnable {privateString key;publicCleanWorkerTask(String key) {this.key =key;
}
@Overridepublic voidrun() {
LocalCache.remove(key);
}
}private static final class DaemonThreadFactory implementsThreadFactory {private AtomicInteger atomicInteger = new AtomicInteger(0);
@OverridepublicThread newThread(Runnable runnable) {
Thread thread= newThread(runnable);
thread.setName("schedule-pool-Thread-" +atomicInteger.getAndIncrement());
thread.setDaemon(true);return null;
}
}/*** 增加缓存*/
public static voidadd(String key, Object value) {
map.put(key, value);
timerService.schedule(newCleanWorkerTask(key), DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
}/*** 增加缓存
*@paramtimeout 有效时长*/
public static void add(String key, Object value, inttimeout) {
map.put(key, value);
timerService.schedule(new CleanWorkerTask(key), timeout *SECOND_TIME, TimeUnit.MILLISECONDS);
}public static void putAll(Map m, inttimeout) {
map.putAll(m);for(String key : m.keySet()) {
timerService.schedule(new CleanWorkerTask(key), timeout *SECOND_TIME, TimeUnit.MILLISECONDS);
}
}/*** 获取缓存*/
public staticObject get(String key) {returnmap.get(key);
}public static booleancontainsKey(String key) {returnmap.containsKey(key);
}/***
*@paramkey*/
public static voidremove(String key) {
map.remove(key);
}public static intsize() {returnmap.size();
}
}