From 0fa7358c4babe99035897f3f3f6403fc1abc49bf Mon Sep 17 00:00:00 2001 From: Thayol Date: Wed, 6 Jan 2021 19:05:38 +0100 Subject: [PATCH] Cacher gardening --- libraries/osu_cacher.php | 49 +++++++++++++++++++++++---------------- libraries/osu_library.php | 6 ++--- libraries/utils.php | 23 ++++++++++++++++++ main.php | 2 +- 4 files changed, 56 insertions(+), 24 deletions(-) diff --git a/libraries/osu_cacher.php b/libraries/osu_cacher.php index b926bf1..9de31a7 100644 --- a/libraries/osu_cacher.php +++ b/libraries/osu_cacher.php @@ -1,22 +1,23 @@ root = $root; + $this->original_root = $original_root; $this->cache_root = $cache_root; - if (!file_exists($cache_root)) mkdir($cache_root, 0777, true); + utils::make_directory($cache_root); } - public function get_root() : string + public function get_original_root() : string { - return $this->root; + return $this->original_root; } public function get_cache_root() : string @@ -26,7 +27,7 @@ class osu_cacher public function get_cached_path(string $path) : string { - return str_replace($this->root, $this->cache_root, $path) . ".json"; + return str_replace($this->get_original_root(), $this->cache_root, $path) . ".json"; } public function is_cached(string $path) : bool @@ -34,29 +35,37 @@ class osu_cacher return file_exists($this->get_cached_path($path)); } - public function get_cache(string $path, $hash=false)// : array|bool // see you again in php8 + public static function is_hash_invalid(array $json, string $hash) : bool + { + if (empty($hash)) return false; // empty means hash check has to be skipped + if (empty($json["hash"])) return true; // json without a hash is invalid + + return $json["hash"] == $hash; + } + + public function get_cache(string $path, string $hash="")// : array|bool // see you again in php8 { if (!$this->is_cached($path)) return false; - $raw = file_get_contents($this->get_cached_path($path)); - $json = json_decode($raw, true); + $json = utils::load_json($this->get_cached_path($path)); if (empty($json)) return false; // cache had empty save - - if ($hash !== false && $hash != ($json["hash"] ?? false)) return false; // hash check failed + if (self::is_hash_invalid($json, $hash)) return false; return $json; } + public static function set_hash_if_not_present(array &$json, string $path) : void + { + if (!isset($json["hash"])) $json["hash"] = hash_file(self::$hash_function, $path); + } + public function set_cache(string $path, array $content) : void { $cache_path = $this->get_cached_path($path); - $cache_dir = dirname($cache_path); - if (!file_exists($cache_dir)) mkdir($cache_dir, 0777, true); + self::set_hash_if_not_present($content, $path); - if (!isset($content["hash"])) $content["hash"] = hash_file(self::$hash_function, $path); - - $encoded = json_encode($content); - file_put_contents($cache_path, $encoded); + utils::make_directory(dirname($cache_path)); + file_put_contents($cache_path, json_encode($content)); } } \ No newline at end of file diff --git a/libraries/osu_library.php b/libraries/osu_library.php index a120a0b..1d85bdb 100644 --- a/libraries/osu_library.php +++ b/libraries/osu_library.php @@ -28,7 +28,7 @@ class osu_library $root = rtrim($root, "/"); // remove trailing slash(es) // giving a different library should always cause a full rescan - if ($this->get_root() !== $root) + if ($this->get_osu_root() !== $root) { $this->rescan_library($root); } @@ -176,7 +176,7 @@ class osu_library if (isset($this->db["library"][$key])) unset($this->db["library"][$key]); } - public function get_root() : string + public function get_osu_root() : string { return $this->db["root"] ?? ""; } @@ -228,7 +228,7 @@ class osu_library public function get_library_folder() : string { - return $this->get_root() . "/" . self::$song_library_folder; + return $this->get_osu_root() . "/" . self::$song_library_folder; } public function get_library() : array diff --git a/libraries/utils.php b/libraries/utils.php index edcea09..0187f47 100644 --- a/libraries/utils.php +++ b/libraries/utils.php @@ -26,4 +26,27 @@ class utils } } } + + // does nothing if the directory already exists + public static function make_directory(string $directory) : void + { + if (!file_exists($directory)) + { + mkdir($directory, 0777, true); + } + } + + public static function load_json(string $path) : array + { + try + { + $raw = file_get_contents($path); + } + catch + { + $raw = ""; + } + + return json_decode($raw, true) ?? array(); + } } \ No newline at end of file diff --git a/main.php b/main.php index 60b8cec..603e6c8 100644 --- a/main.php +++ b/main.php @@ -172,7 +172,7 @@ if ($display == "main") } $mapset_count = count($lib->get_library()); - $osu_root = $lib->get_root(); + $osu_root = $lib->get_osu_root(); $parse_time = round($parse_time, 3); $scan_time = round($lib->get_scan_time(), 3);