| Current Path : /home/h/a/p/happyrenas/fun/public/api/ |
Linux webd005.cluster105.gra.hosting.ovh.net 5.15.206-ovh-vps-grsec-zfs-classid #1 SMP Fri May 15 02:41:25 UTC 2026 x86_64 |
| Current File : /home/h/a/p/happyrenas/fun/public/api/_cache.php |
<?php
function cache_file(string $key, array $config): string {
$dir = $config['cache']['dir'] ?? sys_get_temp_dir();
return rtrim($dir, '/\\') . '/' . sha1($key) . '.json';
}
function cache_get(string $key, array $config) {
$file = cache_file($key, $config);
if (!file_exists($file)) return null;
$raw = @file_get_contents($file);
$j = json_decode($raw ?: '', true);
if (!is_array($j)) return null;
$exp = (int)($j['expires'] ?? 0);
if ($exp > 0 && time() > $exp) {
@unlink($file);
return null;
}
return $j['data'] ?? null;
}
function cache_set(string $key, $data, int $ttlSeconds, array $config): void {
$file = cache_file($key, $config);
$payload = [
'expires' => time() + max(1, $ttlSeconds),
'data' => $data
];
@file_put_contents($file, json_encode($payload, JSON_UNESCAPED_UNICODE));
}