燕山石化溯源三维电子沙盘-【后端】-服务
1
13693261870
2024-11-18 9a86656c139eef8963bcf449a8985ef9bcb1299c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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;
        }
    }
}