Fixed JSON Cache and Cache returning VERIFY_CODE

This commit is contained in:
Pablo Ferreiro 2022-03-12 19:28:37 +01:00
parent 7ba324c935
commit 8ece2c2b25
No known key found for this signature in database
GPG key ID: 41FBCE65B779FA24
3 changed files with 12 additions and 20 deletions

View file

@ -9,19 +9,14 @@ class JSONCache {
$this->cache_path = $_ENV['API_CACHE_JSON'];
}
}
public function get(string $cache_key): object|false {
public function get(string $cache_key): ?object {
$filename = $this->cache_path . '/' . $cache_key . '.json';
if (is_file($filename)) {
$time = time();
$json_string = file_get_contents($filename);
$element = json_decode($json_string);
if ($time < $element->expires) {
return $element->data;
}
// Remove file if expired
unlink($filename);
return $element;
}
return false;
return null;
}
public function exists(string $cache_key): bool {
@ -30,9 +25,6 @@ class JSONCache {
}
public function set(string $cache_key, mixed $data, $timeout = 3600) {
file_put_contents($this->cache_path . '/' . $cache_key . '.json', json_encode([
'data' => $data,
'expires' => time() + $timeout
]));
file_put_contents($this->cache_path . '/' . $cache_key . '.json', $data);
}
}