|
|
@ -16,10 +16,14 @@ import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.collections4.CollectionUtils; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.dao.DataAccessException; |
|
|
|
import org.springframework.data.redis.connection.RedisConnection; |
|
|
|
import org.springframework.data.redis.connection.StringRedisConnection; |
|
|
|
import org.springframework.data.redis.core.RedisCallback; |
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
import java.util.Collections; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Optional; |
|
|
|
import java.util.Set; |
|
|
@ -106,8 +110,23 @@ public class UserBadgeRedis { |
|
|
|
* @date 2020.11.05 13:26 |
|
|
|
*/ |
|
|
|
public List<UserBadgeUnitFormDTO> obtainUserBadge2List(String userId,String customerId) { |
|
|
|
//TODO 补偿
|
|
|
|
return redisUtils.lrange(UserRedisKeys.getResiUserBadgeKey(customerId,userId), NumConstant.ZERO, NumConstant.ONE_NEG, UserBadgeUnitFormDTO.class); |
|
|
|
List<UserBadgeUnitFormDTO> cache = |
|
|
|
redisUtils.lrange(UserRedisKeys.getResiUserBadgeKey(customerId,userId), NumConstant.ZERO, NumConstant.ONE_NEG, UserBadgeUnitFormDTO.class); |
|
|
|
if(!CollectionUtils.isEmpty(cache)) return cache; |
|
|
|
//补偿
|
|
|
|
cache = badgeService.getUserSortedBadge(userId,customerId); |
|
|
|
if(CollectionUtils.isEmpty(cache)) return cache; |
|
|
|
final List<UserBadgeUnitFormDTO> sortedBadges = cache; |
|
|
|
redisTemplate.executePipelined((RedisCallback<List<UserBadgeUnitFormDTO>>) connection ->{ |
|
|
|
sortedBadges.forEach(badge -> { |
|
|
|
|
|
|
|
connection.listCommands().rPush(UserRedisKeys.getResiUserBadgeKey(customerId, userId).getBytes(), |
|
|
|
redisTemplate.getValueSerializer().serialize(badge)); |
|
|
|
}); |
|
|
|
return null; |
|
|
|
}); |
|
|
|
|
|
|
|
return cache; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -160,12 +179,11 @@ public class UserBadgeRedis { |
|
|
|
* @date 2020.11.09 10:02 |
|
|
|
*/ |
|
|
|
public void batchClearUserBadgeCache(String customerId){ |
|
|
|
Set<String> key = redisUtils.keys(UserRedisKeys.getResiUserBadgeKey(customerId, null)); |
|
|
|
if(CollectionUtils.isEmpty(key)) return; |
|
|
|
final Set<String> keys = key; |
|
|
|
redisTemplate.executePipelined((RedisCallback<String>) connection ->{ |
|
|
|
Set<byte[]> keys = |
|
|
|
connection.keys(redisTemplate.getKeySerializer().serialize(UserRedisKeys.getResiUserBadgeKey(customerId, null))); |
|
|
|
if(!CollectionUtils.isEmpty(keys)){ |
|
|
|
keys.forEach( ser -> {connection.del(ser);}); |
|
|
|
} |
|
|
|
keys.forEach( ser -> {connection.del(redisTemplate.getKeySerializer().serialize(ser));}); |
|
|
|
return null; |
|
|
|
}); |
|
|
|
|
|
|
|