You need to sign in or sign up before continuing.
Commit ac0a1aa2 by zhiwei

添加缓存机制

parent 3f8e90d7
package com.zhiwei.searchhotcrawler.cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zhiwei.zhiweiTools.tools.ZhiWeiTools;
public class CacheListener {
Logger logger = LoggerFactory.getLogger(CacheListener.class);
public void startListen() {
new Thread(){
public void run() {
while (true) {
if(CacheManager.caches!=null && CacheManager.caches.size()>0){
for(String key : CacheManager.getAllKeys()) {
if (CacheManager.isTimeOut(key)) {
CacheManager.clearByKey(key);
logger.info(key + "缓存被清除");
}
}
}
ZhiWeiTools.sleep(500);
}
}
}.start();
}
}
package com.zhiwei.searchhotcrawler.cache;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class CacheManager {
public static Map<String, EntityCache> caches = new ConcurrentHashMap<String, EntityCache>();
/**
* 存入缓存
* @param key
* @param cache
*/
public static void putCache(String key, EntityCache cache) {
caches.put(key, cache);
}
/**
* 存入缓存
* @param key
* @param cache
*/
public static void putCache(String key, Object datas, long timeOut) {
timeOut = timeOut > 0 ? timeOut : 0L;
putCache(key, new EntityCache(datas, timeOut, System.currentTimeMillis()));
}
/**
* 获取对应缓存
* @param key
* @return
*/
public static EntityCache getCacheByKey(String key) {
if (isContains(key)) {
return caches.get(key);
}
return null;
}
/**
* 获取对应缓存
* @param key
* @return
*/
public static Object getCacheDataByKey(String key) {
if (isContains(key)) {
return caches.get(key).getDatas();
}
return null;
}
/**
* 获取所有缓存
* @param key
* @return
*/
public static Map<String, EntityCache> getCacheAll() {
return caches;
}
/**
* 判断是否在缓存中
* @param key
* @return
*/
public static boolean isContains(String key) {
return caches.containsKey(key);
}
/**
* 清除所有缓存
*/
public static void clearAll() {
caches.clear();
}
/**
* 清除对应缓存
* @param key
*/
public static void clearByKey(String key) {
if (isContains(key)) {
caches.remove(key);
}
}
/**
* 缓存是否超时失效
* @param key
* @return
*/
public static boolean isTimeOut(String key) {
if (!caches.containsKey(key)) {
return true;
}
EntityCache cache = caches.get(key);
long timeOut = cache.getTimeOut();
long lastRefreshTime = cache.getLastRefeshTime();
if (timeOut == 0 || System.currentTimeMillis() - lastRefreshTime >= timeOut) {
return true;
}
return false;
}
/**
* 获取所有key
* @return
*/
public static Set<String> getAllKeys() {
return caches.keySet();
}
}
package com.zhiwei.searchhotcrawler.cache;
public class EntityCache {
/**
* 保存的数据
*/
private Object datas;
/**
* 设置数据失效时间,为0表示永不失效
*/
private long timeOut;
/**
* 最后刷新时间
*/
private long lastRefeshTime;
public EntityCache(Object datas, long timeOut, long lastRefeshTime) {
this.datas = datas;
this.timeOut = timeOut;
this.lastRefeshTime = lastRefeshTime;
}
public Object getDatas() {
return datas;
}
public void setDatas(Object datas) {
this.datas = datas;
}
public long getTimeOut() {
return timeOut;
}
public void setTimeOut(long timeOut) {
this.timeOut = timeOut;
}
public long getLastRefeshTime() {
return lastRefeshTime;
}
public void setLastRefeshTime(long lastRefeshTime) {
this.lastRefeshTime = lastRefeshTime;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment