Browse Source

分布式本地缓存

master
陈裕财 3 years ago
parent
commit
412ac900ac
  1. 46
      xm-core/src/main/java/com/xm/core/service/cache/XmGroupCacheService.java
  2. 6
      xm-core/src/main/java/com/xm/core/service/cache/XmProductCacheService.java
  3. 7
      xm-core/src/main/java/com/xm/core/service/cache/XmProjectCacheService.java
  4. 11
      xm-core/src/main/java/com/xm/core/service/cache/sub/CacheMessageListener.java

46
xm-core/src/main/java/com/xm/core/service/cache/XmGroupCacheService.java

@ -1,5 +1,6 @@
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;
@ -7,7 +8,9 @@ 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
@ -16,6 +19,11 @@ public class XmGroupCacheService {
@Autowired
RedisTemplate redisTemplate;
Map<String, List<XmGroupVo>> cache=new ConcurrentHashMap<>();
@Autowired
Publish publish;
String currPrdDateKey="";
public String getProductKey(String productId){
Calendar currCal=Calendar.getInstance();
@ -47,8 +55,16 @@ public class XmGroupCacheService {
}
public List<XmGroupVo> getProjectGroups(String projectId){
String key=this.getProjectKey(projectId);
return (List<XmGroupVo>) redisTemplate.opsForHash().values(key);
List<XmGroupVo> groupVoList=this.cache.get("prj_"+projectId);
if(groupVoList==null){
String key=this.getProjectKey(projectId);
groupVoList= (List<XmGroupVo>) redisTemplate.opsForHash().values(key);
this.cache.put("prj_"+projectId,groupVoList);
return groupVoList;
}else {
return groupVoList;
}
}
public XmGroupVo getProjectGroup(String projectId, String groupId){
@ -69,6 +85,7 @@ public class XmGroupCacheService {
this.clearProjectGroups(projectId);
}
public void putProjectGroups(String projectId,List<XmGroupVo> groups){
String key=this.getProjectKey(projectId);
if(groups==null || groups.size()==0){
this.clearProjectGroups(projectId);
@ -78,6 +95,12 @@ public class XmGroupCacheService {
String hashKey= group.getId();
redisTemplate.opsForHash().put(key, hashKey, group);
}
if(groups==null){
this.cache.remove("prj_"+projectId);
}else{
this.cache.put("prj_"+projectId,groups);
}
publish.push("XM_GROUP_PRJ_CACHE",projectId);
}
public void clearProjectGroups(String projectId){
String key=this.getProjectKey(projectId);
@ -85,6 +108,8 @@ public class XmGroupCacheService {
if(keySet!=null && keySet.size()>0){
redisTemplate.opsForHash().delete(key,keySet.toArray());
}
this.cache.remove("prj_"+projectId);
publish.push("XM_GROUP_PRJ_CACHE",projectId);
}
public List<XmGroupVo> getProductGroups(String productId){
@ -114,6 +139,12 @@ public class XmGroupCacheService {
String hashKey= group.getId();
redisTemplate.opsForHash().put(key, hashKey, group);
}
if(groups==null){
this.cache.remove("prd_"+productId);
}else{
this.cache.put("prd_"+productId,groups);
}
publish.push("XM_GROUP_PRD_CACHE",productId);
}
public void clearProductGroups(String productId){
String key=this.getProductKey(productId);
@ -121,6 +152,15 @@ public class XmGroupCacheService {
if(keySet!=null && keySet.size()>0){
redisTemplate.opsForHash().delete(key,keySet.toArray());
}
this.cache.remove("prd_"+productId);
publish.push("XM_GROUP_PRD_CACHE",productId);
}
public void clearLocalPrjectCache(String projectId) {
this.cache.remove("prj_"+projectId);
}
public void clearLocalProductCache(String productId) {
this.cache.remove("prd_"+productId);
}
}

6
xm-core/src/main/java/com/xm/core/service/cache/XmProductCacheService.java

@ -41,7 +41,11 @@ public class XmProductCacheService {
String key=this.getKey();
String hashKey=productId;
redisTemplate.opsForHash().put(key, hashKey, product);
cache.put(productId,product);
if(product!=null){
cache.put(productId,product);
}else{
cache.remove(productId);
}
publish.push("XM_PRODUCT_CACHE",productId);
}

7
xm-core/src/main/java/com/xm/core/service/cache/XmProjectCacheService.java

@ -41,7 +41,12 @@ public class XmProjectCacheService {
String key=this.getKey();
String hashKey=projectId;
redisTemplate.opsForHash().put(key, hashKey, project);
this.cache.put(projectId,project);
if(project!=null){
this.cache.put(projectId,project);
}else{
this.cache.remove(projectId);
}
publish.push("XM_PROJECT_CACHE",projectId);
}
public XmProject getProject(String projectId){

11
xm-core/src/main/java/com/xm/core/service/cache/sub/CacheMessageListener.java

@ -1,5 +1,6 @@
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;
@ -29,6 +30,10 @@ public class CacheMessageListener implements MessageListener {
XmProjectCacheService xmProjectCacheService;
@Autowired
XmGroupCacheService xmGroupCacheService;
private static final Logger logger = LoggerFactory.getLogger(CacheMessageListener.class);
/*定时心跳*/
@ -55,8 +60,10 @@ public class CacheMessageListener implements MessageListener {
xmProductCacheService.clearLocalCache(msg);
}else if(channelName.startsWith("XM_PROJECT")) {
xmProjectCacheService.clearLocalCache(msg);
}else if(channelName.startsWith("XM_GROUP")) {
}else if(channelName.startsWith("XM_GROUP_PRJ")) {
xmGroupCacheService.clearLocalPrjectCache(msg);
}else if(channelName.startsWith("XM_GROUP_PRD")) {
xmGroupCacheService.clearLocalProductCache(msg);
}
} catch (Exception e) {
logger.error("",e);

Loading…
Cancel
Save