use tab indents

This commit is contained in:
alexveebee 2025-04-24 17:10:56 +00:00
parent 416ac88407
commit 58c275472f

View File

@ -4,70 +4,63 @@ namespace Vichan\Data\Driver;
// Prevent direct access to this file for security
defined('TINYBOARD') or exit;
// Defines the required methods for any cache driver
interface CacheDriver {
public function get(string $key): mixed;
public function set(string $key, mixed $value, mixed $expires = false): void;
public function delete(string $key): void;
public function flush(): void;
}
// Handles caching using Redis, a fast in-memory data store
class RedisCacheDriver implements CacheDriver {
private string $prefix;
private \Redis $inner;
private string $prefix;
private \Redis $inner;
// Sets up the Redis connection
public function __construct(string $prefix, string $host, int $port, ?string $password, int $database) {
$this->inner = new \Redis();
$this->inner->connect($host, $port);
// Sets up the Redis connection
public function __construct(string $prefix, string $host, int $port, ?string $password, int $database) {
$this->inner = new \Redis();
$this->inner->connect($host, $port);
if ($password) {
$this->inner->auth($password);
}
if ($password) {
$this->inner->auth($password);
}
if (!$this->inner->select($database)) {
throw new \RuntimeException('Unable to select Redis database ' . $database);
}
if (!$this->inner->select($database)) {
throw new \RuntimeException('Unable to select Redis database ' . $database);
}
$this->prefix = $prefix;
}
$this->prefix = $prefix;
}
// Retrieves a value from the cache by key
public function get(string $key): mixed {
// Retrieves a value from the cache by key
public function get(string $key): mixed {
$ret = $this->inner->get($this->prefix . $key);
if ($ret === false) {
// Return null if the key doesn't exist
return null;
}
$ret = $this->inner->get($this->prefix . $key);
if ($ret === false) {
// Return null if the key doesn't exist
return null;
}
return \json_decode($ret, true);
}
return \json_decode($ret, true);
}
// Stores a value in the cache with an optional expiration time
public function set(string $key, mixed $value, mixed $expires = false): void {
// Convert the value to JSON for storage
$encodedValue = \json_encode($value);
// Stores a value in the cache with an optional expiration time
public function set(string $key, mixed $value, mixed $expires = false): void {
// Convert the value to JSON for storage
$encodedValue = \json_encode($value);
if ($expires === false || !is_numeric($expires) || $expires <= 0) {
// Store the value without an expiration
$this->inner->set($this->prefix . $key, $encodedValue);
} else {
// Store the value with an expiration time (in seconds)
$ttl_seconds = (int)$expires;
$this->inner->setex($this->prefix . $key, $ttl_seconds, $encodedValue);
}
}
if ($expires === false || !is_numeric($expires) || $expires <= 0) {
// Store the value without an expiration
$this->inner->set($this->prefix . $key, $encodedValue);
} else {
// Store the value with an expiration time (in seconds)
$ttl_seconds = (int)$expires;
$this->inner->setex($this->prefix . $key, $ttl_seconds, $encodedValue);
}
}
// Deletes a specific key from the cache
public function delete(string $key): void {
// Remove the key from Redis
$this->inner->del($this->prefix . $key);
}
// Deletes a specific key from the cache
public function delete(string $key): void {
// Remove the key from Redis
$this->inner->del($this->prefix . $key);
}
// Clears all data in the current Redis database
public function flush(): void {
$this->inner->flushDB();
}
// Clears all data in the current Redis database
public function flush(): void {
$this->inner->flushDB();
}
}