mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-23 07:33:53 +08:00
优化获取用户的名称的方法
--RedisKeys.java 设一个新key --SysUserUtilServiceImpl.java 优化赋值用户名的方法,把用户名暂存在redis。获取相同的不需要频繁去数据库查找
This commit is contained in:
@@ -26,4 +26,13 @@ public class RedisKeys {
|
||||
public static String getDeviceCaptchaKey(String captcha) {
|
||||
return "sys:device:captcha:" + captcha;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户id的Key
|
||||
*/
|
||||
public static String getUserIdKey(Long userid) {
|
||||
return "sys:username:id:" + userid;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+12
-3
@@ -2,6 +2,7 @@ package xiaozhi.modules.sys.service.impl;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import xiaozhi.common.redis.RedisKeys;
|
||||
import xiaozhi.common.redis.RedisUtils;
|
||||
import xiaozhi.common.service.impl.BaseServiceImpl;
|
||||
import xiaozhi.modules.sys.dao.SysUserDao;
|
||||
@@ -18,9 +19,17 @@ public class SysUserUtilServiceImpl extends BaseServiceImpl<SysUserDao, SysUserE
|
||||
|
||||
@Override
|
||||
public void assignUsername(Long userId, Consumer<String> setter) {
|
||||
SysUserEntity entity = baseDao.selectById(userId);
|
||||
if (entity != null) {
|
||||
setter.accept(entity.getUsername());
|
||||
String userIdKey = RedisKeys.getUserIdKey(userId);
|
||||
String username = redisUtils.get(userIdKey).toString();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user