From 69e3afc7707d467b758858b52d3784947f7a502b Mon Sep 17 00:00:00 2001
From: 疯狂的狮子Li <15040126243@163.com>
Date: 星期一, 20 五月 2024 10:25:23 +0800
Subject: [PATCH] !538 ♥️发布 5.2.0-BETA 公测版本 Merge pull request !538 from 疯狂的狮子Li/dev

---
 ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/manager/CaffeineCacheDecorator.java |   90 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 90 insertions(+), 0 deletions(-)

diff --git a/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/manager/CaffeineCacheDecorator.java b/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/manager/CaffeineCacheDecorator.java
new file mode 100644
index 0000000..ee1d405
--- /dev/null
+++ b/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/manager/CaffeineCacheDecorator.java
@@ -0,0 +1,90 @@
+package org.dromara.common.redis.manager;
+
+import org.dromara.common.core.utils.SpringUtils;
+import org.springframework.cache.Cache;
+
+import java.util.concurrent.Callable;
+
+/**
+ * Cache 瑁呴グ鍣ㄦā寮�(鐢ㄤ簬鎵╁睍 Caffeine 涓�绾х紦瀛�)
+ *
+ * @author LionLi
+ */
+public class CaffeineCacheDecorator implements Cache {
+
+    private static final com.github.benmanes.caffeine.cache.Cache<Object, Object>
+        CAFFEINE = SpringUtils.getBean("caffeine");
+
+    private final Cache cache;
+
+    public CaffeineCacheDecorator(Cache cache) {
+        this.cache = cache;
+    }
+
+    @Override
+    public String getName() {
+        return cache.getName();
+    }
+
+    @Override
+    public Object getNativeCache() {
+        return cache.getNativeCache();
+    }
+
+    public String getUniqueKey(Object key) {
+        return cache.getName() + ":" + key;
+    }
+
+    @Override
+    public ValueWrapper get(Object key) {
+        Object o = CAFFEINE.get(getUniqueKey(key), k -> cache.get(key));
+        return (ValueWrapper) o;
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T> T get(Object key, Class<T> type) {
+        Object o = CAFFEINE.get(getUniqueKey(key), k -> cache.get(key, type));
+        return (T) o;
+    }
+
+    @Override
+    public void put(Object key, Object value) {
+        CAFFEINE.invalidate(getUniqueKey(key));
+        cache.put(key, value);
+    }
+
+    public ValueWrapper putIfAbsent(Object key, Object value) {
+        CAFFEINE.invalidate(getUniqueKey(key));
+        return cache.putIfAbsent(key, value);
+    }
+
+    @Override
+    public void evict(Object key) {
+        evictIfPresent(key);
+    }
+
+    public boolean evictIfPresent(Object key) {
+        boolean b = cache.evictIfPresent(key);
+        if (b) {
+            CAFFEINE.invalidate(getUniqueKey(key));
+        }
+        return b;
+    }
+
+    @Override
+    public void clear() {
+        cache.clear();
+    }
+
+    public boolean invalidate() {
+        return cache.invalidate();
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public <T> T get(Object key, Callable<T> valueLoader) {
+        Object o = CAFFEINE.get(getUniqueKey(key), k -> cache.get(key, valueLoader));
+        return (T) o;
+    }
+
+}

--
Gitblit v1.9.3