Browse Source

推荐小组-获取组长信息接口优化 直接从缓存取用户数据

master
jianjun 4 years ago
parent
commit
cbc4ab149b
  1. 24
      epmet-user/epmet-user-server/src/main/java/com/epmet/redis/UserBadgeRedis.java
  2. 11
      epmet-user/epmet-user-server/src/main/java/com/epmet/redis/UserBaseInfoRedis.java

24
epmet-user/epmet-user-server/src/main/java/com/epmet/redis/UserBadgeRedis.java

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

11
epmet-user/epmet-user-server/src/main/java/com/epmet/redis/UserBaseInfoRedis.java

@ -19,6 +19,7 @@ package com.epmet.redis;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import com.epmet.commons.tools.constant.NumConstant; 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.redis.RedisUtils;
import com.epmet.commons.tools.utils.ConvertUtils; import com.epmet.commons.tools.utils.ConvertUtils;
import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.utils.Result;
@ -79,7 +80,7 @@ public class UserBaseInfoRedis {
**/ **/
public void set(UserBaseInfoResultDTO baseInfo){ public void set(UserBaseInfoResultDTO baseInfo){
if(StringUtils.isBlank(baseInfo.getUserId())) return; if(StringUtils.isBlank(baseInfo.getUserId())) return;
String key = UserRedisKeys.getResiUserKey(baseInfo.getUserId()); String key = RedisKeys.getResiUserKey(baseInfo.getUserId());
//bean to map //bean to map
Map<String, Object> map = BeanUtil.beanToMap(baseInfo, false, true); Map<String, Object> map = BeanUtil.beanToMap(baseInfo, false, true);
//用户缓存的有效期为七天 //用户缓存的有效期为七天
@ -89,7 +90,7 @@ public class UserBaseInfoRedis {
public UserBaseInfoResultDTO get(String userId){ public UserBaseInfoResultDTO get(String userId){
if(StringUtils.isNotBlank(userId)) { if(StringUtils.isNotBlank(userId)) {
Map<String, Object> map = redisUtils.hGetAll(UserRedisKeys.getResiUserKey(userId)); Map<String, Object> map = redisUtils.hGetAll(RedisKeys.getResiUserKey(userId));
if (null != map && !map.isEmpty()) { if (null != map && !map.isEmpty()) {
//缓存中有数据,直接返回 //缓存中有数据,直接返回
return BeanUtil.mapToBean(map, UserBaseInfoResultDTO.class, true); return BeanUtil.mapToBean(map, UserBaseInfoResultDTO.class, true);
@ -109,7 +110,7 @@ public class UserBaseInfoRedis {
public UserBaseInfoResultDTO getUserInfo(String userId){ public UserBaseInfoResultDTO getUserInfo(String userId){
if(StringUtils.isNotBlank(userId)){ if(StringUtils.isNotBlank(userId)){
//获取居民缓存key //获取居民缓存key
Map<String,Object> map = redisUtils.hGetAll(UserRedisKeys.getResiUserKey(userId)); Map<String, Object> map = redisUtils.hGetAll(RedisKeys.getResiUserKey(userId));
if(null != map && !map.isEmpty()) { if(null != map && !map.isEmpty()) {
//缓存中有数据,直接返回 //缓存中有数据,直接返回
return BeanUtil.mapToBean(map, UserBaseInfoResultDTO.class, true); return BeanUtil.mapToBean(map, UserBaseInfoResultDTO.class, true);
@ -200,7 +201,7 @@ public class UserBaseInfoRedis {
public UserBaseInfoResultDTO getUserInfo(String userId,String customerId){ public UserBaseInfoResultDTO getUserInfo(String userId,String customerId){
if(StringUtils.isNotBlank(userId)){ if(StringUtils.isNotBlank(userId)){
//获取居民缓存key //获取居民缓存key
Map<String,Object> map = redisUtils.hGetAll(UserRedisKeys.getResiUserKey(userId)); Map<String, Object> map = redisUtils.hGetAll(RedisKeys.getResiUserKey(userId));
if(null != map && !map.isEmpty()) { if(null != map && !map.isEmpty()) {
//缓存中有数据,直接返回 //缓存中有数据,直接返回
return BeanUtil.mapToBean(map, UserBaseInfoResultDTO.class, true); return BeanUtil.mapToBean(map, UserBaseInfoResultDTO.class, true);
@ -363,7 +364,7 @@ public class UserBaseInfoRedis {
public void clearUserCache(List<String> userIds){ public void clearUserCache(List<String> userIds){
userIds.forEach(id ->{ userIds.forEach(id ->{
if(StringUtils.isNotBlank(id)) { if(StringUtils.isNotBlank(id)) {
redisUtils.delete(UserRedisKeys.getResiUserKey(id)); redisUtils.delete(RedisKeys.getResiUserKey(id));
} }
}); });
} }

Loading…
Cancel
Save