From 22faad356ee126d66d1a0ac9fd611f6205383329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E8=A3=95=E8=B4=A2?= Date: Sat, 28 Jan 2023 02:08:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E5=B8=83=E5=BC=8F=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/cache/XmGroupCacheService.java | 52 +++++++++++++------ .../service/cache/XmProductCacheService.java | 25 ++++++--- .../service/cache/XmProjectCacheService.java | 28 +++++++--- 3 files changed, 75 insertions(+), 30 deletions(-) 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 index 9f99b32d..736619a8 100644 --- 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 @@ -4,6 +4,7 @@ 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; @@ -19,7 +20,8 @@ public class XmGroupCacheService { @Autowired RedisTemplate redisTemplate; - Map> cache=new ConcurrentHashMap<>(); + Map> prjCache =new ConcurrentHashMap<>(); + Map> prdCache =new ConcurrentHashMap<>(); @Autowired Publish publish; @@ -55,13 +57,12 @@ public class XmGroupCacheService { } public List getProjectGroups(String projectId){ - List groupVoList=this.cache.get("prj_"+projectId); + String key=this.getProjectKey(projectId); + List groupVoList=this.prjCache.get(key); if(groupVoList==null){ - String key=this.getProjectKey(projectId); groupVoList= (List) redisTemplate.opsForHash().values(key); - if(groupVoList!=null){ - this.cache.put("prj_"+projectId,groupVoList); + this.prjCache.put(key,groupVoList); } return groupVoList; }else { @@ -99,9 +100,9 @@ public class XmGroupCacheService { redisTemplate.opsForHash().put(key, hashKey, group); } if(groups==null){ - this.cache.remove("prj_"+projectId); + this.prjCache.remove(key); }else{ - this.cache.put("prj_"+projectId,groups); + this.prjCache.put(key,groups); } publish.push("XM_GROUP_PRJ_CACHE",projectId); } @@ -111,17 +112,18 @@ public class XmGroupCacheService { if(keySet!=null && keySet.size()>0){ redisTemplate.opsForHash().delete(key,keySet.toArray()); } - this.cache.remove("prj_"+projectId); + this.prjCache.remove(key); publish.push("XM_GROUP_PRJ_CACHE",projectId); } public List getProductGroups(String productId){ - List groupVoList=this.cache.get("prd_"+productId); + String key=this.getProductKey(productId); + List groupVoList=this.prdCache.get(key); if(groupVoList==null){ - String key=this.getProductKey(productId); + groupVoList= (List) redisTemplate.opsForHash().values(key); if(groupVoList!=null){ - this.cache.put("prd_"+productId,groupVoList); + this.prdCache.put(key,groupVoList); } return groupVoList; }else { @@ -151,9 +153,9 @@ public class XmGroupCacheService { redisTemplate.opsForHash().put(key, hashKey, group); } if(groups==null){ - this.cache.remove("prd_"+productId); + this.prdCache.remove(key); }else{ - this.cache.put("prd_"+productId,groups); + this.prdCache.put(key,groups); } publish.push("XM_GROUP_PRD_CACHE",productId); } @@ -163,15 +165,33 @@ public class XmGroupCacheService { if(keySet!=null && keySet.size()>0){ redisTemplate.opsForHash().delete(key,keySet.toArray()); } - this.cache.remove("prd_"+productId); + this.prdCache.remove(key); publish.push("XM_GROUP_PRD_CACHE",productId); } public void clearLocalPrjectCache(String projectId) { - this.cache.remove("prj_"+projectId); + this.prjCache.remove(getProjectKey(projectId)); } public void clearLocalProductCache(String productId) { - this.cache.remove("prd_"+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/XmProductCacheService.java b/xm-core/src/main/java/com/xm/core/service/cache/XmProductCacheService.java index 94761126..a6ffd722 100644 --- a/xm-core/src/main/java/com/xm/core/service/cache/XmProductCacheService.java +++ b/xm-core/src/main/java/com/xm/core/service/cache/XmProductCacheService.java @@ -4,6 +4,7 @@ import com.mdp.mq.sp.Publish; import com.xm.core.entity.XmProduct; 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; @@ -42,21 +43,21 @@ public class XmProductCacheService { String hashKey=productId; redisTemplate.opsForHash().put(key, hashKey, product); if(product!=null){ - cache.put(productId,product); + cache.put(key+hashKey,product); }else{ - cache.remove(productId); + cache.remove(key+hashKey); } publish.push("XM_PRODUCT_CACHE",productId); } public XmProduct getProduct(String productId){ - XmProduct product=cache.get(productId); + String key=this.getKey(); + XmProduct product=cache.get(key+productId); if(product==null){ - String key=this.getKey(); String hashKey=productId; product= (XmProduct) redisTemplate.opsForHash().get(key, hashKey); if(product!=null){ - cache.put(productId,product); + cache.put(key+hashKey,product); } return product; }else { @@ -67,13 +68,23 @@ public class XmProductCacheService { public void clear(String productId){ String key=this.getKey(); - cache.remove(productId); + cache.remove(key+productId); redisTemplate.opsForHash().delete(key,productId); publish.push("XM_PRODUCT_CACHE",productId); } public void clearLocalCache(String productId) { - this.cache.remove(productId); + this.cache.remove(getKey()+productId); + } + + @Scheduled(cron = "* */30 * * * *") + public void timer() { + String currPrdKey=this.getKey(); + for (String key : this.cache.keySet()) { + if(!key.startsWith(currPrdKey)){ + this.cache.remove(key); + } + } } } diff --git a/xm-core/src/main/java/com/xm/core/service/cache/XmProjectCacheService.java b/xm-core/src/main/java/com/xm/core/service/cache/XmProjectCacheService.java index 73c03710..f85200cb 100644 --- a/xm-core/src/main/java/com/xm/core/service/cache/XmProjectCacheService.java +++ b/xm-core/src/main/java/com/xm/core/service/cache/XmProjectCacheService.java @@ -4,6 +4,7 @@ import com.mdp.mq.sp.Publish; import com.xm.core.entity.XmProject; 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; @@ -42,20 +43,23 @@ public class XmProjectCacheService { String hashKey=projectId; redisTemplate.opsForHash().put(key, hashKey, project); if(project!=null){ - this.cache.put(projectId,project); + this.cache.put(hashKey+hashKey,project); }else{ - this.cache.remove(projectId); + this.cache.remove(hashKey+hashKey); } publish.push("XM_PROJECT_CACHE",projectId); } public XmProject getProject(String projectId){ - XmProject project=cache.get(projectId); + + String key = this.getKey(); + XmProject project=cache.get(key+projectId); if(project==null) { - String key = this.getKey(); String hashKey = projectId; project= (XmProject) redisTemplate.opsForHash().get(key, hashKey); - cache.put(projectId,project); + if(project!=null){ + cache.put(hashKey+hashKey,project); + } return project; }else{ return project; @@ -64,13 +68,23 @@ public class XmProjectCacheService { } public void clear(String projectId){ String key=this.getKey(); - cache.remove(projectId); + cache.remove(key+projectId); redisTemplate.opsForHash().delete(key,projectId); publish.push("XM_PROJECT_CACHE",projectId); } public void clearLocalCache(String projectId) { - this.cache.remove(projectId); + this.cache.remove(getKey()+projectId); + } + + @Scheduled(cron = "* */30 * * * *") + public void timer() { + String currPrdKey=this.getKey(); + for (String key : this.cache.keySet()) { + if(!key.startsWith(currPrdKey)){ + this.cache.remove(key); + } + } } }