mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
优化获取用户的名称的方法
--RedisKeys.java 设一个新key --SysUserUtilServiceImpl.java 优化赋值用户名的方法,把用户名暂存在redis。获取相同的不需要频繁去数据库查找
This commit is contained in:
parent
083e7071f0
commit
06e4834e27
2 changed files with 21 additions and 3 deletions
|
|
@ -26,4 +26,13 @@ public class RedisKeys {
|
||||||
public static String getDeviceCaptchaKey(String captcha) {
|
public static String getDeviceCaptchaKey(String captcha) {
|
||||||
return "sys:device:captcha:" + captcha;
|
return "sys:device:captcha:" + captcha;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户id的Key
|
||||||
|
*/
|
||||||
|
public static String getUserIdKey(Long userid) {
|
||||||
|
return "sys:username:id:" + userid;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package xiaozhi.modules.sys.service.impl;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import xiaozhi.common.redis.RedisKeys;
|
||||||
import xiaozhi.common.redis.RedisUtils;
|
import xiaozhi.common.redis.RedisUtils;
|
||||||
import xiaozhi.common.service.impl.BaseServiceImpl;
|
import xiaozhi.common.service.impl.BaseServiceImpl;
|
||||||
import xiaozhi.modules.sys.dao.SysUserDao;
|
import xiaozhi.modules.sys.dao.SysUserDao;
|
||||||
|
|
@ -18,9 +19,17 @@ public class SysUserUtilServiceImpl extends BaseServiceImpl<SysUserDao, SysUserE
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void assignUsername(Long userId, Consumer<String> setter) {
|
public void assignUsername(Long userId, Consumer<String> setter) {
|
||||||
SysUserEntity entity = baseDao.selectById(userId);
|
String userIdKey = RedisKeys.getUserIdKey(userId);
|
||||||
if (entity != null) {
|
String username = redisUtils.get(userIdKey).toString();
|
||||||
setter.accept(entity.getUsername());
|
if(username != null){
|
||||||
|
setter.accept(username);
|
||||||
|
}else {
|
||||||
|
SysUserEntity entity = baseDao.selectById(userId);
|
||||||
|
if (entity != null) {
|
||||||
|
username = entity.getUsername();
|
||||||
|
redisUtils.set(userIdKey,username,10);
|
||||||
|
setter.accept(username);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue