| | |
| | | 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; |
| | |
| | | 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); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @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; |
| | | } |
| | |
| | | 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)); |
| | | } |
| | | |
| | | |
| | |
| | | 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)); |
| | | } |
| | | |
| | | /** |
| | |
| | | * 临时保存 默认5min |
| | | */ |
| | | public void tempSave(String key, String value) { |
| | | cacheManager.getCache(TEMP_CACHE_NAME).put(key, 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); |
| | | } |
| | | // 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); |
| | | // } |
| | | |
| | | |
| | | } |