公司項目個人身份票據(jù)驗證不唯一,原想用cookie解決艺谆,但是移動端沒辦法存儲cookie,然后自己寫了一個緩存仅政,不多BB松蒜,直接上代碼。
/**
*
* @author 黑暗料理界扛把子
*
* @Description 緩存實體類 這里自己隨意定義成員變量
*/
public class Cache implements Comparable<Cache>{
private String key;//緩存ID
private String ip;//ip
private String ticket;//票據(jù)
private long timeOut;//過期時間
public Cache() {
super();
}
public String getKey() {
return key;
}
public long getTimeOut() {
return timeOut;
}
public Cache(String key, String ip, String ticket, long timeOut) {
super();
this.key = key;
this.ip = ip;
this.ticket = ticket;
this.timeOut = timeOut;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getTicket() {
return ticket;
}
public void setTicket(String ticket) {
this.ticket = ticket;
}
public void setKey(String string) {
key = string;
}
public void setTimeOut(long l) {
timeOut = l;
}
@Override
public int compareTo(Cache o) {
long r = this.timeOut - o.timeOut;
if (r > 0) {
return 1;
}
if (r < 0) {
return -1;
}
return 0;
}
}
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.log4j.Logger;
/**
*
* @author 黑暗料理界扛把子
*
* @Description 緩存實現(xiàn)類
*/
@SuppressWarnings("all")
public class CacheManager {
private static ScheduledExecutorService swapExpiredPool = new ScheduledThreadPoolExecutor(10);
private ConcurrentHashMap<String, Cache> cacheMap = new ConcurrentHashMap<String, Cache>();
private PriorityQueue<Cache> expireQueue = new PriorityQueue<>(1024);
private Lock lock = new ReentrantLock();
private static final CacheManager cm=new CacheManager();
Logger logger=Logger.getLogger(CacheManager.class);
private CacheManager() {
swapExpiredPool.scheduleWithFixedDelay(new TimeOutCacheTask(), 5, 5, TimeUnit.SECONDS);
}
public static CacheManager cm() {
return cm;
}
//獲取緩存
public Cache getCache(String key){
return cacheMap.get(key);
}
//設置緩存
public void putCache(String key,Cache cache){
lock.lock();
try {
if (hasCache(key)) {
expireQueue.remove(cache);
}
expireQueue.add(cache);
cacheMap.put(key, cache);
}finally {
lock.unlock();
}
}
//判斷是否存在一個緩存
private boolean hasCache(String key) {
return cacheMap.containsKey(key);
}
//清除所有緩存
public void clearAll() {
lock.lock();
try {
cacheMap.clear();
expireQueue.clear();
} finally {
lock.unlock();
}
}
//清除指定的緩存
public void removeCache(String key) {
lock.lock();
try {
if (hasCache(key)) {
cacheMap.remove(key);
expireQueue.remove(getCache(key));
}
} finally {
lock.unlock();
}
}
//獲取緩存中的大小
public int getCacheSize() {
return cacheMap.size();
}
//重寫載入緩存信息方法
public void setCache(String key,String ip,String ticket){
Cache cache = new Cache();
cache.setKey(key);
cache.setTimeOut(getTimeInMillis());
cache.setIp(ip);
cache.setTicket(ticket);
putCache(key,cache);
}
//獲取緩存對象中的所有鍵值名稱
public ArrayList getCaches() {
ArrayList cacheList = new ArrayList();
try {
Iterator i = cacheMap.entrySet().iterator();
while (i.hasNext()) {
java.util.Map.Entry entry = (java.util.Map.Entry) i.next();
cacheList.add((String) entry.getKey());
}
} catch (Exception ex) {} finally {
return cacheList;
}
}
//過期時間設置為10天后
private long getTimeInMillis(){
Calendar c = Calendar.getInstance();
c.set(Calendar.DATE,c.get(Calendar.DATE)+10);
return c.getTimeInMillis();
}
//刪除已經(jīng)過期的數(shù)據(jù)
private class TimeOutCacheTask implements Runnable {
@Override
public void run() {
long now = System.currentTimeMillis();
while (true) {
lock.lock();
try {
Cache chche = expireQueue.peek();
//沒有數(shù)據(jù)了已旧,或者數(shù)據(jù)都是沒有過期的了
if (chche == null || chche.getTimeOut() > now) {
return;
}
cacheMap.remove(chche.getKey());
expireQueue.poll(); //移除并返回頭部元素
} finally {
lock.unlock();
}
}
}
}
}
采用了單例模式秸苗,緩存操作都加了鎖,代碼通俗易懂运褪,就不多BB了惊楼。