27 lines
658 B
PHP
27 lines
658 B
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Redis;
|
|
|
|
final readonly class UserService
|
|
{
|
|
public static function setLastSeen(int $userId, ?Carbon $seenAt = null): void
|
|
{
|
|
Redis::connection()->set(self::lastSeenKey($userId), ($seenAt ?? Carbon::now())->toISOString());
|
|
}
|
|
|
|
public static function getLastSeen(int $userId): ?Carbon
|
|
{
|
|
$lastSeen = Redis::connection()->get(self::lastSeenKey($userId));
|
|
|
|
return $lastSeen ? Carbon::parse($lastSeen) : null;
|
|
}
|
|
|
|
private static function lastSeenKey(int $userId): string
|
|
{
|
|
return sprintf('last_seen:%d', $userId);
|
|
}
|
|
}
|