广州市综治平台后端
xusd
2025-06-07 36306491396230522fa20585c2621a7fc899849a
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
package cn.huge.base.common.utils;
 
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.springframework.util.ObjectUtils;
 
import java.util.concurrent.TimeUnit;
 
/**
 * 本地缓存工具类
 * @author zhouxiantao
 * @create 2024-09-05 10:51
 */
public class GuavaCacheUtils {
    private static Cache<String, Object> cache = CacheBuilder.newBuilder()
            //5秒后过期
            .expireAfterWrite(5, TimeUnit.SECONDS)
//            .expireAfterWrite(10, TimeUnit.SECONDS)
            //最大10个缓存
            .maximumSize(10)
            .build();
 
    private static Cache<String, Object> cacheAi = CacheBuilder.newBuilder()
            //5秒后过期
            .expireAfterWrite(5, TimeUnit.MINUTES)
//            .expireAfterWrite(10, TimeUnit.SECONDS)
            //最大10个缓存
            .maximumSize(500)
            .build();
 
    public static Object getCache(String k) {
        return cache.getIfPresent(k);
    }
 
    public static void putCache(String k, Object value) {
        cache.put(k, value);
    }
 
    public static Object getCacheAi(String k) {
        return cacheAi.getIfPresent(k);
    }
 
    public static void putCacheAi(String k, Object value) {
        cacheAi.put(k, value);
    }
 
    public static void main(String[] args) throws InterruptedException {
        putCache("15008985673","123456");
        while (true){
            Thread.sleep(1000);
            Object cache = getCache("15008985673");
            System.out.println(cache);
            if(ObjectUtils.isEmpty(cache)){
                System.out.println("结束。。。");
                return;
            }
        }
    }
 
}