package com.se.system.utils;
|
|
import com.github.benmanes.caffeine.cache.Cache;
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
import com.se.system.SeSystemApplication;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Value;
|
|
import java.math.BigInteger;
|
import java.security.MessageDigest;
|
import java.util.List;
|
import java.util.concurrent.TimeUnit;
|
|
@SuppressWarnings("ALL")
|
public class CaffeineUtils {
|
static Integer cacheTime;
|
|
@Value("${sys.cacheTime}")
|
public void setCacheTime(Integer cacheTime) {
|
CaffeineUtils.cacheTime = cacheTime;
|
}
|
|
private static Cache<String, Object> cache;
|
|
private static final Logger log = LoggerFactory.getLogger(CaffeineUtils.class);
|
|
public static void init() {
|
cache = Caffeine.newBuilder()
|
.initialCapacity(16)
|
.maximumSize(4096)
|
.expireAfterWrite(60 * 60 * 8, TimeUnit.SECONDS)
|
.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;
|
}
|
}
|
}
|