c
This commit is contained in:
33
inc/Hura8/Traits/ClassCacheTrait.php
Normal file
33
inc/Hura8/Traits/ClassCacheTrait.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?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];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user