From 8549cff1190800aee435ac062463ecdb89a9d1c3 Mon Sep 17 00:00:00 2001 From: Netkas Date: Tue, 3 Oct 2023 17:34:09 -0400 Subject: [PATCH] Refactored RuntimeCache.php --- src/ncc/Classes/ShutdownHandler.php | 1 - src/ncc/Utilities/RuntimeCache.php | 81 ++++------------------------- 2 files changed, 10 insertions(+), 72 deletions(-) diff --git a/src/ncc/Classes/ShutdownHandler.php b/src/ncc/Classes/ShutdownHandler.php index b3cad5e..983709e 100644 --- a/src/ncc/Classes/ShutdownHandler.php +++ b/src/ncc/Classes/ShutdownHandler.php @@ -85,7 +85,6 @@ try { - RuntimeCache::clearCache(); Functions::finalizePermissions(); } catch (Exception $e) diff --git a/src/ncc/Utilities/RuntimeCache.php b/src/ncc/Utilities/RuntimeCache.php index f06ada3..61132ba 100644 --- a/src/ncc/Utilities/RuntimeCache.php +++ b/src/ncc/Utilities/RuntimeCache.php @@ -36,21 +36,14 @@ */ private static $cache = []; - /** - * An array of files to delete when the cache is cleared - * - * @var string[] - */ - private static $temporary_files = []; - /** * Sets a value, returns the value * - * @param $key + * @param string $key * @param $value * @return mixed */ - public static function set($key, $value): mixed + public static function set(string $key, mixed $value): mixed { self::$cache[$key] = $value; return $value; @@ -59,76 +52,22 @@ /** * Gets an existing value, null if it doesn't exist * - * @param $key + * @param string $key * @return mixed|null */ - public static function get($key): mixed + public static function get(string $key): mixed { - if(isset(self::$cache[$key])) - return self::$cache[$key]; - - return null; + return self::$cache[$key] ?? null; } /** - * Sets a file as temporary, it will be deleted when the cache is cleared + * Returns True if the key exists * - * @param string $path - * @return void + * @param string $key + * @return bool */ - public static function setFileAsTemporary(string $path): void + public static function exists(string $key): bool { - Console::outDebug($path); - if(!in_array($path, self::$temporary_files)) - self::$temporary_files[] = $path; - } - - /** - * Removes a file from the temporary files list - * - * @param string $path - * @return void - * @noinspection PhpUnused - */ - public static function removeFileAsTemporary(string $path): void - { - Console::outDebug($path); - if(in_array($path, self::$temporary_files)) - unset(self::$temporary_files[array_search($path, self::$temporary_files)]); - } - - /** - * @param bool $clear_memory - * @param bool $clear_files - * @return void - */ - public static function clearCache(bool $clear_memory=true, bool $clear_files=true): void - { - Console::outDebug('clearing cache'); - - if($clear_memory) - { - Console::outDebug(sprintf('clearing memory cache (%d entries)', count(self::$cache))); - self::$cache = []; - } - - if($clear_files) - { - Console::outDebug('clearing temporary files'); - $filesystem = new Filesystem(); - foreach(self::$temporary_files as $file) - { - try - { - $filesystem->remove($file); - Console::outDebug(sprintf('deleted temporary file \'%s\'', $file)); - } - catch (Exception $e) - { - Console::outDebug(sprintf('failed to delete temporary file \'%s\', %s', $file, $e->getMessage())); - unset($e); - } - } - } + return isset(self::$cache[$key]); } } \ No newline at end of file