|
|
@ -1,9 +1,9 @@ |
|
|
|
package com.epmet.redis; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.epmet.commons.tools.constant.NumConstant; |
|
|
|
import com.epmet.commons.tools.redis.RedisKeys; |
|
|
|
import com.epmet.commons.tools.redis.RedisUtils; |
|
|
|
import com.epmet.commons.tools.utils.ConvertUtils; |
|
|
|
import com.epmet.constant.BadgeConstant; |
|
|
@ -109,7 +109,7 @@ public class UserBadgeRedis { |
|
|
|
*/ |
|
|
|
public List<UserBadgeUnitFormDTO> obtainUserBadge2List(String userId,String customerId) { |
|
|
|
List<UserBadgeUnitFormDTO> cache = |
|
|
|
redisUtils.lrange(UserRedisKeys.getResiUserBadgeKey(customerId,userId), NumConstant.ZERO, NumConstant.ONE_NEG, UserBadgeUnitFormDTO.class); |
|
|
|
redisUtils.lrange(RedisKeys.getResiUserBadgeKey(customerId, userId), NumConstant.ZERO, NumConstant.ONE_NEG, UserBadgeUnitFormDTO.class); |
|
|
|
if(!CollectionUtils.isEmpty(cache)) return cache; |
|
|
|
//补偿
|
|
|
|
cache = badgeService.getUserSortedBadge(userId,customerId); |
|
|
@ -117,7 +117,7 @@ public class UserBadgeRedis { |
|
|
|
final List<UserBadgeUnitFormDTO> sortedBadges = cache; |
|
|
|
redisTemplate.executePipelined((RedisCallback<List<UserBadgeUnitFormDTO>>) connection ->{ |
|
|
|
sortedBadges.forEach(badge -> { |
|
|
|
connection.listCommands().rPush(UserRedisKeys.getResiUserBadgeKey(customerId, userId).getBytes(), |
|
|
|
connection.listCommands().rPush(RedisKeys.getResiUserBadgeKey(customerId, userId).getBytes(), |
|
|
|
redisTemplate.getValueSerializer().serialize(badge)); |
|
|
|
}); |
|
|
|
return null; |
|
|
@ -141,7 +141,7 @@ public class UserBadgeRedis { |
|
|
|
List<String> cacheBlank = new LinkedList<>(); |
|
|
|
userIds.forEach(userId -> { |
|
|
|
List<UserBadgeUnitFormDTO> badges = |
|
|
|
redisUtils.lrange(UserRedisKeys.getResiUserBadgeKey(customerId,userId), NumConstant.ZERO, NumConstant.ONE_NEG, UserBadgeUnitFormDTO.class); |
|
|
|
redisUtils.lrange(RedisKeys.getResiUserBadgeKey(customerId, userId), NumConstant.ZERO, NumConstant.ONE_NEG, UserBadgeUnitFormDTO.class); |
|
|
|
if(CollectionUtils.isEmpty(badges)){cacheBlank.add(userId);}else{result.put(userId,badges);} |
|
|
|
}); |
|
|
|
if(!CollectionUtils.isEmpty(cacheBlank)){ |
|
|
@ -157,7 +157,7 @@ public class UserBadgeRedis { |
|
|
|
|
|
|
|
redisTemplate.executePipelined((RedisCallback<List<UserBadgeUnitFormDTO>>) connection ->{ |
|
|
|
covert.forEach(badge -> { |
|
|
|
connection.listCommands().rPush(UserRedisKeys.getResiUserBadgeKey(customerId, k).getBytes(), |
|
|
|
connection.listCommands().rPush(RedisKeys.getResiUserBadgeKey(customerId, k).getBytes(), |
|
|
|
redisTemplate.getValueSerializer().serialize(badge)); |
|
|
|
}); |
|
|
|
return null; |
|
|
@ -178,14 +178,14 @@ public class UserBadgeRedis { |
|
|
|
*/ |
|
|
|
public long pushOrRemoveUserBadge4List(String userId, String badgeId, String customerId) { |
|
|
|
if(StringUtils.isNotBlank(userId)) return illumeOrExtinguishStronglyConsistent(userId,customerId); |
|
|
|
List<UserBadgeUnitFormDTO> orient = redisUtils.lrange(UserRedisKeys.getResiUserBadgeKey(customerId,userId), NumConstant.ZERO, NumConstant.ONE_NEG, UserBadgeUnitFormDTO.class); |
|
|
|
List<UserBadgeUnitFormDTO> orient = redisUtils.lrange(RedisKeys.getResiUserBadgeKey(customerId, userId), NumConstant.ZERO, NumConstant.ONE_NEG, UserBadgeUnitFormDTO.class); |
|
|
|
UserBadgeUnitFormDTO unit = null; |
|
|
|
|
|
|
|
if (!CollectionUtils.isEmpty(orient)) { |
|
|
|
Optional<UserBadgeUnitFormDTO> opt = orient.stream().filter(badge -> StringUtils.equals(badgeId, badge.getBadgeId())).findFirst(); |
|
|
|
if (opt.isPresent()) { |
|
|
|
unit = opt.get(); |
|
|
|
return redisUtils.lrem(UserRedisKeys.getResiUserBadgeKey(customerId, userId), NumConstant.ONE, unit); |
|
|
|
return redisUtils.lrem(RedisKeys.getResiUserBadgeKey(customerId, userId), NumConstant.ONE, unit); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -206,7 +206,7 @@ public class UserBadgeRedis { |
|
|
|
if (poolOpt.isPresent()) unit = ConvertUtils.sourceToTarget(poolOpt.get(), UserBadgeUnitFormDTO.class); |
|
|
|
else return NumConstant.ZERO; |
|
|
|
} |
|
|
|
redisUtils.leftPush(UserRedisKeys.getResiUserBadgeKey(customerId, userId), unit,-1); |
|
|
|
redisUtils.leftPush(RedisKeys.getResiUserBadgeKey(customerId, userId), unit, -1); |
|
|
|
return NumConstant.ONE; |
|
|
|
} |
|
|
|
|
|
|
@ -219,11 +219,11 @@ public class UserBadgeRedis { |
|
|
|
*/ |
|
|
|
public long illumeOrExtinguishStronglyConsistent(String userId, String customerId){ |
|
|
|
List<UserBadgeUnitFormDTO> db = badgeService.getUserSortedBadge(userId,customerId); |
|
|
|
redisUtils.delete(UserRedisKeys.getResiUserBadgeKey(customerId,userId)); |
|
|
|
redisUtils.delete(RedisKeys.getResiUserBadgeKey(customerId, userId)); |
|
|
|
if(CollectionUtils.isNotEmpty(db)) { |
|
|
|
redisTemplate.executePipelined((RedisCallback<List<UserBadgeUnitFormDTO>>) connection -> { |
|
|
|
db.forEach(badge -> { |
|
|
|
connection.listCommands().rPush(UserRedisKeys.getResiUserBadgeKey(customerId, userId).getBytes(), |
|
|
|
connection.listCommands().rPush(RedisKeys.getResiUserBadgeKey(customerId, userId).getBytes(), |
|
|
|
redisTemplate.getValueSerializer().serialize(badge)); |
|
|
|
}); |
|
|
|
return null; |
|
|
@ -240,7 +240,7 @@ public class UserBadgeRedis { |
|
|
|
* @date 2020.11.09 10:02 |
|
|
|
*/ |
|
|
|
public void batchClearUserBadgeCache(String customerId){ |
|
|
|
Set<String> key = redisUtils.keys(UserRedisKeys.getResiUserBadgeKey(customerId, null)); |
|
|
|
Set<String> key = redisUtils.keys(RedisKeys.getResiUserBadgeKey(customerId, null)); |
|
|
|
if(CollectionUtils.isEmpty(key)) return; |
|
|
|
final Set<String> keys = key; |
|
|
|
redisTemplate.executePipelined((RedisCallback<String>) connection ->{ |
|
|
@ -256,7 +256,7 @@ public class UserBadgeRedis { |
|
|
|
@Override |
|
|
|
public List<String> doInRedis(RedisConnection connection) throws DataAccessException { |
|
|
|
connection.openPipeline(); |
|
|
|
Set<byte[]> keys = connection.keys(redisTemplate.getKeySerializer().serialize(UserRedisKeys.getResiUserBadgeKey(customerId, null))); |
|
|
|
Set<byte[]> keys = connection.keys(redisTemplate.getKeySerializer().serialize(RedisKeys.getResiUserBadgeKey(customerId, null))); |
|
|
|
return null; |
|
|
|
} |
|
|
|
},redisTemplate.getKeySerializer()); |
|
|
|