Utility classes

2019
class Cache
{
    /**
     * All the cached values
     *
     * @var array
     */
    public static $cache = [];

    /**
     * Get a value from the cache
     *
     * @param string $key
     * @return mixed The cached value
     */
    static function get($key)
    {
        return static::has($key) ? static::$cache[$key] : null;
    }

    /**
     * Set a value in the cache
     *
     * @param string $key
     * @param mixed|callback $value
     * @return mixed The cache value
     */
    static function set($key, $value)
    {
        static::$cache[$key] = is_callable($value) ? $value() : $value;
        return static::$cache[$key];
    }

    /**
     * Check that a key exists
     *
     * @param string $key
     * @return boolean
     */
    static function has($key)
    {
        return isset(static::$cache[$key]);
    }

    /**
     * Cache function calls
     *
     * @param [type] $function
     * @param [type] ...$args
     * @return void
     */
    static function memoized($function, $args, $key = false)
    {
        static $memocache;

        $key = $key ?: $args[0];

        if (!isset($memocache[$key])) {
            $memocache[$key] = call_user_func_array($function, $args);
        }

        return $memocache[$key];
    }
}