diff --git a/doc/nginx.conf b/doc/nginx.conf index b77d0bd..9ea83f3 100644 --- a/doc/nginx.conf +++ b/doc/nginx.conf @@ -2,8 +2,6 @@ #user nobody; worker_processes 1; - - events { worker_connections 1024; } diff --git a/lidee-server/src/main/java/top/lidee/taie/business/cache/ReportCacheHelper.java b/lidee-server/src/main/java/top/lidee/taie/business/cache/ReportCacheHelper.java new file mode 100644 index 0000000..6cfdabf --- /dev/null +++ b/lidee-server/src/main/java/top/lidee/taie/business/cache/ReportCacheHelper.java @@ -0,0 +1,150 @@ +package top.lidee.taie.business.cache; + + +import top.lidee.taie.cache.CacheHelper; +import com.google.common.collect.Maps; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.Cache; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class ReportCacheHelper implements CacheHelper, ApplicationContextAware { + + @Autowired + private Cache cache; + + @Override + public String stringGet(String key) { + Cache.ValueWrapper valueWrapper = cache.get(key); + if (valueWrapper != null) { + return (String) valueWrapper.get(); + } + return CacheHelper.super.stringGet(key); + } + + @Override + public Boolean setIfAbsent(String key, String value) { + cache.putIfAbsent(key, value); + return true; + } + + + @Override + public boolean exist(String key) { + String cacheHoldTime = stringGet(key + "_HoldTime"); + if (cacheHoldTime != null && Long.parseLong(cacheHoldTime) > 0) { + if (Long.parseLong(cacheHoldTime) < System.currentTimeMillis()) { + delete(key + "_HoldTime"); + delete(key); + return false; + } + } + return cache.get(key) != null; + } + + + @Override + public void stringSet(String key, String value) { + cache.put(key, value); + } + + + @Override + public String regKey(String key) { + return CacheHelper.super.regKey(key); + } + + @Override + public void stringSetExpire(String key, String value, long seconds) { + stringSet(key, value); + if (seconds > 0) { + //缓存失效时间 + stringSet(key + "_HoldTime", String.valueOf(System.currentTimeMillis() + seconds * 1000)); + } + } + + @Override + public Map hashGet(String key) { + Cache.ValueWrapper t = cache.get(key); + if (t != null) { + return (Map) t.get(); + } + return Maps.newHashMap(); + } + + @Override + public String hashGetString(String key, String hashKey) { + Map stringStringMap = hashGet(key); + return stringStringMap.get(hashKey); + } + + @Override + public void hashDel(String key, String hashKey) { + Map stringStringMap = hashGet(key); + stringStringMap.remove(hashKey); + } + + @Override + public void hashBatchDel(String key, Set hashKeys) { + Map stringStringMap = hashGet(key); + hashKeys.forEach(stringStringMap::remove); + } + + @Override + public boolean hashExist(String key, String hashKey) { + if (exist(key)) { + Map map = hashGet(key); + return map.containsKey(hashKey); + } + return false; + } + + @Override + public boolean hashAnyExist(String key, String[] hashKeys) { + return CacheHelper.super.hashAnyExist(key, hashKeys); + } + + @Override + public void hashSet(String key, String hashKey, String hashValue) { + Map map; + if (exist(key)) { + map = hashGet(key); + } else { + map = new HashMap<>(); + } + map.put(hashKey, hashValue); + hashSet(key, map); + } + + @Override + public void hashSet(String key, Map hash) { + cache.put(key, hash); + } + + @Override + public boolean delete(String key) { + if (exist(key)) { + cache.evict(key); + } + return true; + } + + @Override + public boolean delete(List keys) { + keys.forEach(this::delete); + return true; + } + + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + /*基于内存的本地缓存*/ + cache = (Cache) applicationContext.getBean("ehCacheCache"); + } +}