From cebb05f8d1ddbf5ac415c5471e17d2d94ea1a6de Mon Sep 17 00:00:00 2001 From: zjh <1084500556@qq.com> Date: 星期三, 15 五月 2024 18:14:07 +0800 Subject: [PATCH] zjh 2024/05/15-1 --- ltkj-system/src/main/java/com/ltkj/tduck/utils/CacheUtils.java | 233 ++++++++++++++++++++++++++++++---------------------------- 1 files changed, 121 insertions(+), 112 deletions(-) diff --git a/ltkj-system/src/main/java/com/ltkj/tduck/utils/CacheUtils.java b/ltkj-system/src/main/java/com/ltkj/tduck/utils/CacheUtils.java index 88470c7..b828b20 100644 --- a/ltkj-system/src/main/java/com/ltkj/tduck/utils/CacheUtils.java +++ b/ltkj-system/src/main/java/com/ltkj/tduck/utils/CacheUtils.java @@ -1,122 +1,131 @@ -//package com.ltkj.tduck.utils; -// -//import cn.hutool.core.convert.Convert; -//import cn.hutool.core.util.ObjectUtil; -//import cn.hutool.core.util.StrUtil; -//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; -// -///** -// * @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"; +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; -// -// /** -// * 淇濆瓨鍒癈ache -// */ -// public void save(String key, String value) { + @Autowired + private RedisCache cache; + + /** + * 淇濆瓨鍒癈ache + */ + public void save(String key, String value) { // cacheManager.getCache(ETERNAL_CACHE_NAME).put(key, value); -// } -// -// /** -// * 鑾峰彇 -// * -// * @param key -// */ -// public String get(String key) { + 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(); // } -// 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); + 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)); -// } -// -// -// /** -// * 浠庨泦鍚堜腑绉婚櫎 -// * -// * @param key -// * @param value -// */ -// public void removeList(String key, Object value) { -// List coll = this.getList(key, Object.class); -// coll.remove(value); + 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) { + } + + /** + * 鑾峰彇闆嗗悎 + * + * @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 -// */ + } + + /** + * 鑾峰彇涓存椂瀛樺偍鍙橀噺 + * + * @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())) { @@ -128,6 +137,6 @@ // public void removeTemp(String key) { // cacheManager.getCache(TEMP_CACHE_NAME).evict(key); // } -// -// -//} + + +} -- Gitblit v1.8.0