| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- namespace App\Module\User\Services;
- use Illuminate\Support\Facades\Redis;
- /**
- * 在线
- * 在线统计
- */
- class Online
- {
- static public function on($user_id)
- {
- /**
- * @var \Redis $a
- */
- $a = Redis::client();
- $a->zAdd('user_online', time(), $user_id);
- }
- /**
- * 统计
- * @param int $min 几分钟之内
- * @return false|int|\Redis
- * @throws \RedisException
- */
- static public function count($min = 10)
- {
- $start = time() - ($min * 60);
- /**
- * @var \Redis $a
- */
- $a = Redis::client();
- return $a->zCount('user_online', $start, time());
- }
- /**
- * 垃圾回收
- *
- * @param int $min 几分钟之前
- * @return void
- * @throws \RedisException
- */
- static public function gc($min = 30)
- {
- $start = time() - ($min * 60);
- /**
- * @var \Redis $a
- */
- $a = Redis::client();
- $a->zRemRangeByScore('user_online', 0, $start);
- }
- }
|