陈裕财 2 years ago
parent
commit
effd011a27
  1. 1
      xm-core/src/main/java/com/xm/core/ctrl/XmTaskController.java
  2. 1
      xm-core/src/main/java/com/xm/core/service/XmGroupService.java
  3. 197
      xm-core/src/main/java/com/xm/core/service/cache/XmGroupCacheService.java
  4. 47
      xm-core/src/main/java/com/xm/core/service/cache/XmTaskCacheService.java
  5. 1
      xm-core/src/main/java/com/xm/core/service/cache/sub/CacheMessageListener.java

1
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.*;

1
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;

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

@ -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<String, List<XmGroupVo>> prjCache =new ConcurrentHashMap<>();
Map<String, List<XmGroupVo>> 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<XmGroupVo> getProjectGroups(String projectId){
String key=this.getProjectKey(projectId);
List<XmGroupVo> groupVoList=this.prjCache.get(key);
if(groupVoList==null){
groupVoList= (List<XmGroupVo>) 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<XmGroupVo> 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<String> 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<XmGroupVo> getProductGroups(String productId){
String key=this.getProductKey(productId);
List<XmGroupVo> groupVoList=this.prdCache.get(key);
if(groupVoList==null){
groupVoList= (List<XmGroupVo>) 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<XmGroupVo> 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<String> 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);
}
}
}
}

47
xm-core/src/main/java/com/xm/core/service/cache/XmTaskCacheService.java

@ -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<Map<String,Object>> tasks){
String key=this.getKey();
String hashKey=queryKeys;
redisTemplate.opsForHash().put(key, hashKey, tasks);
}
public PageSerializable<Map<String,Object>> getTasks(String queryKeys){
String key=this.getKey();
String hashKey=queryKeys;
return (PageSerializable<Map<String,Object>>) redisTemplate.opsForHash().get(key, hashKey);
}
}

1
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;

Loading…
Cancel
Save