jiangping
2025-07-16 b0a7148f1830eb544af588e49a36163ee5820c7c
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
package com.doumee.service.common;
 
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.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
 
import java.util.*;
import java.util.concurrent.TimeUnit;
 
/**
 * 缓存代理类,便于缓存变更
 * @author  dm
 * @since 2025/03/31 16:44
 */
@Slf4j
@Component
public class CacheProxy<K,V> {
 
    @Autowired
    private RedisTemplate<K, V> redisTemplate;
 
    /**
     * 获取缓存值
     *
     * @param key 缓存键
     * @return V
     */
    public V get(K key) throws CacheException {
        log.trace("CacheProxy: get, key = [" + key + "]");
        if (key == null) {
            return null;
        }
        return redisTemplate.opsForValue().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");
        }
        redisTemplate.opsForValue().set(key, value);
        return value;
    }
 
    /**
     * 指定过期时长
     *
     * @param key 缓存键
     * @param value 缓存值
     * @param expire 过期时间(s)
     * @return V
     */
    public V put(K key, V value, int expire) throws CacheException {
        this.put(key, value, expire * 1000L);
        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");
        }
        redisTemplate.opsForValue().set(key, value, expire, TimeUnit.MILLISECONDS);
        return value;
    }
 
    /**
     * 清理缓存
     */
    public void clear() throws CacheException {
        log.trace("CacheProxy: clear");
        Set<K> keys = this.keys();
        redisTemplate.delete(keys);
    }
 
    /**
     * 获取缓存数
     *
     * @return int
     */
    public int size() {
        log.trace("CacheProxy: size");
        return this.keys().size();
    }
 
    /**
     * 获取缓存key
     * 
     * @return Set<K>
     */
    public Set<K> keys() {
        log.trace("CacheProxy: keys");
        Set<K> keys = redisTemplate.keys(null);
        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();
        }
        return redisTemplate.keys((K)keyPattern);
    }
 
    /**
     * 获取所有缓存值
     *
     * @return Collection<V>
     */
    public Collection<V> values() {
        log.trace("CacheProxy: values");
        Collection<V> values = new ArrayList<>();
        Set<K> keys = this.keys();
        if (CollectionUtils.isEmpty(keys)) {
            return values;
        }
        for (K k : keys) {
            values.add(redisTemplate.opsForValue().get(k));
        }
        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;
        }
        V value = this.get(key);
        redisTemplate.delete(key);
        return value;
    }
}