对比新文件 |
| | |
| | | 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 && 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); |
| | | |
| | | String result = bigInt.toString(16); |
| | | |
| | | while (result.length() < 32) { |
| | | result = "0" + result; |
| | | } |
| | | |
| | | return result; |
| | | } catch (Exception e) { |
| | | return null; |
| | | } |
| | | } |
| | | } |