| Current Path : /home/seto/testhive.pm/includes/ |
| Current File : /home/seto/testhive.pm/includes/redis.php |
<?php
/**
* Redis wrapper with graceful fallback.
* If Redis is unavailable, all operations are no-ops and cache misses always occur.
*/
function redis(): ?Redis {
static $redis = null;
static $failed = false;
if ($failed) return null;
if ($redis !== null) return $redis;
if (!class_exists('Redis')) { $failed = true; return null; }
try {
$r = new Redis();
$r->connect('127.0.0.1', 6379, 1.0);
$redis = $r;
return $redis;
} catch (Exception $e) {
$failed = true;
return null;
}
}
function cache_get(string $key): mixed {
$r = redis();
if (!$r) return false;
try {
$val = $r->get($key);
return $val === false ? false : unserialize($val);
} catch (Exception $e) {
return false;
}
}
function cache_set(string $key, mixed $value, int $ttl = 60): void {
$r = redis();
if (!$r) return;
try {
$r->setex($key, $ttl, serialize($value));
} catch (Exception $e) {}
}
function cache_del(string $key): void {
$r = redis();
if (!$r) return;
try { $r->del($key); } catch (Exception $e) {}
}
function cache_sadd(string $key, string $member, int $ttl = 86400): void {
$r = redis();
if (!$r) return;
try {
$r->sAdd($key, $member);
$r->expire($key, $ttl);
} catch (Exception $e) {}
}
function cache_sismember(string $key, string $member): bool {
$r = redis();
if (!$r) return false;
try {
return (bool)$r->sIsMember($key, $member);
} catch (Exception $e) {
return false;
}
}