Templates based on classes, cache custom paths
This commit is contained in:
parent
dde4185ad7
commit
6d6ec109ca
26 changed files with 236 additions and 156 deletions
|
|
@ -2,23 +2,30 @@
|
|||
namespace Helpers\CacheEngines;
|
||||
|
||||
class JSONCache {
|
||||
const CACHE_PATH = __DIR__ . '/../../cache/api/';
|
||||
private string $cache_path = __DIR__ . '/../../cache/api';
|
||||
|
||||
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 {
|
||||
if (is_file(self::CACHE_PATH . $cache_key . '.json')) {
|
||||
$filename = $this->cache_path . '/' . $cache_key . '.json';
|
||||
if (is_file($filename)) {
|
||||
$time = time();
|
||||
$json_string = file_get_contents(self::CACHE_PATH . $cache_key . '.json');
|
||||
$json_string = file_get_contents($filename);
|
||||
$element = json_decode($json_string);
|
||||
if ($time < $element->expires) {
|
||||
return $element->data;
|
||||
}
|
||||
// Remove file if expired
|
||||
unlink(self::CACHE_PATH . $cache_key . '.json');
|
||||
unlink($filename);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function set(string $cache_key, mixed $data, $timeout = 3600) {
|
||||
file_put_contents(self::CACHE_PATH . $cache_key . '.json', json_encode([
|
||||
file_put_contents($this->cache_path . '/' . $cache_key . '.json', json_encode([
|
||||
'data' => $data,
|
||||
'expires' => time() + $timeout
|
||||
]));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue