Browse Source

分布式本地缓存

master
陈裕财 3 years ago
parent
commit
22faad356e
  1. 52
      xm-core/src/main/java/com/xm/core/service/cache/XmGroupCacheService.java
  2. 25
      xm-core/src/main/java/com/xm/core/service/cache/XmProductCacheService.java
  3. 28
      xm-core/src/main/java/com/xm/core/service/cache/XmProjectCacheService.java

52
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<String, List<XmGroupVo>> cache=new ConcurrentHashMap<>();
Map<String, List<XmGroupVo>> prjCache =new ConcurrentHashMap<>();
Map<String, List<XmGroupVo>> prdCache =new ConcurrentHashMap<>();
@Autowired
Publish publish;
@ -55,13 +57,12 @@ public class XmGroupCacheService {
}
public List<XmGroupVo> getProjectGroups(String projectId){
List<XmGroupVo> groupVoList=this.cache.get("prj_"+projectId);
if(groupVoList==null){
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.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<XmGroupVo> getProductGroups(String productId){
List<XmGroupVo> groupVoList=this.cache.get("prd_"+productId);
if(groupVoList==null){
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.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);
}
}
}
}

25
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);
if(product==null){
String key=this.getKey();
XmProduct product=cache.get(key+productId);
if(product==null){
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);
}
}
}
}

28
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);
if(project==null) {
String key = this.getKey();
XmProject project=cache.get(key+projectId);
if(project==null) {
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);
}
}
}
}
Loading…
Cancel
Save