The method of prohibiting Multi-end Login based on Redis unordered set
This article is about how to implement the function of prohibiting multi-side login based on Redis unordered collection. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Preface
A collection type can store up to 2 ^ 32-1 strings
Collection types are implemented inside redis using a hash table (hash table) with empty values, so the time complexity of adding or deleting elements in the collection is O (1).
The collection has element uniqueness.
Application background
The names of multiple applications are assumed to be An and B, which prohibit users from logging in from A / B at the same time.
Realization idea
Set two unordered sets a_set, b_set
A / b executes when logging in
$redis- > sAdd ('ajar setter sRem); / A log in to $redis- > sRem (' bouncy setter dagger camera UserID); / / kick Blockrediss-> sAdd ('bouncy setter requlibility UserID); / B Log in to $redis- > sRem (' aholysetter reflexive UserID); / / kick A
Before api acquires data, determine whether the id of this end is online (the api of the two sides of the AB is separate)
If ($redis- > sIsmember) {/ / true} else {/ / false}
B judgment
If ($redis- > sIsmember ('baked setbacks grammatical user ID)) {/ / true} else {/ / false}
The method used
Sadd key_set value setting value to set
The sismember key_set value judgment value is stored in the key_set.
Srem key_set value removes the specified value
Smembers key_set gets all the value
Thank you for reading! This is the end of this article on "the method of prohibiting multiple logins based on Redis unordered sets". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!