jiangping
2025-02-19 7cac7eb8e45578aca8abc4d8baa683ead49f4080
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
package com.doumee.service.proxy;
 
import com.doumee.core.cache.LocalCache;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.*;
 
/**
 * 缓存代理类,便于缓存变更
 * @author Eva.Caesar Liu
 * @date 2022/03/15 09:54
 */
@Slf4j
@Component
public class CacheProxy<K,V> {
 
    @Autowired
    private LocalCache<K,V> localCache;
 
    /**
     * 获取缓存值
     * @param key 缓存键
     *
     * @return V
     */
    public V get(K key) throws CacheException {
        log.trace("CacheProxy: get, key = [" + key + "]");
        if (key == null) {
            return null;
        }
        return localCache.get(key);
    }
 
    /**
     * 获取缓存值
     * @param key 缓存键
     * @param value 缓存值
     *
     * @return V
     */
    public V put(K key, V value) throws CacheException {
        log.trace("CacheProxy: put, key = [" + key + "]");
        if (key == null) {
            log.warn("CacheProxy: put, key can not be null");
        }
        localCache.put(key, value);
        return value;
    }
 
    /**
     * 指定过期时长
     * @param key 缓存键
     * @param value 缓存值
     * @param expire 过期时间(s)
     *
     * @return V
     */
    public V put(K key, V value, int expire) throws CacheException {
        log.trace("CacheProxy: put, key = [" + key + "]");
        if (key == null) {
            log.warn("CacheProxy: put, key can not be null");
        }
        localCache.put(key, value, expire * 1000);
        return value;
    }
 
    /**
     * 指定过期时长
     * @param key 缓存键
     * @param value 缓存值
     * @param expire 过期时间(ms)
     *
     * @return V
     */
    public V put(K key, V value, long expire) throws CacheException {
        log.trace("CacheProxy: put, key = [" + key + "]");
        if (key == null) {
            log.warn("CacheProxy: put, key can not be null");
        }
        localCache.put(key, value, expire);
        return value;
    }
 
    /**
     * 清理缓存
     */
    public void clear() throws CacheException {
        log.trace("CacheProxy: clear");
        localCache.clear();
    }
 
    /**
     * 获取缓存数
     *
     * @return int
     */
    public int size() {
        log.trace("CacheProxy: size");
        return localCache.size();
    }
 
    /**
     * 获取缓存key
     *
     * @return Set<K>
     */
    public Set<K> keys() {
        log.trace("CacheProxy: keys");
        Set<K> keys = localCache.keys();
        if (CollectionUtils.isEmpty(keys)) {
            return Collections.emptySet();
        }
        return keys;
    }
 
    /**
     * 获取缓存key
     * @param keyPattern 缓存键正则
     *
     * @return Set<K>
     */
    public Set<K> keys(String keyPattern) {
        log.trace("CacheProxy: keys, keyPattern = [" + keyPattern + "]");
        if (keyPattern == null || "".equals(keyPattern) || "*".equals(keyPattern)) {
            return keys();
        }
        Set<K> keys = this.keys();
        if (CollectionUtils.isEmpty(keys)) {
            return Collections.emptySet();
        }
        Set<K> filterKeys = new HashSet<>();
        Iterator<K> iter = keys.iterator();
        while(iter.hasNext()) {
            K key = iter.next();
            if (key instanceof String && ((String) key).matches(keyPattern)) {
                filterKeys.add(key);
            }
        }
        return filterKeys;
    }
 
    /**
     * 获取所有缓存值
     *
     * @return Collection<V>
     */
    public Collection<V> values() {
        log.trace("CacheProxy: values");
        Collection<V> values = localCache.values();
        if (CollectionUtils.isEmpty(values)) {
            return Collections.emptyList();
        }
        return values;
    }
 
    /**
     * 获取所有缓存值
     * @param key 缓存键
     *
     * @return Collection<V>
     */
    public V remove(K key) throws CacheException {
        log.trace("CacheProxy: remove, key = [" + key + "]");
        if (key == null) {
            return null;
        }
        return localCache.remove(key);
    }
 
}