Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

踢掉之前已经登录的token的逻辑有点可怕...性能堪优呐 😓请优化 #802

Closed
LonelyTear opened this issue Jul 4, 2023 · 1 comment
Labels
enhancement New feature or request Fixed

Comments

@LonelyTear
Copy link

LonelyTear commented Jul 4, 2023

onlineUserService.checkLoginOnUser(authUser.getUsername(), token);

checkLoginOnUser方法内部用到的

getAll(xxxxxxx)

getAll方法内部用到

        List<String> keys = redisUtils.scan(properties.getOnlineKey() + "*");  
        for (String key : keys) {
            OnlineUserDto onlineUserDto = (OnlineUserDto) redisUtils.get(key);
            .....
         }

踢掉之前已经登录的token的逻辑有点可怕...性能堪优呐 😓请优化

    /**
     * 检测用户是否在之前已经登录,已经登录踢下线
     * @param userName 用户名
     */
    public void checkLoginOnUser(String userName, String igoreToken){
        List<OnlineUserDto> onlineUserDtos = getAll(userName);
        if(onlineUserDtos ==null || onlineUserDtos.isEmpty()){
            return;
        }
        for(OnlineUserDto onlineUserDto : onlineUserDtos){
            if(onlineUserDto.getUserName().equals(userName)){
                try {
                    String token =EncryptUtils.desDecrypt(onlineUserDto.getKey());
                    if(StringUtils.isNotBlank(igoreToken)&&!igoreToken.equals(token)){
                        this.kickOut(token);
                    }else if(StringUtils.isBlank(igoreToken)){
                        this.kickOut(token);
                    }
                } catch (Exception e) {
                    log.error("checkUser is error",e);
                }
            }
        }
    }

俺是没有勇气使用号 edisUtils.scan(properties.getOnlineKey() + ""); 呀 😰
更没有勇气循环向redis 索取 redisUtils.get(key) 呀 😰
惊呆啦 , 好yellow好暴力💥

    /**
     * 查询全部数据,不分页
     * @param filter /
     * @return /
     */
    public List<OnlineUserDto> getAll(String filter){
        List<String> keys = redisUtils.scan(properties.getOnlineKey() + "*");
        Collections.reverse(keys);
        List<OnlineUserDto> onlineUserDtos = new ArrayList<>();
        for (String key : keys) {
            OnlineUserDto onlineUserDto = (OnlineUserDto) redisUtils.get(key);
            if(StringUtils.isNotBlank(filter)){
                if(onlineUserDto.toString().contains(filter)){
                    onlineUserDtos.add(onlineUserDto);
                }
            } else {
                onlineUserDtos.add(onlineUserDto);
            }
        }
        onlineUserDtos.sort((o1, o2) -> o2.getLoginTime().compareTo(o1.getLoginTime()));
        return onlineUserDtos;
    }

如何优化建议从redis的key名着手

@LonelyTear LonelyTear changed the title 踢掉之前已经登录的token的逻辑有点搞笑了... , 性能堪优😓......非常建议优化...💬 踢掉之前已经登录的token的逻辑有点可怕...性能堪优呐 😓请优化 Jul 4, 2023
@elunez
Copy link
Owner

elunez commented Jul 4, 2023

已更新优化

@elunez elunez closed this as completed in cf3655a Jul 4, 2023
@elunez elunez added dependencies Pull requests that update a dependency file Fixed enhancement New feature or request and removed dependencies Pull requests that update a dependency file labels Jul 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Fixed
Projects
None yet
Development

No branches or pull requests

2 participants