RedisService.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Service;
  3. use App\Cache\SocketFdBindUser;
  4. use App\Cache\SocketUserBindFds;
  5. /**
  6. * websocket关系存储
  7. *
  8. * @package App\Service
  9. */
  10. class RedisService extends RedisInterface
  11. {
  12. protected $prefix_fn = 'chat_fn';
  13. protected $prefix_user = 'chat_user';
  14. /**
  15. * 绑定fd和用户关系
  16. * @param string $fid
  17. * @param int $userId
  18. * @param $run_id
  19. * @return void
  20. * @throws \RedisException
  21. */
  22. public function bind(string $fid,int $userId, $run_id = SERVER_RUN_ID)
  23. {
  24. //站点通道+用户
  25. $this->redis->hSet($run_id,$this->prefix_fn.$fid,$userId);
  26. //站点用户+通道
  27. $this->redis->hSet($run_id,$this->prefix_user.$userId,$fid);
  28. }
  29. /**
  30. * 解绑通道和用户关系
  31. * @param string $fid
  32. * @param int $userId
  33. * @param $run_id
  34. * @return void
  35. * @throws \RedisException
  36. */
  37. public function unbind(string $fid,int $userId, $run_id = SERVER_RUN_ID)
  38. {
  39. $this->redis->hDel($run_id,$this->prefix_fn.$fid);
  40. $this->redis->hDel($run_id,$this->prefix_user.$userId);
  41. }
  42. /**
  43. * 通过FD获取userID
  44. * @param string $fid
  45. * @param $run_id
  46. * @return false|\Redis|string
  47. * @throws \RedisException
  48. */
  49. public function findUser(string $fid, $run_id = SERVER_RUN_ID)
  50. {
  51. return $this->redis->hGet($run_id,$this->prefix_fn.$fid);
  52. }
  53. /**
  54. * 通过UserID 获取fd
  55. * @param int $userId
  56. * @param $run_id
  57. * @return false|\Redis|string
  58. * @throws \RedisException
  59. */
  60. public function findFd(int $userId, $run_id = SERVER_RUN_ID)
  61. {
  62. return $this->redis->hGet($run_id,$this->prefix_user.$userId);
  63. }
  64. }