zjh
2025-02-21 2c785c3d4513daea9deb5c7edbb17a9f17111d25
ltkj-system/src/main/java/com/ltkj/tduck/utils/CacheUtils.java
@@ -3,12 +3,12 @@
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.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Component;
import java.util.List;
@@ -24,14 +24,17 @@
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 EhCacheCacheManager cacheManager;
    private RedisCache cache;
    /**
     * 保存到Cache
     */
    public void save(String key, String value) {
        cacheManager.getCache(ETERNAL_CACHE_NAME).put(key, value);
//        cacheManager.getCache(ETERNAL_CACHE_NAME).put(key, value);
        cache.setCacheObject(key,value);
    }
    /**
@@ -40,9 +43,14 @@
     * @param key
     */
    public String get(String key) {
        Cache.ValueWrapper valueWrapper = cacheManager.getCache(ETERNAL_CACHE_NAME).get(key);
        if (ObjectUtil.isNotNull(valueWrapper) && ObjectUtil.isNotNull(valueWrapper.get())) {
            return valueWrapper.get().toString();
//        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;
    }
@@ -75,7 +83,8 @@
    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));
//        cacheManager.getCache(TEMP_CACHE_NAME).put(key, JsonUtils.objToJson(coll));
        cache.setCacheObject(key, JsonUtils.objToJson(coll));
    }
@@ -88,7 +97,7 @@
    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));
//        cacheManager.getCache(TEMP_CACHE_NAME).put(key, JsonUtils.objToJson(coll));
    }
    /**
@@ -109,7 +118,7 @@
     * 临时保存 默认5min
     */
    public void tempSave(String key, String value) {
        cacheManager.getCache(TEMP_CACHE_NAME).put(key, value);
//        cacheManager.getCache(TEMP_CACHE_NAME).put(key, value);
    }
    /**
@@ -117,17 +126,17 @@
     *
     * @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);
    }
//    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);
//    }
}