How to disable multi terminal login based on Redis unordered collection

  • 2020-06-23 02:13:09
  • OfStack

preface

1 collection type can store up to 2^32 -1 strings

The collection type is implemented within redis using a hash table (hash table) with an empty value, so the time complexity of adding or removing elements from the collection is O(1).

A set has element-only sex.

This article mainly introduces the implementation of banning multi-end login based on the unordered collection of Redis. Let's start with the detailed introduction

Application background

Multiple applications with assumed names A and B prohibit simultaneous login from A B,A to kick B, and B to kick A

Implementation approach

Set two unordered sets a_set, b_set a b when logged in

$redis->sAdd('a_set',$user_id);//A The login 
$redis->sRem('b_set',$user_id);// play B

$redis->sAdd('b_set',$user_id);//B The login 
$redis->sRem('a_set',$user_id);// play A

api determine if id is online before getting data (AB and api are separate)


if($redis->sIsmember('a_set',$user_id)){
 //true 
}else{
 //false
}

B judgment


if($redis->sIsmember('b_set',$user_id)){
 //true
}else{
 //false
}

Methods used

sadd key_set value sets the value to set sismember key_set value value exists in key_set srem key_set value removes the specified value smembers key_set gets all value

conclusion


Related articles: