package com.se.nsl.helper; import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; import lombok.extern.slf4j.Slf4j; import java.math.BigInteger; import java.security.MessageDigest; import java.util.List; import java.util.concurrent.TimeUnit; @Slf4j @SuppressWarnings("ALL") public class CaffeineHelper { private static Cache cache; public static void init(Integer cacheTime) { cache = Caffeine.newBuilder() .initialCapacity(16) .maximumSize(4096) .expireAfterWrite(cacheTime, TimeUnit.MINUTES) .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 List getListByKey(String key) { Object obj = get(key); if (obj instanceof List) { return (List) obj; } return null; } public static void putListByKey(String key, List list) { if (null != list && list.size() > 0) { put(key, list); } } public static String getMd5(String str) { if (StringHelper.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; } } }