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
|
||||
]));
|
||||
|
|
|
|||
|
|
@ -25,8 +25,8 @@ class Misc {
|
|||
}
|
||||
}
|
||||
// Cache config
|
||||
if (isset($_ENV['APP_CACHE'])) {
|
||||
switch ($_ENV['APP_CACHE']) {
|
||||
if (isset($_ENV['API_CACHE'])) {
|
||||
switch ($_ENV['API_CACHE']) {
|
||||
case 'json':
|
||||
$cacheEngine = new JSONCache();
|
||||
break;
|
||||
|
|
@ -54,15 +54,24 @@ class Misc {
|
|||
$subdir = '';
|
||||
}
|
||||
$latte = new \Latte\Engine;
|
||||
$latte->setTempDirectory(__DIR__ . '/../cache/views');
|
||||
$cache_path = isset($_ENV['LATTE_CACHE']) && !empty($_ENV['LATTE_CACHE']) ? $_ENV['LATTE_CACHE'] : __DIR__ . '/../cache/latte';
|
||||
$latte->setTempDirectory($cache_path);
|
||||
|
||||
// -- CUSTOM FUNCTIONS -- //
|
||||
// Import assets
|
||||
$latte->addFunction('assets', function (string $name, string $type) use ($subdir) {
|
||||
$path = "{$subdir}/{$type}/{$name}";
|
||||
return $path;
|
||||
});
|
||||
// Relative path
|
||||
$latte->addFunction('path', function (string $name) use ($subdir) {
|
||||
$path = "{$subdir}/{$name}";
|
||||
return $path;
|
||||
});
|
||||
// Version being used
|
||||
$latte->addFunction('version', function () {
|
||||
return \Composer\InstalledVersions::getVersion('pablouser1/proxitok');
|
||||
});
|
||||
// https://stackoverflow.com/a/36365553
|
||||
$latte->addFunction('number', function (int $x) {
|
||||
if($x > 1000) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue