2022-01-13 10:51:45 -05:00
|
|
|
<?php
|
2022-01-30 18:02:52 -05:00
|
|
|
namespace App\Cache;
|
2022-01-13 10:51:45 -05:00
|
|
|
|
2022-02-13 16:21:08 -05:00
|
|
|
use App\Helpers\Misc;
|
|
|
|
|
2022-01-13 10:51:45 -05:00
|
|
|
class JSONCache {
|
2022-02-13 16:21:08 -05:00
|
|
|
private string $cache_path = '';
|
2022-01-25 11:20:11 -05:00
|
|
|
|
|
|
|
function __construct() {
|
2022-02-13 16:21:08 -05:00
|
|
|
$this->cache_path = Misc::env('API_CACHE_JSON', __DIR__ . '/../../cache/api');
|
2022-01-25 11:20:11 -05:00
|
|
|
}
|
2022-02-13 16:21:08 -05:00
|
|
|
|
|
|
|
public function get(string $cache_key): ?object {
|
2022-01-25 11:20:11 -05:00
|
|
|
$filename = $this->cache_path . '/' . $cache_key . '.json';
|
|
|
|
if (is_file($filename)) {
|
|
|
|
$json_string = file_get_contents($filename);
|
2022-01-13 10:51:45 -05:00
|
|
|
$element = json_decode($json_string);
|
2022-02-13 16:21:08 -05:00
|
|
|
return $element;
|
2022-01-13 10:51:45 -05:00
|
|
|
}
|
2022-02-13 16:21:08 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function exists(string $cache_key): bool {
|
|
|
|
$filename = $this->cache_path . '/' . $cache_key . '.json';
|
|
|
|
return is_file($filename);
|
2022-01-13 10:51:45 -05:00
|
|
|
}
|
|
|
|
|
2022-02-13 16:21:08 -05:00
|
|
|
public function set(string $cache_key, string $data, $timeout = 3600) {
|
|
|
|
file_put_contents($this->cache_path . '/' . $cache_key . '.json', $data);
|
2022-01-13 10:51:45 -05:00
|
|
|
}
|
|
|
|
}
|