AdaKing88
2023-08-23 ae35159387a55199e8ab150ebb97d89d68a235bd
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
package org.jeecg.modules.arj.config;
 
import org.springframework.stereotype.Service;
 
import java.time.LocalDateTime;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * @author zk
 * @Description: 缓存
 */
@Service("cacheMap")
public class CacheMap {
 
    /**
     * 缓存的map
     */
    private static ConcurrentHashMap<String, CacheValue> map = new ConcurrentHashMap<>(16);
 
 
    /**
     * 设置缓存
     *
     * @param key        键
     * @param cacheValue 值
     */
    public void set(String key, CacheValue cacheValue) {
        map.put(key, cacheValue);
    }
 
 
    /**
     * 设置缓存
     *
     * @param key 键
     */
    public void set(String key, Object object) {
        CacheValue<Object> value = new CacheValue<>(object);
        map.put(key, value);
    }
 
    /**
     * 删除缓存
     *
     * @param key 键
     */
    public void remove(String key) {
        map.remove(key);
    }
 
 
    /**
     * 获取缓存数据
     * 如果过期了就返回null
     */
    public Object get(String key) {
        CacheValue value = map.get(key);
        if (key == null || value == null) {
            return null;
        }
        LocalDateTime dateTime = value.getCreateTime().plusSeconds(value.getExpireTime());
        boolean after = LocalDateTime.now().isAfter(dateTime);
        if (after) {
            map.remove(key);
            return null;
        } else {
            value.setTimes(value.getTimes() + 1);
            map.put(key, value);
            return value.getData();
        }
    }
 
 
    /**
     * 就只有当前包可以获取
     */
    ConcurrentHashMap<String, CacheValue> getMap() {
        return map;
    }
 
 
}