peter
2025-11-26 566a1b9fda0276e2cc4a35f7ba322c0e599a2c84
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.nq.utils;
 
 
import java.util.*;
import java.util.concurrent.*;
 
 
/**
 * @author wangbingchen
 * @Description
 * @create 2021-11-23 14:12
 * 简易的缓存工具,用于做短信验证码校验
 * 此类为常驻内存工具
 */
public class CacheUtil {
 
    private final static ExecutorService EXECUTOR_SERVICE=new ThreadPoolExecutor(2,2,30, TimeUnit.SECONDS,new ArrayBlockingQueue<>(1000));
 
    public static void main(String[] args) throws InterruptedException {
        CacheUtil.set("name","zhangsan",2000);
        CacheUtil.set("name","lisi",2000);
        CacheUtil.set("name","ww",500);
        CacheUtil.set("name1","qweqwe",4000);
        Thread.sleep(1000);
        Thread.sleep(1000);
        System.out.println(CacheUtil.get("name"));
        System.out.println(CacheUtil.get("name1"));
        Thread.sleep(1000);
        System.out.println(CacheUtil.get("name"));
        System.out.println(CacheUtil.get("name1"));
        Thread.sleep(1000);
        System.out.println(CacheUtil.get("name"));
        System.out.println(CacheUtil.get("name1"));
        Thread.sleep(1000);
        System.out.println(CacheUtil.get("name"));
        System.out.println(CacheUtil.get("name1"));
 
    }
 
    private CacheUtil(){}
 
    private static final Map<String, CacheUtilBean> CACHE_MAP = new ConcurrentHashMap<>();
 
    public static void set(String key,Object value,long exprTime){
        //将传入的毫秒数 转换为 将来的时间戳
        CACHE_MAP.put(key,new CacheUtilBean(value,System.currentTimeMillis()+exprTime));
    }
 
    private static final long DEFAULT_EXPR_TIME = 24*60*60*1000L;
    public static void set(String key,Object value){
        set(key,value, DEFAULT_EXPR_TIME);
    }
 
    public static void remove(String key){
        CACHE_MAP.remove(key);
    }
 
    public static Object get(String key){
 
        CacheUtilBean cacheUtilBean = CACHE_MAP.get(key);
        if(cacheUtilBean==null){
            return null;
        }
//        EXECUTOR_SERVICE.submit(()->{
//            //获取之后在删除之前的
//            removeExp();
//        });
        return cacheUtilBean.getValue();
    }
 
    public static void removeExp() {
        List<String> removeKey = new ArrayList<>();
        Iterator<Map.Entry<String, CacheUtilBean>> iterator = CACHE_MAP.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, CacheUtilBean> next = iterator.next();
            Long exprTime = next.getValue().getExprTime();
            if(System.currentTimeMillis()>exprTime){
                 iterator.remove();
            }
        }
    }
    static class CacheUtilBean {
        //存的值
        private Object value;
        //过期时间戳 set的时候计算好
        private Long exprTime;
 
        public CacheUtilBean(Object value, Long exprTime) {
            this.value = value;
            this.exprTime = exprTime;
        }
 
        public Object getValue() {
            return value;
        }
 
        public void setValue(String value) {
            this.value = value;
        }
 
        public Long getExprTime() {
            return exprTime;
        }
 
        public void setExprTime(Long exprTime) {
            this.exprTime = exprTime;
        }
    }
 
}