Online.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\Module\User\Services;
  3. use Illuminate\Support\Facades\Redis;
  4. /**
  5. * 在线
  6. * 在线统计
  7. */
  8. class Online
  9. {
  10. static public function on($user_id)
  11. {
  12. /**
  13. * @var \Redis $a
  14. */
  15. $a = Redis::client();
  16. $a->zAdd('user_online', time(), $user_id);
  17. }
  18. /**
  19. * 统计
  20. * @param int $min 几分钟之内
  21. * @return false|int|\Redis
  22. * @throws \RedisException
  23. */
  24. static public function count($min = 10)
  25. {
  26. $start = time() - ($min * 60);
  27. /**
  28. * @var \Redis $a
  29. */
  30. $a = Redis::client();
  31. return $a->zCount('user_online', $start, time());
  32. }
  33. /**
  34. * 垃圾回收
  35. *
  36. * @param int $min 几分钟之前
  37. * @return void
  38. * @throws \RedisException
  39. */
  40. static public function gc($min = 30)
  41. {
  42. $start = time() - ($min * 60);
  43. /**
  44. * @var \Redis $a
  45. */
  46. $a = Redis::client();
  47. $a->zRemRangeByScore('user_online', 0, $start);
  48. }
  49. }