Merge pull request #105 from rymcu/wx-dev

🐛 StackOverflowError 问题修复
This commit is contained in:
ronger 2022-09-03 16:34:25 +08:00 committed by GitHub
commit f0615bd832
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 139 deletions

View File

@ -1,8 +1,11 @@
package com.rymcu.forest.service.impl; 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.BusinessException;
import com.rymcu.forest.core.exception.ServiceException; import com.rymcu.forest.core.exception.ServiceException;
import com.rymcu.forest.core.service.AbstractService; 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.ArticleTagDTO;
import com.rymcu.forest.dto.LabelModel; import com.rymcu.forest.dto.LabelModel;
import com.rymcu.forest.entity.Article; 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.ArticleMapper;
import com.rymcu.forest.mapper.TagMapper; import com.rymcu.forest.mapper.TagMapper;
import com.rymcu.forest.service.TagService; import com.rymcu.forest.service.TagService;
import com.rymcu.forest.util.CacheUtils;
import com.rymcu.forest.util.XssUtils; import com.rymcu.forest.util.XssUtils;
import com.rymcu.forest.web.api.common.UploadController; import com.rymcu.forest.web.api.common.UploadController;
import com.rymcu.forest.web.api.exception.BaseApiException; import com.rymcu.forest.web.api.exception.BaseApiException;
@ -35,6 +37,8 @@ public class TagServiceImpl extends AbstractService<Tag> implements TagService {
private TagMapper tagMapper; private TagMapper tagMapper;
@Resource @Resource
private ArticleMapper articleMapper; private ArticleMapper articleMapper;
@Resource
private RedisService redisService;
@Override @Override
@Transactional(rollbackFor = {UnsupportedEncodingException.class, BaseApiException.class}) @Transactional(rollbackFor = {UnsupportedEncodingException.class, BaseApiException.class})
@ -63,7 +67,7 @@ public class TagServiceImpl extends AbstractService<Tag> implements TagService {
int n = articleTagDTOList.size(); int n = articleTagDTOList.size();
for (int m = 0; m < n; m++) { for (int m = 0; m < n; m++) {
ArticleTagDTO articleTag = articleTagDTOList.get(m); ArticleTagDTO articleTag = articleTagDTOList.get(m);
if (articleTag.getIdTag().equals(tag.getIdTag())) { if (articleTag.getIdTag().toString().equals(tag.getIdTag().toString())) {
articleTagDTOList.remove(articleTag); articleTagDTOList.remove(articleTag);
m--; m--;
n--; n--;
@ -93,6 +97,7 @@ public class TagServiceImpl extends AbstractService<Tag> implements TagService {
return 1; return 1;
} else { } else {
if (StringUtils.isNotBlank(articleContentHtml)) { if (StringUtils.isNotBlank(articleContentHtml)) {
article.setArticleTags("待分类");
saveTagArticle(article, articleContentHtml, userId); saveTagArticle(article, articleContentHtml, userId);
} }
} }
@ -152,10 +157,10 @@ public class TagServiceImpl extends AbstractService<Tag> implements TagService {
@Override @Override
public List<LabelModel> findTagLabels() { public List<LabelModel> findTagLabels() {
List<LabelModel> list = (List<LabelModel>) CacheUtils.get("tags"); List<LabelModel> list = JSONObject.parseArray(redisService.get("tags"), LabelModel.class);
if (list == null) { if (list == null) {
list = tagMapper.selectTagLabels(); list = tagMapper.selectTagLabels();
CacheUtils.put("tags", list); redisService.set("tags", JSON.toJSONString(list), 600);
} }
return list; return list;
} }

View File

@ -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<String, Object> cache = getCache(cacheName);
Set<String> 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<String, Object> getCache(String cacheName){
Cache<String, Object> cache = CACHE_MANAGER.getCache(cacheName);
if (cache == null){
throw new RuntimeException("当前系统中没有定义“"+cacheName+"”这个缓存。");
}
return cache;
}
}