|
|
@@ -1,7 +1,10 @@
|
|
|
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.function.Function;
|
|
|
@@ -10,30 +13,64 @@ import java.util.function.Function;
|
|
|
/**
|
|
|
* @author Administrator
|
|
|
*/
|
|
|
-public class CacheOnce<T> {
|
|
|
+public class CacheOnce<V> {
|
|
|
private final Map<String, Object> map = new HashMap<>();
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- T get(String name) {
|
|
|
+ public V get(String name) {
|
|
|
if (map.containsKey(name)) {
|
|
|
- return (T) map.get(name);
|
|
|
+ return (V) map.get(name);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- T get(String name, Function<String, Object> filter) {
|
|
|
+ public V get(String name, Function<String, Object> filter) {
|
|
|
+ Objects.requireNonNull(filter);
|
|
|
+
|
|
|
+ if (map.containsKey(name)) {
|
|
|
+ return (V) map.get(name);
|
|
|
+ }
|
|
|
+ Object object = filter.apply(name);
|
|
|
+ if (object == null) {
|
|
|
+ return null;
|
|
|
+ } else {
|
|
|
+ map.put(name, object);
|
|
|
+ return (V) (object);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public <T> T getObject(String name, Function<String, Object> filter, Class<T> tClass) {
|
|
|
+ Objects.requireNonNull(filter);
|
|
|
+
|
|
|
+ if (map.containsKey(name)) {
|
|
|
+ String oldOb = JSON.toJSONString(map.get(name));
|
|
|
+ return JSON.parseObject(oldOb, tClass);
|
|
|
+ }
|
|
|
+ Object object = filter.apply(name);
|
|
|
+ if (object == null) {
|
|
|
+ return null;
|
|
|
+ } else {
|
|
|
+ map.put(name, object);
|
|
|
+ String oldOb = JSON.toJSONString(object);
|
|
|
+ return JSON.parseObject(oldOb, tClass);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public <T> List<T> getList(String name, Function<String, Object> filter, Class<T> tClass) {
|
|
|
Objects.requireNonNull(filter);
|
|
|
|
|
|
if (map.containsKey(name)) {
|
|
|
- return (T) map.get(name);
|
|
|
+ String oldOb = JSON.toJSONString(map.get(name));
|
|
|
+ return JSON.parseArray(oldOb, tClass);
|
|
|
}
|
|
|
Object object = filter.apply(name);
|
|
|
if (object == null) {
|
|
|
return null;
|
|
|
} else {
|
|
|
map.put(name, object);
|
|
|
- return (T) (object);
|
|
|
+ String oldOb = JSON.toJSONString(object);
|
|
|
+ return JSON.parseArray(oldOb, tClass);
|
|
|
}
|
|
|
}
|
|
|
|