Authored by csgyoho

inbox redis添加

1 package com.yohoufo.common.redis; 1 package com.yohoufo.common.redis;
2 2
3 import com.alibaba.fastjson.JSON; 3 import com.alibaba.fastjson.JSON;
  4 +import com.alibaba.fastjson.serializer.SerializerFeature;
4 import com.yoho.core.redis.cluster.annotation.Redis; 5 import com.yoho.core.redis.cluster.annotation.Redis;
5 import com.yoho.core.redis.cluster.operations.nosync.*; 6 import com.yoho.core.redis.cluster.operations.nosync.*;
6 import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; 7 import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
@@ -9,7 +10,7 @@ import org.slf4j.Logger; @@ -9,7 +10,7 @@ import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory; 10 import org.slf4j.LoggerFactory;
10 import org.springframework.stereotype.Service; 11 import org.springframework.stereotype.Service;
11 12
12 -import java.util.Map; 13 +import java.util.List;
13 import java.util.concurrent.TimeUnit; 14 import java.util.concurrent.TimeUnit;
14 15
15 /** 16 /**
@@ -62,7 +63,7 @@ public class NoSyncGracefulRedisTemplate extends AbstractGracefulRedisTemplate { @@ -62,7 +63,7 @@ public class NoSyncGracefulRedisTemplate extends AbstractGracefulRedisTemplate {
62 return false; 63 return false;
63 } 64 }
64 65
65 - public <T> T get(RedisKeyBuilder key,String hashKey, Class<T> clazz) { 66 + public <T> List<T> getList(RedisKeyBuilder key, String hashKey, Class<T> clazz) {
66 logger.info("NoSyncGracefulRedisTemplate get cache,key={}",key); 67 logger.info("NoSyncGracefulRedisTemplate get cache,key={}",key);
67 if (isCache()) { 68 if (isCache()) {
68 return null; 69 return null;
@@ -71,7 +72,7 @@ public class NoSyncGracefulRedisTemplate extends AbstractGracefulRedisTemplate { @@ -71,7 +72,7 @@ public class NoSyncGracefulRedisTemplate extends AbstractGracefulRedisTemplate {
71 try { 72 try {
72 String value = hashOperations.get(key, hashKey); 73 String value = hashOperations.get(key, hashKey);
73 if ( org.springframework.util.StringUtils.hasText( value ) ) { 74 if ( org.springframework.util.StringUtils.hasText( value ) ) {
74 - return JSON.parseObject( value, clazz ); 75 + return JSON.parseArray( value, clazz );
75 } else { 76 } else {
76 logger.info( "NoSyncGracefulRedisTemplate can not get data from redis by key {}.", key ); 77 logger.info( "NoSyncGracefulRedisTemplate can not get data from redis by key {}.", key );
77 return null; 78 return null;
@@ -90,27 +91,15 @@ public class NoSyncGracefulRedisTemplate extends AbstractGracefulRedisTemplate { @@ -90,27 +91,15 @@ public class NoSyncGracefulRedisTemplate extends AbstractGracefulRedisTemplate {
90 } 91 }
91 92
92 93
93 - public void put(RedisKeyBuilder key, String hashKey, String value,long timeout, TimeUnit unit) { 94 + public void put(RedisKeyBuilder key, String hashKey, Object value,long timeout) {
94 try { 95 try {
95 - hashOperations.putIfAbsent(key, hashKey, value);  
96 - redis.longExpire(key, timeout, unit); 96 + String redisValue = JSON.toJSONString( value, SerializerFeature.WriteClassName, SerializerFeature.DisableCircularReferenceDetect );
  97 + hashOperations.putIfAbsent(key, hashKey, redisValue);
  98 + redis.longExpire(key, timeout, TimeUnit.SECONDS);
97 } catch (Exception e) { 99 } catch (Exception e) {
98 logger.warn( 100 logger.warn(
99 "NoSyncGracefulRedisTemplate call redis hash put error,key is {},fieldKey is {},value is {},error is {}", 101 "NoSyncGracefulRedisTemplate call redis hash put error,key is {},fieldKey is {},value is {},error is {}",
100 key, hashKey, value, e); 102 key, hashKey, value, e);
101 } 103 }
102 } 104 }
103 -  
104 - public void delete(RedisKeyBuilder key) {  
105 - try {  
106 - Map<String, String> map = hashOperations.entries(key);  
107 - if (map != null && map.size() > 0) {  
108 - hashOperations.delete(key, map.keySet().toArray());  
109 - }  
110 - } catch (Exception e) {  
111 - logger.warn(  
112 - "NoSyncGracefulRedisTemplate call redis hash delete error,key is {},error is {}",  
113 - key, e);  
114 - }  
115 - }  
116 } 105 }
@@ -124,13 +124,10 @@ public class InBoxServiceImpl implements IInBoxService { @@ -124,13 +124,10 @@ public class InBoxServiceImpl implements IInBoxService {
124 } 124 }
125 125
126 private void setInboxByRedis(ListInboxReqVO reqVO, List<InBox> inBoxes, int total) { 126 private void setInboxByRedis(ListInboxReqVO reqVO, List<InBox> inBoxes, int total) {
127 - RedisKeyBuilder inboxKey = CacheEnum.USERS_INBOX_LIST.generateKey(reqVO.getUid(),reqVO.getType(),reqVO.getPage(),reqVO.getLimit());  
128 - if(null == inBoxes){  
129 - redisTemplate.delete(inboxKey);  
130 - }else{  
131 - redisTemplate.setEx(inboxKey,inBoxes,CacheEnum.USERS_INBOX_LIST.getCacheTime());  
132 - }  
133 - RedisKeyBuilder inboxTotalKey = CacheEnum.USERS_INBOX_LIST_TOTAL.generateKey(reqVO.getUid(),reqVO.getType()); 127 + RedisKeyBuilder inboxKey = CacheEnum.USERS_INBOX_LIST.generateKey(reqVO.getUid());
  128 + redisTemplate.put(inboxKey, CacheKeyHelper.getInboxRedisHashKey(reqVO.getType(),reqVO.getPage(),reqVO.getLimit()),
  129 + inBoxes,CacheEnum.USERS_INBOX_LIST.getCacheTime());
  130 + RedisKeyBuilder inboxTotalKey = CacheEnum.USERS_INBOX_LIST_TOTAL.generateKey(reqVO.getUid(),reqVO.getType() == null ?"N":reqVO.getType());
134 redisTemplate.setEx(inboxTotalKey,total,CacheEnum.USERS_INBOX_LIST_TOTAL.getCacheTime()); 131 redisTemplate.setEx(inboxTotalKey,total,CacheEnum.USERS_INBOX_LIST_TOTAL.getCacheTime());
135 } 132 }
136 133
@@ -146,9 +143,9 @@ public class InBoxServiceImpl implements IInBoxService { @@ -146,9 +143,9 @@ public class InBoxServiceImpl implements IInBoxService {
146 private PageResponseVO<InBox> listInboxByRedis(ListInboxReqVO reqVO) { 143 private PageResponseVO<InBox> listInboxByRedis(ListInboxReqVO reqVO) {
147 PageResponseVO<InBox> response = new PageResponseVO<>(); 144 PageResponseVO<InBox> response = new PageResponseVO<>();
148 RedisKeyBuilder inboxKey = CacheEnum.USERS_INBOX_LIST.generateKey(reqVO.getUid()); 145 RedisKeyBuilder inboxKey = CacheEnum.USERS_INBOX_LIST.generateKey(reqVO.getUid());
149 - List<InBox> inboxes = redisTemplate.get(inboxKey, CacheKeyHelper.getInboxRedisHashKey(reqVO.getType(),reqVO.getPage(),reqVO.getLimit()),List.class);  
150 - RedisKeyBuilder inboxTotalKey = CacheEnum.USERS_INBOX_LIST_TOTAL.generateKey(reqVO.getUid());  
151 - Integer total = redisTemplate.get(inboxTotalKey,CacheKeyHelper.getInboxTotalRedisHashKey(reqVO.getType()),Integer.class); 146 + List<InBox> inboxes = redisTemplate.getList(inboxKey, CacheKeyHelper.getInboxRedisHashKey(reqVO.getType(),reqVO.getPage(),reqVO.getLimit()),InBox.class);
  147 + RedisKeyBuilder inboxTotalKey = CacheEnum.USERS_INBOX_LIST_TOTAL.generateKey(reqVO.getUid(),reqVO.getType() == null ?"N":reqVO.getType());
  148 + Integer total = redisTemplate.get(inboxTotalKey,Integer.class);
152 if(inboxes == null || total == null){ 149 if(inboxes == null || total == null){
153 log.info("listInboxByRedis cache is empty.inboxes is {},inboxKey is {}," + 150 log.info("listInboxByRedis cache is empty.inboxes is {},inboxKey is {}," +
154 "total is {},totalKey is {}",inboxes,inboxKey,total,inboxTotalKey); 151 "total is {},totalKey is {}",inboxes,inboxKey,total,inboxTotalKey);
@@ -179,14 +176,19 @@ public class InBoxServiceImpl implements IInBoxService { @@ -179,14 +176,19 @@ public class InBoxServiceImpl implements IInBoxService {
179 inBox.setContent(createContent(businessTypeEnum.getContent(),params)); 176 inBox.setContent(createContent(businessTypeEnum.getContent(),params));
180 inBox.setBusinessType(businessType); 177 inBox.setBusinessType(businessType);
181 inBoxDao.insertInbox(getTableName(inBox.getUid()),inBox); 178 inBoxDao.insertInbox(getTableName(inBox.getUid()),inBox);
182 - deleteIboxsByRedis(uid); 179 + deleteIboxsByRedis(uid,type);
183 } 180 }
184 181
185 - private void deleteIboxsByRedis(int uid){ 182 + private void deleteIboxsByRedis(int uid,int type){
  183 + log.info("deleteIboxsByRedis params uid is {} type is {}",uid,type);
186 RedisKeyBuilder inboxKey = CacheEnum.USERS_INBOX_LIST.generateKey(uid); 184 RedisKeyBuilder inboxKey = CacheEnum.USERS_INBOX_LIST.generateKey(uid);
187 redisTemplate.delete(inboxKey); 185 redisTemplate.delete(inboxKey);
188 - RedisKeyBuilder inboxTotalKey = CacheEnum.USERS_INBOX_LIST_TOTAL.generateKey(uid); 186 + RedisKeyBuilder key = CacheEnum.USERS_INBOX_TYPE_UNREADCOUNT.generateKey(uid,type);
  187 + redisTemplate.delete(key);
  188 + RedisKeyBuilder inboxTotalKey = CacheEnum.USERS_INBOX_LIST_TOTAL.generateKey(uid,"N");
189 redisTemplate.delete(inboxTotalKey); 189 redisTemplate.delete(inboxTotalKey);
  190 + RedisKeyBuilder inboxTypeTotalKey = CacheEnum.USERS_INBOX_LIST_TOTAL.generateKey(uid,type);
  191 + redisTemplate.delete(inboxTypeTotalKey);
190 } 192 }
191 193
192 private String createContent(String template, String params){ 194 private String createContent(String template, String params){