radfusion/app/Services/UserService.php
2025-01-05 12:49:53 +06:00

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);
}
}