hyperf-chat/app/Cache/SocketFdBindUser.php

55 lines
1.2 KiB
PHP
Raw Normal View History

2021-05-22 14:47:46 +08:00
<?php
namespace App\Cache;
use App\Cache\Repository\HashGroupRedis;
2021-07-29 23:30:42 +08:00
use App\Traits\StaticInstance;
2021-05-22 14:47:46 +08:00
/**
* :客户端ID与用户ID绑定(多对一关系)
*
* @package App\Cache
*/
class SocketFdBindUser extends HashGroupRedis
{
2021-07-29 23:30:42 +08:00
use StaticInstance;
2021-05-22 14:47:46 +08:00
protected $name = 'ws:fd-user';
/**
* 添加绑定
*
* @param int $fd 客户端ID
* @param int $user_id 用户ID
* @param string $run_id 服务运行ID默认当前服务ID
* @return bool|int
*/
public function bind(int $fd, int $user_id, $run_id = SERVER_RUN_ID)
{
return $this->add($run_id, strval($fd), $user_id);
}
/**
* 解除绑定
*
* @param string $run_id 服务运行ID默认当前服务ID
* @return bool|int
*/
public function unBind(int $fd, $run_id = SERVER_RUN_ID)
{
return $this->rem($run_id, strval($fd));
}
/**
* 查询客户端 FD 对应的用户ID
*
* @param int $fd 客户端ID
* @param string $run_id 服务运行ID默认当前服务ID
* @return int
*/
2021-05-22 20:54:30 +08:00
public function findUserId(int $fd, $run_id = SERVER_RUN_ID): int
2021-05-22 14:47:46 +08:00
{
return (int)$this->get($run_id, strval($fd)) ?: 0;
}
}