Files
admin_hura_8/inc/Hura8/Traits/ClassCacheTrait.php

34 lines
740 B
PHP
Raw Normal View History

2024-01-29 10:39:53 +07:00
<?php
// 04-Mar-2023 simple array-cache for class
namespace Hura8\Traits;
trait ClassCacheTrait
{
protected static $_cache = [];
protected static function clearCache() {
self::$_cache = [];
}
protected static function deleteCacheKey($key) {
if (array_key_exists($key, self::$_cache)) {
unset(self::$_cache[$key]);
}
}
protected static function getCache($key, callable $fn_create_value) {
if (!array_key_exists($key, self::$_cache)) {
//echo 'cached --- ';
$value = call_user_func($fn_create_value);
self::$_cache[$key] = $value;
}
// echo "from-cached-".$key."<br>";
return self::$_cache[$key];
}
}