jiangping
2024-05-31 6be28040e74e1ff7764478174a9b5d706fe4fc39
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.doumee.core.cache;
 
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.SerializationUtils;
import org.springframework.stereotype.Component;
 
import java.io.Serializable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * 单机本地缓存,项目初始阶段使用
 * - 超过指定大小可通过日志方式报警
 * - 延长超时时间
 * @author Eva.Caesar Liu
 * @date 2019/3/23 17:57
 */
@Slf4j
@Component
public final class LocalCache<K,V> {
 
    // 警告大小
    private final int WARN_SIZE = 1073741824;
 
    // GB大小
    private final int GB_SIZE = 1073741824;
 
    // 缓存实例
    private static LocalCache instance;
 
    // 数据存储对象
    private ConcurrentHashMap<K, Value<V>> cache = new ConcurrentHashMap();
 
    private LocalCache(){}
 
    public static LocalCache getInstance() {
        if(instance == null) {
            synchronized (LocalCache.class) {
                if(instance == null)
                    instance = new LocalCache();
            }
        }
        return instance;
    }
 
    /**
     * 添加缓存,超时时间为30分钟
     * @param key 缓存键
     * @param value 缓存值
     */
    public void put(K key, V value) {
        Value<V> v = cache.get(key);
        if (v != null) {
            v.value = value;
            return;
        }
        put(key, value, 30 * 60 * 1000);
    }
 
    /**
     * 添加缓存
     * @param key 缓存键
     * @param value 缓存值
     * @param timeout 超时时间
     */
    public void put(K key, V value, long timeout) {
        if(key == null) throw new NullPointerException("key can not be null");
        // 清理旧数据
        long now = System.currentTimeMillis();
        for(Map.Entry<K, Value<V>> entry: cache.entrySet()){
            Value v = entry.getValue();
            if(v.getValue() == null) // 值为空时清除掉
                cache.remove(entry.getKey());
            if(now - v.getBirthtime() > v.getTimeout()) // 超时时清除掉
                cache.remove(entry.getKey());
        }
 
        // 添加缓存
        Value<V> v = new Value<>();
        v.setBirthtime(now);
        v.setTimeout(timeout);
        v.setValue(value);
        cache.put(key, v);
 
        // 获取对象大小,超过一定大小打印警告
        int cacheSize = SerializationUtils.serialize(cache).length;
        if(cacheSize > WARN_SIZE) {
            log.warn("本地缓存已超过{}G,当前缓存容量为:{}G", (double) Math.round((double) WARN_SIZE / GB_SIZE * 100) / 100, (double) Math.round((double) cacheSize / GB_SIZE * 100) / 100);
        }
    }
 
    /**
     * 根据key获取缓存对象
     * @param key 缓存键
     */
    public V get(K key) {
        if(key == null) return null;
        Value<V> value = cache.get(key);
        if(value == null)
            return null;
        if(value.getValue() == null)
            return null;
        if(System.currentTimeMillis() - value.getBirthtime() > value.getTimeout()) {
            remove(key);
            return null;
        }
        return value.getValue();
    }
 
    /**
     * 根据key获取缓存对象
     * @param key 缓存键
     *
     * @return V
     */
    public V remove(K key) {
        if(key == null) return null;
        Value<V> value = cache.get(key);
        if (value == null) {
            return null;
        }
        cache.remove(key);
        return value.value;
    }
 
    /**
     * 清空缓存
     */
    public void clear () {
        cache.clear();
    }
 
    /**
     * 获取缓存数
     */
    public int size () {
        return cache.size();
    }
 
    /**
     * 获取缓存所有的key
     */
    public Set<K> keys () {
        return cache.keySet();
    }
 
    /**
     * 获取缓存值
     */
    public Collection<V> values () {
        Collection<Value<V>> values = cache.values();
        List<V> vs = new ArrayList(keys().size());
        for (Value<V> v : values) {
            if (v.value != null) {
                vs.add(v.value);
            }
        }
        return vs;
    }
 
    /**
     * 重置缓存诞生时间
     * @param key 缓存键
     */
    public void relive(String key) {
        if (key == null) return;
        Value v = cache.get(key);
        if (v == null) {
            return;
        }
        v.setBirthtime(new Date().getTime());
    }
 
    @Data
    private static class Value<V> implements Serializable {
 
        // 缓存具体值
        private V value;
 
        // 存储时间
        private long birthtime;
 
        // 超时时间
        private long timeout;
    }
 
}