first commit
This commit is contained in:
55
backend/includes/Redis.php
Normal file
55
backend/includes/Redis.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
/**
|
||||
* Redis Singleton — phpredis Extension
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/../config.php';
|
||||
|
||||
class RedisClient
|
||||
{
|
||||
private static ?\Redis $instance = null;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
try {
|
||||
self::$instance = new \Redis();
|
||||
self::$instance->connect(REDIS_HOST, REDIS_PORT, 2.0);
|
||||
|
||||
if (REDIS_PASSWORD !== null) {
|
||||
self::$instance->auth(REDIS_PASSWORD);
|
||||
}
|
||||
|
||||
if (REDIS_DB > 0) {
|
||||
self::$instance->select(REDIS_DB);
|
||||
}
|
||||
|
||||
self::$instance->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_JSON);
|
||||
} catch (\RedisException $e) {
|
||||
$errorMsg = $e->getMessage();
|
||||
error_log('Redis connection failed: ' . $errorMsg);
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'message' => 'cache_error', 'error' => $errorMsg]);
|
||||
exit;
|
||||
} catch (\Exception $e) {
|
||||
$errorMsg = $e->getMessage();
|
||||
error_log('Unexpected error in Redis: ' . $errorMsg);
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'message' => 'cache_error', 'error' => $errorMsg]);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
public static function getInstance(): \Redis
|
||||
{
|
||||
if (self::$instance === null) {
|
||||
new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
private function __clone() {}
|
||||
public function __wakeup()
|
||||
{
|
||||
throw new \Exception("Cannot unserialize singleton");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user