Browse Source

用户缓存

master
wangchao 5 years ago
parent
commit
179d1f4530
  1. 36
      epmet-user/epmet-user-server/src/main/java/com/epmet/redis/UserBaseInfoRedis.java
  2. 36
      epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/UserServiceImpl.java

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

@ -87,8 +87,17 @@ public class UserBaseInfoRedis {
redisUtils.hMSet(key, map,RedisUtils.DEFAULT_EXPIRE * NumConstant.SEVEN);
}
public String get(String id){
public UserBaseInfoResultDTO get(String userId){
if(StringUtils.isNotBlank(userId)) {
Map<String, Object> map = redisUtils.hGetAll(UserRedisKeys.getResiUserKey(userId));
if (null != map && !map.isEmpty()) {
//缓存中有数据,直接返回
return BeanUtil.mapToBean(map, UserBaseInfoResultDTO.class, true);
}
}
return null;
}
/**
@ -193,6 +202,14 @@ public class UserBaseInfoRedis {
UserBaseInfoResultDTO currentCache = ConvertUtils.sourceToTarget(entity,UserBaseInfoResultDTO.class);
UserBaseInfoResultDTO orientCache = get(entity.getUserId());
if(null != orientCache && StringUtils.isNotBlank(orientCache.getUserId())){
//将要更改的信息赋值到原缓存中
mergeObject(currentCache,orientCache);
//将原来的缓存对象赋值给要更新的缓存对象
//此时orientCache中包概括要更新的属性以及之前有切不更新的属性
currentCache = orientCache;
}
RegisterRelationEntity relation = registerRelationDao.selectRegisteredGridIdByUserId(entity.getUserId());
//如果没有首次注册网格,则没有网格名称(xx机关-xx网格)、显示昵称(xx网格-x先生/女士)
if(null != relation && StringUtils.isNotBlank(relation.getGridId())){
@ -203,10 +220,11 @@ public class UserBaseInfoRedis {
Result<BelongGridNameResultDTO> gridResult =
govOrgOpenFeignClient.getGridNameByGridId(gridParam);
if(gridResult.success() && null != gridResult.getData()
&& StringUtils.isNotBlank(gridResult.getData().getBelongsGridName())){
&& StringUtils.isNotBlank(gridResult.getData().getBelongsGridName())) {
String gridFullName = gridResult.getData().getBelongsGridName();
currentCache.setRegisteredGridName(gridFullName);
if(StringUtils.isBlank(currentCache.getGender()))currentCache.setGender(NumConstant.ZERO_STR);
if (StringUtils.isBlank(currentCache.getGender())) currentCache.setGender(NumConstant.ZERO_STR);
if (StringUtils.isNotBlank(currentCache.getNickname())) {
StringBuffer buffer = new StringBuffer(gridFullName.split(ModuleConstant.DASH)[NumConstant.ONE]).append(ModuleConstant.DASH).append(currentCache.getSurname());
switch (currentCache.getGender()) {
case NumConstant.ONE_STR:
@ -220,6 +238,7 @@ public class UserBaseInfoRedis {
}
currentCache.setShowName(buffer.toString());
}
}
}else{
//如果没有居民注册记录,说明当前用户是陌生人,需要从陌生人网格访问列表中查询所属的客户Id
LatestGridInfoResultDTO gridLatest =
@ -240,18 +259,7 @@ public class UserBaseInfoRedis {
// }
//}
Map<String,Object> map = redisUtils.hGetAll(UserRedisKeys.getResiUserKey(entity.getUserId()));
if(null != map && !map.isEmpty()) {
//缓存中有数据,直接返回
UserBaseInfoResultDTO orientCache = BeanUtil.mapToBean(map, UserBaseInfoResultDTO.class, true);
mergeObject(currentCache , orientCache);
set(orientCache);
}else{
//缓存中没有该数据
set(currentCache);
}
}

36
epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/UserServiceImpl.java

@ -8,11 +8,10 @@ import com.epmet.commons.tools.utils.ConvertUtils;
import com.epmet.commons.tools.utils.CpUserDetailRedis;
import com.epmet.commons.tools.utils.Result;
import com.epmet.constant.UserConstant;
import com.epmet.dao.UserDao;
import com.epmet.dao.UserRoleDao;
import com.epmet.dao.UserWechatDao;
import com.epmet.dao.*;
import com.epmet.dto.CustomerGridDTO;
import com.epmet.dto.UserDTO;
import com.epmet.dto.UserResiInfoDTO;
import com.epmet.dto.UserWechatDTO;
import com.epmet.dto.form.*;
import com.epmet.dto.result.*;
@ -21,6 +20,7 @@ import com.epmet.entity.UserEntity;
import com.epmet.entity.UserWechatEntity;
import com.epmet.feign.EpmetPointOpenFeignClient;
import com.epmet.feign.GovOrgFeignClient;
import com.epmet.redis.UserBaseInfoRedis;
import com.epmet.service.UserBaseInfoService;
import com.epmet.service.UserService;
import com.epmet.util.ModuleConstant;
@ -53,6 +53,12 @@ public class UserServiceImpl extends BaseServiceImpl<UserDao, UserEntity> implem
private UserBaseInfoService userBaseInfoService;
@Autowired
private EpmetPointOpenFeignClient epmetPointOpenFeignClient;
@Autowired
private UserBaseInfoRedis userBaseInfoRedis;
@Autowired
private UserResiInfoDao userResiInfoDao;
@Autowired
private UserBaseInfoDao userBaseInfoDao;
private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
@ -130,7 +136,31 @@ public class UserServiceImpl extends BaseServiceImpl<UserDao, UserEntity> implem
userWechatDTO.setUpdatedTime(new Date());
userWechatDTO.setUpdatedBy(wxUserInfoFormDTO.getUserId());
userWechatDao.updateByUserId(userWechatDTO);
UserBaseInfoEntity baseInfo = new UserBaseInfoEntity();
UserBaseInfoResultDTO cache = userBaseInfoRedis.get(wxUserInfoFormDTO.getUserId());
if(null == cache || StringUtils.isBlank(cache.getId())){
//没有缓存
cache =
userBaseInfoDao.selectListByUserIdList(wxUserInfoFormDTO.getUserId());
if(null == cache || StringUtils.isBlank(cache.getId()) || StringUtils.isBlank(cache.getMobile()) ||StringUtils.isBlank(cache.getSurname())){
//没有baseInfo
UserResiInfoDTO resiInfo =
userResiInfoDao.selectByUserId(wxUserInfoFormDTO.getUserId());
if(null == resiInfo || StringUtils.isBlank(resiInfo.getId())){
//没有resiInfo
return new Result();
}else{
baseInfo.setSurname(resiInfo.getSurname());
baseInfo.setName(resiInfo.getName());
baseInfo.setStreet(resiInfo.getStreet());
baseInfo.setDistrict(resiInfo.getDistrict());
baseInfo.setBuildingAddress(resiInfo.getBuildingAddress());
baseInfo.setMobile(resiInfo.getRegMobile());
}
}
}
baseInfo.setUserId(wxUserInfoFormDTO.getUserId());
baseInfo.setNickname(wxUserInfoFormDTO.getNickName());
baseInfo.setHeadImgUrl(wxUserInfoFormDTO.getAvatarUrl());

Loading…
Cancel
Save