hyperf-chat/app/Cache/SocketFdBindUser.php

52 lines
1.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Cache;
use App\Cache\Repository\HashGroupRedis;
/**
* 注:客户端ID与用户ID绑定(多对一关系)
*
* @package App\Cache
*/
class SocketFdBindUser extends HashGroupRedis
{
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
*/
public function findUserId(int $fd, $run_id = SERVER_RUN_ID)
{
return (int)$this->get($run_id, strval($fd)) ?: 0;
}
}