From 9d3afe6ccb9cabe5d0f8ce86dfbf81a82f989962 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期三, 27 三月 2024 11:00:20 +0800
Subject: [PATCH] 添加缓存设置

---
 src/main/java/com/yssh/utils/CacheUtils.java |   48 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 46 insertions(+), 2 deletions(-)

diff --git a/src/main/java/com/yssh/utils/CacheUtils.java b/src/main/java/com/yssh/utils/CacheUtils.java
index 10527b1..1292f87 100644
--- a/src/main/java/com/yssh/utils/CacheUtils.java
+++ b/src/main/java/com/yssh/utils/CacheUtils.java
@@ -4,6 +4,9 @@
 import com.github.benmanes.caffeine.cache.Caffeine;
 import org.checkerframework.checker.nullness.qual.NonNull;
 
+import java.math.BigInteger;
+import java.security.MessageDigest;
+import java.util.List;
 import java.util.concurrent.TimeUnit;
 
 public class CacheUtils {
@@ -12,8 +15,8 @@
     public static void init() {
         cache = Caffeine.newBuilder()
                 .initialCapacity(2)
-                .maximumSize(1024)
-                .expireAfterWrite(24, TimeUnit.HOURS)
+                .maximumSize(4096)
+                .expireAfterWrite(24 * 7, TimeUnit.HOURS)
                 .build();
     }
 
@@ -32,4 +35,45 @@
     public static void clear() {
         cache.invalidateAll();
     }
+
+    public static <T> List<T> getListByKey(String key) {
+        Object obj = get(key);
+        if (obj instanceof List<?>) {
+            return (List<T>) obj;
+        }
+
+        return null;
+    }
+
+    public static <T> void putListByKey(String key, List<T> list) {
+        if (null != list && list.size() > 0) {
+            put(key, list);
+        }
+    }
+
+    public static String getMd5(String str) {
+        if (StringUtils.isEmpty(str)) {
+            return null;
+        }
+
+        try {
+            MessageDigest md5 = MessageDigest.getInstance("MD5");
+            md5.update(str.getBytes());
+            byte[] byteArray = md5.digest();
+
+            BigInteger bigInt = new BigInteger(1, byteArray);
+
+            // 鍙傛暟16琛ㄧず16杩涘埗
+            String result = bigInt.toString(16);
+
+            // 涓嶈冻32浣嶉珮浣嶈ˉ闆�
+            while (result.length() < 32) {
+                result = "0" + result;
+            }
+
+            return result;
+        } catch (Exception e) {
+            return null;
+        }
+    }
 }

--
Gitblit v1.9.3