Browse Source

分布式本地缓存

master
陈裕财 3 years ago
parent
commit
412ac900ac
  1. 42
      xm-core/src/main/java/com/xm/core/service/cache/XmGroupCacheService.java
  2. 4
      xm-core/src/main/java/com/xm/core/service/cache/XmProductCacheService.java
  3. 5
      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

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

@ -1,5 +1,6 @@
package com.xm.core.service.cache; package com.xm.core.service.cache;
import com.mdp.mq.sp.Publish;
import com.xm.core.vo.XmGroupVo; import com.xm.core.vo.XmGroupVo;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
@ -7,7 +8,9 @@ import org.springframework.stereotype.Service;
import java.util.Calendar; import java.util.Calendar;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@Service @Service
@ -16,6 +19,11 @@ public class XmGroupCacheService {
@Autowired @Autowired
RedisTemplate redisTemplate; RedisTemplate redisTemplate;
Map<String, List<XmGroupVo>> cache=new ConcurrentHashMap<>();
@Autowired
Publish publish;
String currPrdDateKey=""; String currPrdDateKey="";
public String getProductKey(String productId){ public String getProductKey(String productId){
Calendar currCal=Calendar.getInstance(); Calendar currCal=Calendar.getInstance();
@ -47,8 +55,16 @@ public class XmGroupCacheService {
} }
public List<XmGroupVo> getProjectGroups(String projectId){ public List<XmGroupVo> getProjectGroups(String projectId){
List<XmGroupVo> groupVoList=this.cache.get("prj_"+projectId);
if(groupVoList==null){
String key=this.getProjectKey(projectId); String key=this.getProjectKey(projectId);
return (List<XmGroupVo>) redisTemplate.opsForHash().values(key);
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){ public XmGroupVo getProjectGroup(String projectId, String groupId){
@ -69,6 +85,7 @@ public class XmGroupCacheService {
this.clearProjectGroups(projectId); this.clearProjectGroups(projectId);
} }
public void putProjectGroups(String projectId,List<XmGroupVo> groups){ public void putProjectGroups(String projectId,List<XmGroupVo> groups){
String key=this.getProjectKey(projectId); String key=this.getProjectKey(projectId);
if(groups==null || groups.size()==0){ if(groups==null || groups.size()==0){
this.clearProjectGroups(projectId); this.clearProjectGroups(projectId);
@ -78,6 +95,12 @@ public class XmGroupCacheService {
String hashKey= group.getId(); String hashKey= group.getId();
redisTemplate.opsForHash().put(key, hashKey, group); 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){ public void clearProjectGroups(String projectId){
String key=this.getProjectKey(projectId); String key=this.getProjectKey(projectId);
@ -85,6 +108,8 @@ public class XmGroupCacheService {
if(keySet!=null && keySet.size()>0){ if(keySet!=null && keySet.size()>0){
redisTemplate.opsForHash().delete(key,keySet.toArray()); redisTemplate.opsForHash().delete(key,keySet.toArray());
} }
this.cache.remove("prj_"+projectId);
publish.push("XM_GROUP_PRJ_CACHE",projectId);
} }
public List<XmGroupVo> getProductGroups(String productId){ public List<XmGroupVo> getProductGroups(String productId){
@ -114,6 +139,12 @@ public class XmGroupCacheService {
String hashKey= group.getId(); String hashKey= group.getId();
redisTemplate.opsForHash().put(key, hashKey, group); 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){ public void clearProductGroups(String productId){
String key=this.getProductKey(productId); String key=this.getProductKey(productId);
@ -121,6 +152,15 @@ public class XmGroupCacheService {
if(keySet!=null && keySet.size()>0){ if(keySet!=null && keySet.size()>0){
redisTemplate.opsForHash().delete(key,keySet.toArray()); 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);
}
} }

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

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

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

@ -41,7 +41,12 @@ public class XmProjectCacheService {
String key=this.getKey(); String key=this.getKey();
String hashKey=projectId; String hashKey=projectId;
redisTemplate.opsForHash().put(key, hashKey, project); redisTemplate.opsForHash().put(key, hashKey, project);
if(project!=null){
this.cache.put(projectId,project); this.cache.put(projectId,project);
}else{
this.cache.remove(projectId);
}
publish.push("XM_PROJECT_CACHE",projectId);
} }
public XmProject getProject(String 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; 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.XmProductCacheService;
import com.xm.core.service.cache.XmProjectCacheService; import com.xm.core.service.cache.XmProjectCacheService;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -29,6 +30,10 @@ public class CacheMessageListener implements MessageListener {
XmProjectCacheService xmProjectCacheService; XmProjectCacheService xmProjectCacheService;
@Autowired
XmGroupCacheService xmGroupCacheService;
private static final Logger logger = LoggerFactory.getLogger(CacheMessageListener.class); private static final Logger logger = LoggerFactory.getLogger(CacheMessageListener.class);
/*定时心跳*/ /*定时心跳*/
@ -55,8 +60,10 @@ public class CacheMessageListener implements MessageListener {
xmProductCacheService.clearLocalCache(msg); xmProductCacheService.clearLocalCache(msg);
}else if(channelName.startsWith("XM_PROJECT")) { }else if(channelName.startsWith("XM_PROJECT")) {
xmProjectCacheService.clearLocalCache(msg); 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) { } catch (Exception e) {
logger.error("",e); logger.error("",e);

Loading…
Cancel
Save