Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

preface

This section introduces the code of the SysUserOnlineController part of the upload and download module of Ruoyi-Admin module of Ruoyi-Vue. This interface is mainly used to show the situation of the online user monitoring module

List page

This interface is directly from the Redis to find all the login user information key out, directly traverse all the data back to the front end.

Get all login keys

The Redis Keys command is used

Collection<String> keys = redisCache.keys(Constants.LOGIN_TOKEN_KEY + "*");
Copy the code

Get all login_tokens:* using the Keys command

Iterate through all the keys to get all the user caches

LoginUser user = redisCache.getCacheObject(key);
Copy the code

This interface returns all LoginUser information to the foreground at once. For more than 50 users, it is 11KB. If there are more users, it will be slower. I don’t think there is a good way to avoid the current Redis format of saving login users. If the login user has a large amount of data, it can be considered to save a copy of the login user’s information in ES in addition to Redis, so that ES can be used for paging

Don’t forget to finally reverse the order of the resulting userList.

        Collections.reverse(userOnlineList);
        userOnlineList.removeAll(Collections.singleton(null));
Copy the code

Strong back user

redisCache.deleteObject(Constants.LOGIN_TOKEN_KEY + tokenId);

Copy the code

Pass in the user’s tokenId and delete the corresponding key from the Redis

conclusion

For small projects, these two simple functions can satisfy the demand of user management, user mode but there is no access to the site or need to consider whether the existing in the actual business, if do not need to log in you can visit the web site, then there is no way to access control, then consider other means to access control.