From 13b31b2ca2a5f8600002a042b536c9d5529842e3 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期一, 09 三月 2026 19:21:18 +0800
Subject: [PATCH] #
---
src/main/java/com/core/common/Cache.java | 61 ++++++++++++++++++++++++++++++
1 files changed, 61 insertions(+), 0 deletions(-)
diff --git a/src/main/java/com/core/common/Cache.java b/src/main/java/com/core/common/Cache.java
new file mode 100644
index 0000000..2fef707
--- /dev/null
+++ b/src/main/java/com/core/common/Cache.java
@@ -0,0 +1,61 @@
+package com.core.common;
+
+import com.core.exception.ApplicationException;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Supplier;
+
+public class Cache {
+
+ private Map<String, Object> caches = new ConcurrentHashMap<>();
+
+ public <T> T get(Class<T> clazz) {
+ return (T) get(clazz.getName());
+ }
+
+ public Object get(String key) {
+ return get(key, true);
+ }
+
+ public <T> T get(String key, Supplier<T> supplier) {
+ if (!hasKey(key)) {
+ put(key, supplier.get());
+ }
+ return (T) get(key);
+ }
+
+ public Object get(String key, boolean require) {
+ if (require && !hasKey(key)) {
+ throw new ApplicationException(this + "-鎵句笉鍒扮紦瀛樺璞�:" + key);
+ }
+ return caches.get(key);
+ }
+
+ public Cache put(Object value) {
+ String key = value.getClass().getName();
+ put(key, value);
+ return this;
+ }
+
+ public Cache put(String key, Object value) {
+ put(key, value, true);
+ return this;
+ }
+
+ public Cache put(String key, Object value, boolean requireNotExists) {
+ if (requireNotExists && hasKey(key)) {
+ throw new ApplicationException(this + "-缂撳瓨" + key + "宸插瓨鍦�");
+ }
+ caches.put(key, value);
+ return this;
+ }
+
+ public boolean hasKey(Class<?> clazz) {
+ return hasKey(clazz.getName());
+ }
+
+ public boolean hasKey(String key) {
+ return caches.containsKey(key);
+ }
+}
--
Gitblit v1.9.1