|
@@ -1,19 +1,19 @@
|
|
package com.qmth.jkserver.core.cache;
|
|
package com.qmth.jkserver.core.cache;
|
|
|
|
|
|
-import java.util.HashMap;
|
|
|
|
-import java.util.HashSet;
|
|
|
|
-import java.util.Set;
|
|
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
-
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.stereotype.Repository;
|
|
import org.springframework.stereotype.Repository;
|
|
|
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.HashSet;
|
|
|
|
+import java.util.Set;
|
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
+
|
|
@Repository("memoryCacheDao")
|
|
@Repository("memoryCacheDao")
|
|
public class MemoryCacheDao {
|
|
public class MemoryCacheDao {
|
|
-
|
|
|
|
- private static final Logger LOGGER = LoggerFactory.getLogger(MemoryCacheDao.class);
|
|
|
|
|
|
+
|
|
|
|
+ private static final Logger LOGGER = LoggerFactory.getLogger(MemoryCacheDao.class);
|
|
|
|
|
|
private static ConcurrentHashMap<String, Long> cacheTime = new ConcurrentHashMap<String, Long>();// 缓存到期时间
|
|
private static ConcurrentHashMap<String, Long> cacheTime = new ConcurrentHashMap<String, Long>();// 缓存到期时间
|
|
|
|
|
|
@@ -24,32 +24,31 @@ public class MemoryCacheDao {
|
|
private static ConcurrentHashMap<String, String> stringCache = new ConcurrentHashMap<String, String>();// 缓存
|
|
private static ConcurrentHashMap<String, String> stringCache = new ConcurrentHashMap<String, String>();// 缓存
|
|
|
|
|
|
//清理过期的缓存
|
|
//清理过期的缓存
|
|
- public static void chean(){
|
|
|
|
- Set<String> expiredKeys = new HashSet<>();//已经过期的key
|
|
|
|
- for (String key : cacheTime.keySet()) {
|
|
|
|
- if (cacheTime.get(key) != null && cacheTime.get(key) < System.currentTimeMillis()) {
|
|
|
|
- expiredKeys.add(key);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- for (String key : expiredKeys) {
|
|
|
|
- if(key.indexOf("_")>-1){
|
|
|
|
- for (String m: objectMapCache.keySet()) {
|
|
|
|
- for (String field: objectMapCache.get(m).keySet()) {
|
|
|
|
- if((key+"_"+field).equals(key)){
|
|
|
|
- objectMapCache.remove(key);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- else{
|
|
|
|
- cacheTime.remove(key);
|
|
|
|
- objectCache.remove(key);
|
|
|
|
- stringCache.remove(key);
|
|
|
|
- }
|
|
|
|
- LOGGER.info("---成功清理缓存,KEY="+key+"----------------------------------------");
|
|
|
|
- }
|
|
|
|
|
|
+ public static void chean() {
|
|
|
|
+ Set<String> expiredKeys = new HashSet<>();//已经过期的key
|
|
|
|
+ for (String key : cacheTime.keySet()) {
|
|
|
|
+ if (cacheTime.get(key) != null && cacheTime.get(key) < System.currentTimeMillis()) {
|
|
|
|
+ expiredKeys.add(key);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ for (String key : expiredKeys) {
|
|
|
|
+ if (key.indexOf("_") > -1) {
|
|
|
|
+ for (String m : objectMapCache.keySet()) {
|
|
|
|
+ for (String field : objectMapCache.get(m).keySet()) {
|
|
|
|
+ if ((key + "_" + field).equals(key)) {
|
|
|
|
+ objectMapCache.remove(key);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ cacheTime.remove(key);
|
|
|
|
+ objectCache.remove(key);
|
|
|
|
+ stringCache.remove(key);
|
|
|
|
+ }
|
|
|
|
+ LOGGER.info("---成功清理缓存,KEY=" + key + "----------------------------------------");
|
|
|
|
+ }
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
public String getStr(String key) {
|
|
public String getStr(String key) {
|
|
// 缓存过期
|
|
// 缓存过期
|
|
if (cacheTime.get(key) != null && cacheTime.get(key) < System.currentTimeMillis()) {
|
|
if (cacheTime.get(key) != null && cacheTime.get(key) < System.currentTimeMillis()) {
|
|
@@ -78,24 +77,24 @@ public class MemoryCacheDao {
|
|
|
|
|
|
public Object getObj(String key, String field) {
|
|
public Object getObj(String key, String field) {
|
|
// 缓存过期
|
|
// 缓存过期
|
|
- if(StringUtils.isNotEmpty(field)){
|
|
|
|
- if (cacheTime.get(key + "_" + field) != null && cacheTime.get(key + "_" + field) < System.currentTimeMillis()) {
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
- if (objectMapCache.containsKey(key)) {
|
|
|
|
- return objectMapCache.get(key).get(field);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- else{
|
|
|
|
- return objectMapCache.get(key);
|
|
|
|
- }
|
|
|
|
|
|
+ if (StringUtils.isNotEmpty(field)) {
|
|
|
|
+ if (cacheTime.get(key + "_" + field) != null && cacheTime.get(key + "_" + field) < System.currentTimeMillis()) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ if (objectMapCache.containsKey(key)) {
|
|
|
|
+ return objectMapCache.get(key).get(field);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ return objectMapCache.get(key);
|
|
|
|
+ }
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
public boolean setObj(String key, Object value, int expireTime) { //单位是秒
|
|
public boolean setObj(String key, Object value, int expireTime) { //单位是秒
|
|
// 如果为-1,存储一年
|
|
// 如果为-1,存储一年
|
|
- if (expireTime == -1)
|
|
|
|
|
|
+ if (expireTime == -1) {
|
|
expireTime = 360 * 24 * 60 * 60;
|
|
expireTime = 360 * 24 * 60 * 60;
|
|
|
|
+ }
|
|
objectCache.put(key, value);
|
|
objectCache.put(key, value);
|
|
cacheTime.put(key, System.currentTimeMillis() + expireTime * 1000);
|
|
cacheTime.put(key, System.currentTimeMillis() + expireTime * 1000);
|
|
return true;
|
|
return true;
|