<?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];
    }

}
