34 lines
888 B
PHP
34 lines
888 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Core;
|
|
|
|
use Predis\Client;
|
|
use Exception;
|
|
|
|
final class Redis
|
|
{
|
|
private static ?Client $instance = null;
|
|
|
|
public static function getInstance(): Client
|
|
{
|
|
if (self::$instance === null) {
|
|
try {
|
|
self::$instance = new Client([
|
|
'scheme' => 'tcp',
|
|
'host' => $_ENV['REDIS_HOST'] ?? '127.0.0.1',
|
|
'port' => $_ENV['REDIS_PORT'] ?? 6379,
|
|
'password' => $_ENV['REDIS_PASSWORD'] ?: null,
|
|
]);
|
|
} catch (Exception $e) {
|
|
// If Redis fails, we might want to log it or handle gracefully
|
|
// depending on how critical it is.
|
|
throw new Exception("Redis Connection Error: " . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
return self::$instance;
|
|
}
|
|
}
|