shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
package com.walker.support.redis.cache;
 
import com.walker.cache.AbstractCacheProvider;
import com.walker.cache.Cache;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.support.redis.RedisHelper;
 
import java.util.Map;
 
/**
 * Redis方式缓存提供者实现
 * @author shikeying
 *
 * @param <T>
 */
public abstract class RedisCacheProvider<T> extends AbstractCacheProvider<T> {
 
    private RedisHelper redisHelper;
 
    public void setRedisHelper(RedisHelper redisHelper) {
        this.redisHelper = redisHelper;
    }
 
    @Override
    protected Cache provideCacheInstance(String name, Map<String, String> param){
//        RedisCache cache = null;
        if(this.isUseRedis()){
            RedisCache cache = new RedisCache(name, param);
            cache.setRedisHelper(this.redisHelper);
            cache.setSupportExpiredCache(this.supportExpiredCache);    // 2024-01-05
            return cache;
        } else {
            return super.provideCacheInstance(name, param);
        }
    }
 
    @Override
    public void destroy() throws Exception {
        if(!this.isUseRedis()){
            // 本地缓存
//            if(userCache != null)
//                userCache.clear();
            super.destroy();
        } else {
            // redis 缓存,先清空,2023-08-26
            // 这里不能清空,否则,系统停止时会调用该方法,让redis磁盘中数据清空! 2023-09-02
//            userCache.clear();
        }
    }
 
    @Override
    protected int loadDataToCache(Cache cache) {
        return 0;
    }
 
    @Override
    public T getCacheData(String key) {
        if(this.isUseRedis()){
            return (T)getRedisCache().get(key, getProviderType());
        } else {
            return super.getCacheData(key);
        }
    }
 
    protected RedisCache getRedisCache(){
        return (RedisCache)this.getCache();
    }
 
    @Override
    public long getCacheCount(){
        if(this.isUseRedis()){
            return getRedisCache().getPersistentSize();
        } else {
            return super.getCacheCount();
        }
    }
 
    @Override
    public void removeCacheData(String key) {
        if(this.isUseRedis()){
            getRedisCache().remove(key);
        } else {
            super.removeCacheData(key);
        }
    }
 
    @Override
    public void updateCacheData(String key, T data) {
        if(this.isUseRedis()){
            getRedisCache().replace(key, data);
        } else {
            super.updateCacheData(key, data);
        }
    }
 
    @Override
    public void putCacheData(String key, T data){
        if(this.isUseRedis()){
            getRedisCache().put(key, data);
        } else {
            super.putCacheData(key, data);
        }
    }
 
    /**
     * 添加支持数据过期的缓存方法。
     * @param key
     * @param data
     * @param expiredSeconds 过期秒数
     * @date 2022-11-13
     */
    @Override
    public void putCacheData(String key, T data, long expiredSeconds){
        assert (StringUtils.isNotEmpty(key));
        assert (data != null);
        assert(expiredSeconds > 0);
        if(this.isUseRedis()){
            getRedisCache().put(key, data, expiredSeconds);
        } else {
            super.putCacheData(key, data, expiredSeconds);
        }
    }
 
    @Override
    public void reload() throws Exception{
        if(this.isUseRedis()){
//            destroy();
//            afterPropertiesSet();
            // 2023-09-04 目前redis重新加载机制和本地缓存一样,都要删除原有缓存数据。
            super.reload();
        } else {
            super.reload();
        }
    }
 
    /**
     * 缓存对象不再需要单独根下面的独立的key方式。
     * @param key
     * @return
     * @date 2024-01-05
     */
    @Deprecated
    public String getRedisRootData(String key){
        return getRedisCache().getRootData(key);
    }
 
    public boolean isSupportExpiredCache() {
        return supportExpiredCache;
    }
 
    /**
     * 设置是否支持缓存失效,因为redis失效是针对key,因此HashMap方式是作为整体失效的!
     * <pre>
     *     1) 默认不支持缓存失效,此时使用Map方式存储数据
     *     2) 当设置运行失效时,必须采用字符串形式的数据结构
     *     3) 该选项只对hashmap数据结构有效。
     * </pre>
     * @param supportExpiredCache
     * @date 2024-01-05
     */
    public void setSupportExpiredCache(boolean supportExpiredCache) {
        this.supportExpiredCache = supportExpiredCache;
    }
 
    private boolean supportExpiredCache = false;
}