proxitok/app/Cache/JSONCache.php

31 lines
953 B
PHP
Raw Normal View History

2022-01-13 15:51:45 +00:00
<?php
2022-01-30 23:02:52 +00:00
namespace App\Cache;
2022-01-13 15:51:45 +00:00
class JSONCache {
2022-02-16 14:20:35 +00:00
private string $cache_path = __DIR__ . '/../../cache/api';
function __construct() {
2022-02-16 14:20:35 +00:00
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 {
$filename = $this->cache_path . '/' . $cache_key . '.json';
if (is_file($filename)) {
$json_string = file_get_contents($filename);
2022-01-13 15:51:45 +00:00
$element = json_decode($json_string);
return $element;
2022-01-13 15:51:45 +00:00
}
return null;
2022-01-13 15:51:45 +00:00
}
2022-03-11 22:18:26 +00:00
public function exists(string $cache_key): bool {
$filename = $this->cache_path . '/' . $cache_key . '.json';
return is_file($filename);
}
2022-02-16 14:20:35 +00:00
public function set(string $cache_key, mixed $data, $timeout = 3600) {
file_put_contents($this->cache_path . '/' . $cache_key . '.json', $data);
2022-01-13 15:51:45 +00:00
}
}