31-01-2026 01:25

Hallo mensen, Ik heb hier een relatief simpel cache script. echter werkt dit niet... ik krijg geen errors. Kan iemand mij hier vertellen wat er mis gaat. Alvast bedankt voor het lezen.


class Cache
{
protected static string $path = __DIR__ . '/cache';

public static function get(string $key)
{
$file = self::file($key);

if (!file_exists($file)) {
return null;
}

$data = unserialize(file_get_contents($file));

if ($data['expires'] !== 0 && $data['expires'] < time()) {
unlink($file);
return null;
}

return $data['value'];
}

public static function set(string $key, mixed $value, int $ttl = 300): void
{
if (!is_dir(self::$path)) {
mkdir(self::$path, 0777, true);
}

$data = [
'expires' => $ttl === 0 ? 0 : time() + $ttl,
'value' => $value,
];

file_put_contents(self::file($key), serialize($data));
}

public static function remember(string $key, int $ttl, callable $callback)
{
$cached = self::get($key);
if ($cached !== null) {
return $cached;
}

$value = $callback();
self::set($key, $value, $ttl);

return $value;
}

protected static function file(string $key): string
{
return self::$path . '/' . md5($key) . '.cache';
}
}