From effd011a2721798308b5e17c6c4a1f7fe1d3332e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E8=A3=95=E8=B4=A2?= Date: Tue, 3 Oct 2023 23:42:08 +0800 Subject: [PATCH] 2.0 --- .../com/xm/core/ctrl/XmTaskController.java | 1 - .../com/xm/core/service/XmGroupService.java | 1 - .../service/cache/XmGroupCacheService.java | 197 ------------------ .../service/cache/XmTaskCacheService.java | 47 ----- .../cache/sub/CacheMessageListener.java | 1 - 5 files changed, 247 deletions(-) delete mode 100644 xm-core/src/main/java/com/xm/core/service/cache/XmGroupCacheService.java delete mode 100644 xm-core/src/main/java/com/xm/core/service/cache/XmTaskCacheService.java diff --git a/xm-core/src/main/java/com/xm/core/ctrl/XmTaskController.java b/xm-core/src/main/java/com/xm/core/ctrl/XmTaskController.java index d0bfb76f..2c0028d9 100644 --- a/xm-core/src/main/java/com/xm/core/ctrl/XmTaskController.java +++ b/xm-core/src/main/java/com/xm/core/ctrl/XmTaskController.java @@ -21,7 +21,6 @@ import com.xm.core.PubTool; import com.xm.core.entity.*; import com.xm.core.queue.XmTaskSumParentsPushService; import com.xm.core.service.*; -import com.xm.core.service.cache.XmTaskCacheService; import com.xm.core.service.client.SysClient; import com.xm.core.service.push.XmPushMsgService; import com.xm.core.vo.*; diff --git a/xm-core/src/main/java/com/xm/core/service/XmGroupService.java b/xm-core/src/main/java/com/xm/core/service/XmGroupService.java index c8945d1a..5d016c07 100644 --- a/xm-core/src/main/java/com/xm/core/service/XmGroupService.java +++ b/xm-core/src/main/java/com/xm/core/service/XmGroupService.java @@ -5,7 +5,6 @@ import com.mdp.safe.client.entity.Dept; import com.mdp.safe.client.entity.User; import com.xm.core.entity.XmProduct; import com.xm.core.entity.XmProject; -import com.xm.core.service.cache.XmGroupCacheService; import com.xm.core.service.client.SysClient; import com.xm.core.service.push.XmPushMsgService; import org.springframework.beans.factory.annotation.Autowired; diff --git a/xm-core/src/main/java/com/xm/core/service/cache/XmGroupCacheService.java b/xm-core/src/main/java/com/xm/core/service/cache/XmGroupCacheService.java deleted file mode 100644 index 736619a8..00000000 --- a/xm-core/src/main/java/com/xm/core/service/cache/XmGroupCacheService.java +++ /dev/null @@ -1,197 +0,0 @@ -package com.xm.core.service.cache; - -import com.mdp.mq.sp.Publish; -import com.xm.core.vo.XmGroupVo; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Service; - -import java.util.Calendar; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.TimeUnit; - -@Service -public class XmGroupCacheService { - - @Autowired - RedisTemplate redisTemplate; - - Map> prjCache =new ConcurrentHashMap<>(); - Map> prdCache =new ConcurrentHashMap<>(); - - @Autowired - Publish publish; - - String currPrdDateKey=""; - public String getProductKey(String productId){ - Calendar currCal=Calendar.getInstance(); - String dateKey=currCal.get(Calendar.YEAR)+"-"+currCal.get(Calendar.DAY_OF_YEAR)+"_prd"; - String finalKey=this.getCacheKey()+":"+dateKey+":"+productId; - if(dateKey.equals(currPrdDateKey)){ - return finalKey; - }else { - currPrdDateKey=dateKey; - this.redisTemplate.expire(finalKey,24,TimeUnit.HOURS); - return finalKey; - } - } - String currPrjDateKey=""; - public String getProjectKey(String projectId){ - Calendar currCal=Calendar.getInstance(); - String dateKey=currCal.get(Calendar.YEAR)+"-"+currCal.get(Calendar.DAY_OF_YEAR)+"_prj"; - String finalKey=this.getCacheKey()+":"+dateKey+":"+projectId; - if(dateKey.equals(currPrjDateKey)){ - return finalKey; - }else { - currPrjDateKey=dateKey; - this.redisTemplate.expire(finalKey,24,TimeUnit.HOURS); - return finalKey; - } - } - String getCacheKey() { - return "xm_group"; - } - - public List getProjectGroups(String projectId){ - String key=this.getProjectKey(projectId); - List groupVoList=this.prjCache.get(key); - if(groupVoList==null){ - groupVoList= (List) redisTemplate.opsForHash().values(key); - if(groupVoList!=null){ - this.prjCache.put(key,groupVoList); - } - return groupVoList; - }else { - return groupVoList; - } - - - } - public XmGroupVo getProjectGroup(String projectId, String groupId){ - String key=this.getProjectKey(projectId); - return (XmGroupVo) redisTemplate.opsForHash().get(key,groupId); - } - public void putProjectGroup(XmGroupVo group){ - String key=this.getProjectKey(group.getProjectId()); - redisTemplate.opsForHash().put(key, group.getId(), group); - } - - /** - * - * @param projectId - */ - public void clearProjectGroup(String projectId){ - String key=this.getProjectKey(projectId); - this.clearProjectGroups(projectId); - } - public void putProjectGroups(String projectId,List groups){ - - String key=this.getProjectKey(projectId); - if(groups==null || groups.size()==0){ - this.clearProjectGroups(projectId); - return; - } - for (XmGroupVo group : groups) { - String hashKey= group.getId(); - redisTemplate.opsForHash().put(key, hashKey, group); - } - if(groups==null){ - this.prjCache.remove(key); - }else{ - this.prjCache.put(key,groups); - } - publish.push("XM_GROUP_PRJ_CACHE",projectId); - } - public void clearProjectGroups(String projectId){ - String key=this.getProjectKey(projectId); - Set keySet=redisTemplate.opsForHash().keys(key); - if(keySet!=null && keySet.size()>0){ - redisTemplate.opsForHash().delete(key,keySet.toArray()); - } - this.prjCache.remove(key); - publish.push("XM_GROUP_PRJ_CACHE",projectId); - } - - public List getProductGroups(String productId){ - String key=this.getProductKey(productId); - List groupVoList=this.prdCache.get(key); - if(groupVoList==null){ - - groupVoList= (List) redisTemplate.opsForHash().values(key); - if(groupVoList!=null){ - this.prdCache.put(key,groupVoList); - } - return groupVoList; - }else { - return groupVoList; - } - } - public XmGroupVo getProductGroup(String productId, String groupId){ - String key=this.getProductKey(productId); - return (XmGroupVo) redisTemplate.opsForHash().get(key,groupId); - } - public void putProductGroup(XmGroupVo group){ - String key=this.getProductKey(group.getProductId()); - redisTemplate.opsForHash().put(key, group.getId(), group); - } - public void clearProductGroup(String productId){ - String key=this.getProductKey(productId); - this.clearProductGroups(productId); - } - public void putProductGroups(String productId,List groups){ - String key=this.getProductKey(productId); - if(groups==null || groups.size()==0){ - this.clearProductGroups(productId); - return; - } - for (XmGroupVo group : groups) { - String hashKey= group.getId(); - redisTemplate.opsForHash().put(key, hashKey, group); - } - if(groups==null){ - this.prdCache.remove(key); - }else{ - this.prdCache.put(key,groups); - } - publish.push("XM_GROUP_PRD_CACHE",productId); - } - public void clearProductGroups(String productId){ - String key=this.getProductKey(productId); - Set keySet=redisTemplate.opsForHash().keys(key); - if(keySet!=null && keySet.size()>0){ - redisTemplate.opsForHash().delete(key,keySet.toArray()); - } - this.prdCache.remove(key); - publish.push("XM_GROUP_PRD_CACHE",productId); - } - - public void clearLocalPrjectCache(String projectId) { - this.prjCache.remove(getProjectKey(projectId)); - } - - public void clearLocalProductCache(String productId) { - this.prdCache.remove(getProductKey(productId)); - } - - /*每30分钟清除一次过期的本地缓存*/ - @Scheduled(cron = "* */30 * * * *") - public void timer() { - String currPrdKey=this.getCacheKey()+":"+currPrdDateKey+":"; - - String currPrjKey=this.getCacheKey()+":"+currPrjDateKey+":"; - for (String key : this.prdCache.keySet()) { - if(!key.startsWith(currPrdKey)){ - this.prdCache.remove(key); - } - } - for (String key : this.prjCache.keySet()) { - if(!key.startsWith(currPrjKey)){ - this.prjCache.remove(key); - } - } - } -} diff --git a/xm-core/src/main/java/com/xm/core/service/cache/XmTaskCacheService.java b/xm-core/src/main/java/com/xm/core/service/cache/XmTaskCacheService.java deleted file mode 100644 index dcde4abd..00000000 --- a/xm-core/src/main/java/com/xm/core/service/cache/XmTaskCacheService.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.xm.core.service.cache; - -import com.github.pagehelper.PageSerializable; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.stereotype.Service; - -import java.util.Calendar; -import java.util.Map; -import java.util.concurrent.TimeUnit; - -@Service -public class XmTaskCacheService { - - @Autowired - RedisTemplate redisTemplate; - - String currDateKey=""; - public String getKey(){ - Calendar currCal=Calendar.getInstance(); - String dateKey=currCal.get(Calendar.YEAR)+"-"+currCal.get(Calendar.DAY_OF_YEAR); - if(dateKey.equals(currDateKey)){ - return this.getCacheKey()+":"+dateKey; - }else { - currDateKey=dateKey; - this.redisTemplate.expire(this.getCacheKey()+":"+dateKey,24,TimeUnit.HOURS); - return this.getCacheKey()+":"+dateKey; - } - } - - String getCacheKey() { - return "xm_task"; - } - public void putTasks(String queryKeys, PageSerializable> tasks){ - String key=this.getKey(); - String hashKey=queryKeys; - redisTemplate.opsForHash().put(key, hashKey, tasks); - } - - public PageSerializable> getTasks(String queryKeys){ - String key=this.getKey(); - String hashKey=queryKeys; - return (PageSerializable>) redisTemplate.opsForHash().get(key, hashKey); - - } - -} diff --git a/xm-core/src/main/java/com/xm/core/service/cache/sub/CacheMessageListener.java b/xm-core/src/main/java/com/xm/core/service/cache/sub/CacheMessageListener.java index 61c181aa..d867863f 100644 --- a/xm-core/src/main/java/com/xm/core/service/cache/sub/CacheMessageListener.java +++ b/xm-core/src/main/java/com/xm/core/service/cache/sub/CacheMessageListener.java @@ -1,6 +1,5 @@ package com.xm.core.service.cache.sub; -import com.xm.core.service.cache.XmGroupCacheService; import com.xm.core.service.cache.XmProductCacheService; import com.xm.core.service.cache.XmProjectCacheService; import org.slf4j.Logger;