From 9a86656c139eef8963bcf449a8985ef9bcb1299c Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期一, 18 十一月 2024 17:57:05 +0800 Subject: [PATCH] 1 --- src/main/java/com/yssh/utils/CacheUtils.java | 77 ++++++++++++++++++++++++++++++++++++++ 1 files changed, 77 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/yssh/utils/CacheUtils.java b/src/main/java/com/yssh/utils/CacheUtils.java new file mode 100644 index 0000000..fd04690 --- /dev/null +++ b/src/main/java/com/yssh/utils/CacheUtils.java @@ -0,0 +1,77 @@ +package com.yssh.utils; + +import com.github.benmanes.caffeine.cache.Cache; +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 { + private static @NonNull Cache<String, Object> cache; + + public static void init() { + cache = Caffeine.newBuilder() + .initialCapacity(2) + .maximumSize(4096) + .expireAfterWrite(24 * 7, TimeUnit.HOURS) + .build(); + } + + public static Object get(String key) { + return cache.getIfPresent(key); + } + + public static void put(String key, Object obj) { + cache.put(key, obj); + } + + public static void remove(String key) { + cache.invalidate(key); + } + + 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) { + 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); + + String result = bigInt.toString(16); + + while (result.length() < 32) { + result = "0" + result; + } + + return result; + } catch (Exception e) { + return null; + } + } +} -- Gitblit v1.9.3