From 869d881f8164f04010afb4d93f04369b390faf43 Mon Sep 17 00:00:00 2001 From: ronger Date: Sat, 3 Sep 2022 16:30:10 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20StackOverflowError=20=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../forest/service/impl/TagServiceImpl.java | 13 +- .../com/rymcu/forest/util/CacheUtils.java | 135 ------------------ 2 files changed, 9 insertions(+), 139 deletions(-) delete mode 100644 src/main/java/com/rymcu/forest/util/CacheUtils.java diff --git a/src/main/java/com/rymcu/forest/service/impl/TagServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/TagServiceImpl.java index 25fcf3f..ce37474 100644 --- a/src/main/java/com/rymcu/forest/service/impl/TagServiceImpl.java +++ b/src/main/java/com/rymcu/forest/service/impl/TagServiceImpl.java @@ -1,8 +1,11 @@ package com.rymcu.forest.service.impl; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.rymcu.forest.core.exception.BusinessException; import com.rymcu.forest.core.exception.ServiceException; import com.rymcu.forest.core.service.AbstractService; +import com.rymcu.forest.core.service.redis.RedisService; import com.rymcu.forest.dto.ArticleTagDTO; import com.rymcu.forest.dto.LabelModel; import com.rymcu.forest.entity.Article; @@ -10,7 +13,6 @@ import com.rymcu.forest.entity.Tag; import com.rymcu.forest.mapper.ArticleMapper; import com.rymcu.forest.mapper.TagMapper; import com.rymcu.forest.service.TagService; -import com.rymcu.forest.util.CacheUtils; import com.rymcu.forest.util.XssUtils; import com.rymcu.forest.web.api.common.UploadController; import com.rymcu.forest.web.api.exception.BaseApiException; @@ -35,6 +37,8 @@ public class TagServiceImpl extends AbstractService implements TagService { private TagMapper tagMapper; @Resource private ArticleMapper articleMapper; + @Resource + private RedisService redisService; @Override @Transactional(rollbackFor = {UnsupportedEncodingException.class, BaseApiException.class}) @@ -63,7 +67,7 @@ public class TagServiceImpl extends AbstractService implements TagService { int n = articleTagDTOList.size(); for (int m = 0; m < n; m++) { ArticleTagDTO articleTag = articleTagDTOList.get(m); - if (articleTag.getIdTag().equals(tag.getIdTag())) { + if (articleTag.getIdTag().toString().equals(tag.getIdTag().toString())) { articleTagDTOList.remove(articleTag); m--; n--; @@ -93,6 +97,7 @@ public class TagServiceImpl extends AbstractService implements TagService { return 1; } else { if (StringUtils.isNotBlank(articleContentHtml)) { + article.setArticleTags("待分类"); saveTagArticle(article, articleContentHtml, userId); } } @@ -152,10 +157,10 @@ public class TagServiceImpl extends AbstractService implements TagService { @Override public List findTagLabels() { - List list = (List) CacheUtils.get("tags"); + List list = JSONObject.parseArray(redisService.get("tags"), LabelModel.class); if (list == null) { list = tagMapper.selectTagLabels(); - CacheUtils.put("tags", list); + redisService.set("tags", JSON.toJSONString(list), 600); } return list; } diff --git a/src/main/java/com/rymcu/forest/util/CacheUtils.java b/src/main/java/com/rymcu/forest/util/CacheUtils.java deleted file mode 100644 index bac94a2..0000000 --- a/src/main/java/com/rymcu/forest/util/CacheUtils.java +++ /dev/null @@ -1,135 +0,0 @@ -package com.rymcu.forest.util; - -import org.apache.shiro.cache.Cache; -import org.apache.shiro.cache.CacheManager; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Set; - -/** - * Cache工具类 - */ -public class CacheUtils { - - private static final Logger LOGGER = LoggerFactory.getLogger(CacheUtils.class); - private static final CacheManager CACHE_MANAGER = SpringContextHolder.getBean(CacheManager.class); - - private static final String SYS_CACHE = "system"; - - /** - * 获取SYS_CACHE缓存 - * @param key 键值 - * @return 缓存对象 - */ - public static Object get(String key) { - return get(SYS_CACHE, key); - } - - /** - * 获取SYS_CACHE缓存 - * @param key 键值 - * @param defaultValue 默认返回值 - * @return 缓存对象或默认值 - */ - public static Object get(String key, Object defaultValue) { - Object value = get(key); - return value != null ? value : defaultValue; - } - - /** - * 写入SYS_CACHE缓存 - * @param key 键值 - * @param value 被缓存对象 - */ - public static void put(String key, Object value) { - put(SYS_CACHE, key, value); - } - - /** - * 从SYS_CACHE缓存中移除 - * @param key 键值 - */ - public static void remove(String key) { - remove(SYS_CACHE, key); - } - - /** - * 获取指定命名空间下的缓存 - * @param cacheName 缓存命名空间 - * @param key 键值 - * @return 该命名空间下的对应键值缓存 - */ - public static Object get(String cacheName, String key) { - return getCache(cacheName).get(getKey(key)); - } - - /** - * 获取指定命名空间下的缓存,并在无缓存对象时返回默认值 - * @param cacheName 缓存命名空间 - * @param key 键值 - * @param defaultValue 默认返回值 - * @return 缓存对象或默认值 - */ - public static Object get(String cacheName, String key, Object defaultValue) { - Object value = get(cacheName, getKey(key)); - return value != null ? value : defaultValue; - } - - /** - * 向指定命名空间下存入被缓存对象 - * @param cacheName 缓存命名空间 - * @param key 键值 - * @param value 该命名空间下被缓存对象 - */ - public static void put(String cacheName, String key, Object value) { - getCache(cacheName).put(getKey(key), value); - } - - /** - * 将指定命名空间下的缓存移除 - * @param cacheName 缓存命名空间 - * @param key 键值 - */ - public static void remove(String cacheName, String key) { - getCache(cacheName).remove(getKey(key)); - } - - /** - * 清空命名空间下所有缓存 - * @param cacheName 命名空间 - */ - public static void removeAll(String cacheName) { - Cache cache = getCache(cacheName); - Set keys = cache.keys(); - cache.clear(); - LOGGER.info("清理缓存: {} => {}", cacheName, keys); - } - - /** - * 获取缓存键名,多数据源下增加数据源名称前缀 - * @param key 键值 - * @return 返回对应数据源前缀拼接键值 - */ - private static String getKey(String key){ -// String dsName = DataSourceHolder.getDataSourceName(); -// if (StringUtils.isNotBlank(dsName)){ -// return dsName + "_" + key; -// } - return key; - } - - /** - * 获得指定命名空间的Cache,没有则显示日志。 - * @param cacheName 命名空间 - * @return 命名空间对应的Cache - */ - private static Cache getCache(String cacheName){ - Cache cache = CACHE_MANAGER.getCache(cacheName); - if (cache == null){ - throw new RuntimeException("当前系统中没有定义“"+cacheName+"”这个缓存。"); - } - return cache; - } - -}