Authored by csgyoho

inbox redis添加

package com.yohoufo.common.redis;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.yoho.core.redis.cluster.annotation.Redis;
import com.yoho.core.redis.cluster.operations.nosync.*;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
... ... @@ -9,7 +10,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
... ... @@ -62,7 +63,7 @@ public class NoSyncGracefulRedisTemplate extends AbstractGracefulRedisTemplate {
return false;
}
public <T> T get(RedisKeyBuilder key,String hashKey, Class<T> clazz) {
public <T> List<T> getList(RedisKeyBuilder key, String hashKey, Class<T> clazz) {
logger.info("NoSyncGracefulRedisTemplate get cache,key={}",key);
if (isCache()) {
return null;
... ... @@ -71,7 +72,7 @@ public class NoSyncGracefulRedisTemplate extends AbstractGracefulRedisTemplate {
try {
String value = hashOperations.get(key, hashKey);
if ( org.springframework.util.StringUtils.hasText( value ) ) {
return JSON.parseObject( value, clazz );
return JSON.parseArray( value, clazz );
} else {
logger.info( "NoSyncGracefulRedisTemplate can not get data from redis by key {}.", key );
return null;
... ... @@ -90,27 +91,15 @@ public class NoSyncGracefulRedisTemplate extends AbstractGracefulRedisTemplate {
}
public void put(RedisKeyBuilder key, String hashKey, String value,long timeout, TimeUnit unit) {
public void put(RedisKeyBuilder key, String hashKey, Object value,long timeout) {
try {
hashOperations.putIfAbsent(key, hashKey, value);
redis.longExpire(key, timeout, unit);
String redisValue = JSON.toJSONString( value, SerializerFeature.WriteClassName, SerializerFeature.DisableCircularReferenceDetect );
hashOperations.putIfAbsent(key, hashKey, redisValue);
redis.longExpire(key, timeout, TimeUnit.SECONDS);
} catch (Exception e) {
logger.warn(
"NoSyncGracefulRedisTemplate call redis hash put error,key is {},fieldKey is {},value is {},error is {}",
key, hashKey, value, e);
}
}
public void delete(RedisKeyBuilder key) {
try {
Map<String, String> map = hashOperations.entries(key);
if (map != null && map.size() > 0) {
hashOperations.delete(key, map.keySet().toArray());
}
} catch (Exception e) {
logger.warn(
"NoSyncGracefulRedisTemplate call redis hash delete error,key is {},error is {}",
key, e);
}
}
}
... ...
... ... @@ -124,13 +124,10 @@ public class InBoxServiceImpl implements IInBoxService {
}
private void setInboxByRedis(ListInboxReqVO reqVO, List<InBox> inBoxes, int total) {
RedisKeyBuilder inboxKey = CacheEnum.USERS_INBOX_LIST.generateKey(reqVO.getUid(),reqVO.getType(),reqVO.getPage(),reqVO.getLimit());
if(null == inBoxes){
redisTemplate.delete(inboxKey);
}else{
redisTemplate.setEx(inboxKey,inBoxes,CacheEnum.USERS_INBOX_LIST.getCacheTime());
}
RedisKeyBuilder inboxTotalKey = CacheEnum.USERS_INBOX_LIST_TOTAL.generateKey(reqVO.getUid(),reqVO.getType());
RedisKeyBuilder inboxKey = CacheEnum.USERS_INBOX_LIST.generateKey(reqVO.getUid());
redisTemplate.put(inboxKey, CacheKeyHelper.getInboxRedisHashKey(reqVO.getType(),reqVO.getPage(),reqVO.getLimit()),
inBoxes,CacheEnum.USERS_INBOX_LIST.getCacheTime());
RedisKeyBuilder inboxTotalKey = CacheEnum.USERS_INBOX_LIST_TOTAL.generateKey(reqVO.getUid(),reqVO.getType() == null ?"N":reqVO.getType());
redisTemplate.setEx(inboxTotalKey,total,CacheEnum.USERS_INBOX_LIST_TOTAL.getCacheTime());
}
... ... @@ -146,9 +143,9 @@ public class InBoxServiceImpl implements IInBoxService {
private PageResponseVO<InBox> listInboxByRedis(ListInboxReqVO reqVO) {
PageResponseVO<InBox> response = new PageResponseVO<>();
RedisKeyBuilder inboxKey = CacheEnum.USERS_INBOX_LIST.generateKey(reqVO.getUid());
List<InBox> inboxes = redisTemplate.get(inboxKey, CacheKeyHelper.getInboxRedisHashKey(reqVO.getType(),reqVO.getPage(),reqVO.getLimit()),List.class);
RedisKeyBuilder inboxTotalKey = CacheEnum.USERS_INBOX_LIST_TOTAL.generateKey(reqVO.getUid());
Integer total = redisTemplate.get(inboxTotalKey,CacheKeyHelper.getInboxTotalRedisHashKey(reqVO.getType()),Integer.class);
List<InBox> inboxes = redisTemplate.getList(inboxKey, CacheKeyHelper.getInboxRedisHashKey(reqVO.getType(),reqVO.getPage(),reqVO.getLimit()),InBox.class);
RedisKeyBuilder inboxTotalKey = CacheEnum.USERS_INBOX_LIST_TOTAL.generateKey(reqVO.getUid(),reqVO.getType() == null ?"N":reqVO.getType());
Integer total = redisTemplate.get(inboxTotalKey,Integer.class);
if(inboxes == null || total == null){
log.info("listInboxByRedis cache is empty.inboxes is {},inboxKey is {}," +
"total is {},totalKey is {}",inboxes,inboxKey,total,inboxTotalKey);
... ... @@ -179,14 +176,19 @@ public class InBoxServiceImpl implements IInBoxService {
inBox.setContent(createContent(businessTypeEnum.getContent(),params));
inBox.setBusinessType(businessType);
inBoxDao.insertInbox(getTableName(inBox.getUid()),inBox);
deleteIboxsByRedis(uid);
deleteIboxsByRedis(uid,type);
}
private void deleteIboxsByRedis(int uid){
private void deleteIboxsByRedis(int uid,int type){
log.info("deleteIboxsByRedis params uid is {} type is {}",uid,type);
RedisKeyBuilder inboxKey = CacheEnum.USERS_INBOX_LIST.generateKey(uid);
redisTemplate.delete(inboxKey);
RedisKeyBuilder inboxTotalKey = CacheEnum.USERS_INBOX_LIST_TOTAL.generateKey(uid);
RedisKeyBuilder key = CacheEnum.USERS_INBOX_TYPE_UNREADCOUNT.generateKey(uid,type);
redisTemplate.delete(key);
RedisKeyBuilder inboxTotalKey = CacheEnum.USERS_INBOX_LIST_TOTAL.generateKey(uid,"N");
redisTemplate.delete(inboxTotalKey);
RedisKeyBuilder inboxTypeTotalKey = CacheEnum.USERS_INBOX_LIST_TOTAL.generateKey(uid,type);
redisTemplate.delete(inboxTypeTotalKey);
}
private String createContent(String template, String params){
... ...