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