zhaowenxuan
2025-02-06 06c12d2b42c343798d9476be66031b48967df639
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
package com.ltkj.tduck.utils;
 
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ltkj.common.core.redis.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Component;
 
import java.util.List;
 
/**
 * @author : tduck
 * @description : 基于ehcache实现
 * @create :  2022/01/06 10:40
 **/
@Component
@Slf4j
@EnableCaching
public class CacheUtils {
    private final String ETERNAL_CACHE_NAME = "eternal_cache";
    private final String TEMP_CACHE_NAME = "temp_cache";
//    @Autowired
//    private EhCacheCacheManager cacheManager;
    @Autowired
    private RedisCache cache;
 
    /**
     * 保存到Cache
     */
    public void save(String key, String value) {
//        cacheManager.getCache(ETERNAL_CACHE_NAME).put(key, value);
        cache.setCacheObject(key,value);
    }
 
    /**
     * 获取
     *
     * @param key
     */
    public String get(String key) {
//        Cache.ValueWrapper valueWrapper = cacheManager.getCache(ETERNAL_CACHE_NAME).get(key);
//        Cache.ValueWrapper valueWrapper = cache.getCacheObject(key);
//        if (ObjectUtil.isNotNull(valueWrapper) && ObjectUtil.isNotNull(valueWrapper.get())) {
//            return valueWrapper.get().toString();
//        }
        Object valueWrapper = cache.getCacheObject(key);
        if (ObjectUtil.isNotNull(valueWrapper) && ObjectUtil.isNotNull(valueWrapper)) {
            return valueWrapper.toString();
        }
        return null;
    }
 
 
    /**
     * 自增
     *
     * @param key
     * @param number
     * @return
     */
    public Long incr(String key, Integer number) {
        String v = get(key);
        if (StrUtil.isBlank(v)) {
            v = "0";
        }
        long finalValue = Convert.toLong(v) + number;
        save(key, String.valueOf(finalValue));
        return finalValue;
    }
 
 
    /**
     * 添加到集合缓存
     *
     * @param key
     * @param value
     */
    public void addList(String key, Object value) {
        List coll = this.getList(key, Object.class);
        coll.add(value);
//        cacheManager.getCache(TEMP_CACHE_NAME).put(key, JsonUtils.objToJson(coll));
        cache.setCacheObject(key, JsonUtils.objToJson(coll));
    }
 
 
    /**
     * 从集合中移除
     *
     * @param key
     * @param value
     */
    public void removeList(String key, Object value) {
        List coll = this.getList(key, Object.class);
        coll.remove(value);
//        cacheManager.getCache(TEMP_CACHE_NAME).put(key, JsonUtils.objToJson(coll));
    }
 
    /**
     * 获取集合
     *
     * @param key
     */
    public List getList(String key, Class classz) {
        String v = get(key);
        if (ObjectUtil.isNotNull(v)) {
            return JsonUtils.jsonToList(v, classz.getClass());
        }
        return Lists.newArrayList();
    }
 
 
    /**
     * 临时保存 默认5min
     */
    public void tempSave(String key, String value) {
//        cacheManager.getCache(TEMP_CACHE_NAME).put(key, value);
    }
 
    /**
     * 获取临时存储变量
     *
     * @param key
     */
//    public String getTemp(String key) {
//        Cache.ValueWrapper valueWrapper = cacheManager.getCache(TEMP_CACHE_NAME).get(key);
//        if (ObjectUtil.isNotNull(valueWrapper) && ObjectUtil.isNotNull(valueWrapper.get())) {
//            return valueWrapper.get().toString();
//        }
//        return null;
//    }
//
//    public void removeTemp(String key) {
//        cacheManager.getCache(TEMP_CACHE_NAME).evict(key);
//    }
 
 
}