Home >Database >Redis >Method to prohibit multi-terminal login based on Redis unordered collection

Method to prohibit multi-terminal login based on Redis unordered collection

尚
forward
2020-05-28 09:21:232386browse

Method to prohibit multi-terminal login based on Redis unordered collection

Application Background

Assuming multiple application names are named A and B, users are prohibited from logging in from A and B at the same time. A login kicks B, and B login kicks A

Implementation idea

Set two unordered sets a_set, b_set

a b When logging in, execute

$redis->sAdd('a_set',$user_id);//A登录
$redis->sRem('b_set',$user_id);//踢B
$redis->sAdd('b_set',$user_id);//B登录
$redis->sRem('a_set',$user_id);//踢A

Before api obtains data, determine whether the id of the end is online (AB two The APIs of each end are separate)

A judgment:

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

B judgment

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

Method used:

sadd key_set value 设置值到set中
sismember key_set value 判断值时候存在key_set里面
srem key_set value 移除指定值
smembers key_set 获取所有的value

For more redis knowledge, please pay attention to the redis introductory tutorial column.

The above is the detailed content of Method to prohibit multi-terminal login based on Redis unordered collection. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete