|
@@ -3,10 +3,7 @@ package thyyxxk.webserver.utils;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Objects;
|
|
|
+import java.util.*;
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
|
|
@@ -15,6 +12,7 @@ import java.util.function.Function;
|
|
|
*/
|
|
|
public class CacheOnce<V> {
|
|
|
private final Map<String, Object> map = new HashMap<>();
|
|
|
+ private final Set<String> notQueried = new HashSet<>();
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
public V get(String name) {
|
|
@@ -31,8 +29,14 @@ public class CacheOnce<V> {
|
|
|
if (map.containsKey(name)) {
|
|
|
return (V) map.get(name);
|
|
|
}
|
|
|
+
|
|
|
+ if (notQueried.contains(name)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
Object object = filter.apply(name);
|
|
|
if (object == null) {
|
|
|
+ notQueried.add(name);
|
|
|
return null;
|
|
|
} else {
|
|
|
map.put(name, object);
|
|
@@ -47,8 +51,14 @@ public class CacheOnce<V> {
|
|
|
String oldOb = JSON.toJSONString(map.get(name));
|
|
|
return JSON.parseObject(oldOb, tClass);
|
|
|
}
|
|
|
+
|
|
|
+ if (notQueried.contains(name)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
Object object = filter.apply(name);
|
|
|
if (object == null) {
|
|
|
+ notQueried.add(name);
|
|
|
return null;
|
|
|
} else {
|
|
|
map.put(name, object);
|
|
@@ -64,8 +74,14 @@ public class CacheOnce<V> {
|
|
|
String oldOb = JSON.toJSONString(map.get(name));
|
|
|
return JSON.parseArray(oldOb, tClass);
|
|
|
}
|
|
|
+
|
|
|
+ if (notQueried.contains(name)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
Object object = filter.apply(name);
|
|
|
if (object == null) {
|
|
|
+ notQueried.add(name);
|
|
|
return null;
|
|
|
} else {
|
|
|
map.put(name, object);
|
|
@@ -74,5 +90,4 @@ public class CacheOnce<V> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
}
|