Using new wrapper

This commit is contained in:
Pablo Ferreiro 2022-02-13 22:21:08 +01:00
parent 19e2c32c77
commit a3ebb08d26
No known key found for this signature in database
GPG key ID: 41FBCE65B779FA24
22 changed files with 141 additions and 154 deletions

View file

@ -1,33 +1,31 @@
<?php
namespace App\Cache;
use App\Helpers\Misc;
class JSONCache {
private string $cache_path = __DIR__ . '/../../cache/api';
private string $cache_path = '';
function __construct() {
if (isset($_ENV['API_CACHE_JSON']) && !empty($_ENV['API_CACHE_JSON'])) {
$this->cache_path = $_ENV['API_CACHE_JSON'];
}
}
public function get(string $cache_key): object|false {
$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 false;
$this->cache_path = Misc::env('API_CACHE_JSON', __DIR__ . '/../../cache/api');
}
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
]));
public function get(string $cache_key): ?object {
$filename = $this->cache_path . '/' . $cache_key . '.json';
if (is_file($filename)) {
$json_string = file_get_contents($filename);
$element = json_decode($json_string);
return $element;
}
return null;
}
public function exists(string $cache_key): bool {
$filename = $this->cache_path . '/' . $cache_key . '.json';
return is_file($filename);
}
public function set(string $cache_key, string $data, $timeout = 3600) {
file_put_contents($this->cache_path . '/' . $cache_key . '.json', $data);
}
}

View file

@ -27,6 +27,10 @@ class RedisCache {
return null;
}
public function exists(string $cache_key): bool {
return $this->client->exists($cache_key);
}
public function set(string $cache_key, array $data, $timeout = 3600) {
$this->client->set($cache_key, json_encode($data), $timeout);
}