From d698569ce49df21eafec25b8dbd32d98db36297b Mon Sep 17 00:00:00 2001 From: jianjun Date: Wed, 17 Jun 2020 10:14:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=87=E7=AD=BEkey=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E5=88=B0redisKeys=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/epmet/constant/TagConstant.java | 22 ++++++------ .../main/java/com/epmet/redis/TagRedis.java | 34 +++++++++---------- .../service/impl/ArticleServiceImpl.java | 9 ++--- .../epmet/service/impl/TagServiceImpl.java | 18 ++++++---- 4 files changed, 44 insertions(+), 39 deletions(-) diff --git a/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/constant/TagConstant.java b/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/constant/TagConstant.java index 7e51e3a3d1..f2de457976 100644 --- a/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/constant/TagConstant.java +++ b/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/constant/TagConstant.java @@ -15,34 +15,34 @@ package com.epmet.constant; */ public interface TagConstant { - /** +/* *//** * 客户维度(政府端) 热度标签 key的前缀 【zset】 * 完整key:GRID_TAG_KEY:客户ID - */ + *//* String GOV_TAG_KEY = "epmet:tags:customer:rankingTag:"; - /** + *//** * 客户维度(政府端) 关联标签 key的前缀 【set】 * 完整key:GRID_TAG_KEY:标签ID - */ + *//* String GOV_RETAG_KEY = "epmet:tags:customer:relationTag:"; - /** + *//** * 网格热度标签(居民端) key的前缀 【zset】 * 完整key:GRID_TAG_KEY:网格ID - */ + *//* String GRID_TAG_KEY = "epmet:tags:grid:rankingTag:"; - /** + *//** * 网格关联标签(居民端) key的前缀 【set】 * 完整key:GRID_RETAG_KEY:网格ID:标签ID - */ + *//* String GRID_RETAG_KEY ="epmet:tags:grid:relationTag:"; - /** + *//** * 冒号 - */ - String COLON = ":"; + *//* + String COLON = ":";*/ String BEGIN_UPDATE = "开始更新标签缓存... ..."; String SUCCESS_UPDATE = "更新标签缓存成功... ..."; diff --git a/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/redis/TagRedis.java b/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/redis/TagRedis.java index 9902437427..c8657af1a5 100644 --- a/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/redis/TagRedis.java +++ b/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/redis/TagRedis.java @@ -88,14 +88,14 @@ public class TagRedis { if (tagIdList.size() > 1){ for (int i = NumConstant.ONE; i < tagIdList.size(); i++) { String tagId = tagIdList.get(i); - tagId = TagConstant.GOV_RETAG_KEY+customerId+TagConstant.COLON+tagId; + tagId = RedisKeys.getCustomerReTagKey(customerId,tagId); keys.add(tagId); } - String key = TagConstant.GOV_RETAG_KEY+customerId+TagConstant.COLON+tagIdList.get(0); + String key = RedisKeys.getCustomerReTagKey(customerId,tagIdList.get(0)); objects = redisUtils.intersect(key, keys); }else { // 查询关联标签数量 等于1条时 - String key = TagConstant.GOV_RETAG_KEY+customerId+TagConstant.COLON+tagIdList.get(0); + String key = RedisKeys.getCustomerReTagKey(customerId,tagIdList.get(0)); objects = redisUtils.sMembers(key); } List resultList = new ArrayList<>(); @@ -113,7 +113,7 @@ public class TagRedis { if (resultList.size() > NumConstant.ZERO) { long start = 0; long end = -1; - String customerKey = TagConstant.GOV_TAG_KEY + customerId; + String customerKey = RedisKeys.getCustomerTagKey(customerId); Set> typedTuples = redisUtils.zReverseRangeWithScores(customerKey, start, end); for (CorrelationTagListResultDTO correlationTagList : resultList) { for (ZSetOperations.TypedTuple typedTuple : typedTuples) { @@ -145,7 +145,7 @@ public class TagRedis { public List zRevRange(String key){ long start = 0; long end = -1; - String tagKey = TagConstant.GOV_TAG_KEY+key; + String tagKey = RedisKeys.getCustomerTagKey(key); Set objects = redisUtils.zRevRange(tagKey, start, end); if (objects.size()== NumConstant.ZERO){ return new ArrayList<>(); @@ -166,7 +166,7 @@ public class TagRedis { public List zGridRevRange(String key){ long start = 0; long end = -1; - String tagKey = TagConstant.GRID_TAG_KEY+key; + String tagKey = RedisKeys.getGridTagKey(key); Set objects = redisUtils.zRevRange(tagKey, start, end); if (objects.size()== NumConstant.ZERO){ return new ArrayList<>(); @@ -192,14 +192,14 @@ public class TagRedis { if (tagIdList.size() > 1){ for (int i = NumConstant.ONE; i < tagIdList.size(); i++) { String tagId = tagIdList.get(i); - tagId = TagConstant.GRID_RETAG_KEY+gridId+TagConstant.COLON+tagId; + tagId = RedisKeys.getGridReTagKey(gridId,tagId); keys.add(tagId); } - String key = TagConstant.GRID_RETAG_KEY+gridId+TagConstant.COLON+tagIdList.get(0); + String key = RedisKeys.getGridReTagKey(gridId,tagIdList.get(0)); objects = redisUtils.intersect(key, keys); }else { //当级联标签为一条时 - String key = TagConstant.GRID_RETAG_KEY+gridId+TagConstant.COLON+tagIdList.get(0); + String key = RedisKeys.getGridReTagKey(gridId,tagIdList.get(0)); objects = redisUtils.sMembers(key); } List resultList = new ArrayList<>(); @@ -216,8 +216,9 @@ public class TagRedis { if (resultList.size() > NumConstant.ZERO) { long start = 0; long end = -1; - String customerKey = TagConstant.GRID_TAG_KEY+formDto.getGridId(); - Set> typedTuples = redisUtils.zReverseRangeWithScores(customerKey, start, end); + String gridTagKey =RedisKeys.getGridTagKey(formDto.getGridId()); + + Set> typedTuples = redisUtils.zReverseRangeWithScores(gridTagKey, start, end); for (TagInfoResultDTO tagInfo : resultList) { for (ZSetOperations.TypedTuple typedTuple : typedTuples) { TagRankResultDTO tagRank = objectToDTO(typedTuple.getValue(), TagRankResultDTO.class); @@ -291,14 +292,11 @@ public class TagRedis { public void updateMoreTag(String key, Set value) { try { log.info(TagConstant.BEGIN_UPDATE); - redisTemplate.executePipelined(new RedisCallback>() { - @Override - public Set doInRedis(RedisConnection connection) throws DataAccessException { - for (UpdateTagUseCountsResultDTO tag : value) { - connection.sAdd(redisTemplate.getKeySerializer().serialize(key),redisTemplate.getValueSerializer().serialize(tag)); - } - return null; + redisTemplate.executePipelined((RedisCallback>) connection -> { + for (UpdateTagUseCountsResultDTO tag : value) { + connection.sAdd(redisTemplate.getKeySerializer().serialize(key),redisTemplate.getValueSerializer().serialize(tag)); } + return null; }); log.info(TagConstant.SUCCESS_UPDATE); }catch (Exception e){ diff --git a/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/service/impl/ArticleServiceImpl.java b/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/service/impl/ArticleServiceImpl.java index 37b6b60664..b537270257 100644 --- a/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/service/impl/ArticleServiceImpl.java +++ b/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/service/impl/ArticleServiceImpl.java @@ -26,6 +26,7 @@ import com.epmet.commons.tools.constant.StrConstant; import com.epmet.commons.tools.exception.EpmetErrorCode; import com.epmet.commons.tools.exception.RenException; import com.epmet.commons.tools.page.PageData; +import com.epmet.commons.tools.redis.RedisKeys; import com.epmet.commons.tools.scan.param.ImgScanParamDTO; import com.epmet.commons.tools.scan.param.ImgTaskDTO; import com.epmet.commons.tools.scan.param.TextScanParamDTO; @@ -1196,14 +1197,14 @@ public class ArticleServiceImpl extends BaseServiceImpl { - String customerKey = TagConstant.GOV_TAG_KEY + customerId; + String customerKey = RedisKeys.getCustomerTagKey(customerId); tagRedis.updateTagUseCounts(customerKey, resultDTO); }); //政府端更新redis的级联标签(set) List tagsInfoCopy = new ArrayList<>(); for (int i = 0; i < tagsInfo.size(); i++) { tagsInfoCopy.addAll(tagsInfo); - String key = TagConstant.GOV_RETAG_KEY + customerId + TagConstant.COLON + tagsInfo.get(i).getTagId(); + String key = RedisKeys.getCustomerReTagKey(customerId, tagsInfo.get(i).getTagId()); tagsInfoCopy.remove(tagsInfo.get(i)); Set setTag = new HashSet<>(tagsInfoCopy); tagRedis.updateMoreTag(key, setTag); @@ -1259,7 +1260,7 @@ public class ArticleServiceImpl extends BaseServiceImpl gridTagCacheCopy = new ArrayList<>(); gridTagCache.forEach(cacheDTO -> { String gridId = cacheDTO.getGridId(); - String key = TagConstant.GRID_TAG_KEY + gridId; + String key = RedisKeys.getGridTagKey(gridId); List tagsInfo = cacheDTO.getTagsInfo(); tagsInfo.forEach(resultDTO -> { tagRedis.updateTagUseCounts(key, resultDTO); @@ -1267,7 +1268,7 @@ public class ArticleServiceImpl extends BaseServiceImpl setTag = new HashSet<>(gridTagCacheCopy); tagRedis.updateMoreTag(moreKey, setTag); diff --git a/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/service/impl/TagServiceImpl.java b/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/service/impl/TagServiceImpl.java index 4ac5d67ac0..e1fb6b092c 100644 --- a/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/service/impl/TagServiceImpl.java +++ b/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/service/impl/TagServiceImpl.java @@ -3,8 +3,8 @@ package com.epmet.service.impl; import com.epmet.commons.tools.constant.NumConstant; import com.epmet.commons.tools.constant.StrConstant; import com.epmet.commons.tools.exception.RenException; +import com.epmet.commons.tools.redis.RedisKeys; import com.epmet.commons.tools.security.dto.TokenDto; -import com.epmet.constant.TagConstant; import com.epmet.dao.*; import com.epmet.dto.form.*; import com.epmet.dto.result.CorrelationTagListResultDTO; @@ -90,7 +90,7 @@ public class TagServiceImpl implements TagService { InitTagsFormDTO tagInfo = new InitTagsFormDTO(); UpdateTagUseCountsResultDTO initTag = new UpdateTagUseCountsResultDTO(); UUID uuid = UUID.randomUUID(); - String replaceUuid = uuid.toString().replace("-", ""); + String replaceUuid = uuid.toString().replace(StrConstant.HYPHEN, ""); tagInfo.setId(replaceUuid); initTag.setTagId(replaceUuid); tagInfo.setTagName(tagName); @@ -100,7 +100,7 @@ public class TagServiceImpl implements TagService { initCacheTags.add(initTag); }); tagCustomerDao.initTags(tags); - String key = TagConstant.GOV_TAG_KEY+customerId; + String key = RedisKeys.getCustomerTagKey(customerId); tagRedis.initCacheTags(key,initCacheTags); return tagRedis.zRevRange(customerId); } @@ -159,7 +159,10 @@ public class TagServiceImpl implements TagService { }); } if (reCustomerTagMap.size() > 0) { - reCustomerTagMap.forEach((customerAndTagId, tagSet) -> tagRedis.updateMoreTag(TagConstant.GOV_RETAG_KEY.concat(customerAndTagId), tagSet)); + reCustomerTagMap.forEach((customerAndTagId, tagSet) -> { + String[] customerIdAndTagIdArr = customerAndTagId.split(StrConstant.COLON); + tagRedis.updateMoreTag(RedisKeys.getCustomerReTagKey(customerIdAndTagIdArr[0], customerIdAndTagIdArr[1]), tagSet); + }); } @@ -189,14 +192,17 @@ public class TagServiceImpl implements TagService { for (ArticlePublishRangeEntity publishRange : articleIdList) { List articleTagsEntities = articleReTagMap.get(publishRange.getArticleId()); if (CollectionUtils.isEmpty(articleTagsEntities)){ - //该文章诶呦标签 + //该文章没有标签 continue; } buildGridReTag(resultMap,gridId,articleTagsEntities); } } if (resultMap.size() > 0) { - resultMap.forEach((gridIdAndTagId, tagSet) -> tagRedis.updateMoreTag(TagConstant.GRID_RETAG_KEY.concat(gridIdAndTagId), tagSet)); + resultMap.forEach((gridIdAndTagId, tagSet) -> { + String[] gridIdAndTagIdArr = gridIdAndTagId.split(StrConstant.COLON); + tagRedis.updateMoreTag(RedisKeys.getGridReTagKey(gridIdAndTagIdArr[0],gridIdAndTagIdArr[1]), tagSet); + }); } return true; }