Cacher gardening
This commit is contained in:
+29
-20
@@ -1,22 +1,23 @@
|
|||||||
<?php
|
<?php
|
||||||
|
require_once "libraries/utils.php";
|
||||||
|
|
||||||
class osu_cacher
|
class osu_cacher
|
||||||
{
|
{
|
||||||
public static $hash_function = "md5";
|
public static $hash_function = "md5"; // crc32 was another option, but md5 produced better filesizes
|
||||||
// public static $hash_function = "crc32";
|
|
||||||
|
|
||||||
private $root;
|
private $original_root;
|
||||||
private $cache_root;
|
private $cache_root;
|
||||||
|
|
||||||
public function __construct(string $root, string $cache_root)
|
public function __construct(string $original_root, string $cache_root)
|
||||||
{
|
{
|
||||||
$this->root = $root;
|
$this->original_root = $original_root;
|
||||||
$this->cache_root = $cache_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
|
public function get_cache_root() : string
|
||||||
@@ -26,7 +27,7 @@ class osu_cacher
|
|||||||
|
|
||||||
public function get_cached_path(string $path) : string
|
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
|
public function is_cached(string $path) : bool
|
||||||
@@ -34,29 +35,37 @@ class osu_cacher
|
|||||||
return file_exists($this->get_cached_path($path));
|
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;
|
if (!$this->is_cached($path)) return false;
|
||||||
|
|
||||||
$raw = file_get_contents($this->get_cached_path($path));
|
$json = utils::load_json($this->get_cached_path($path));
|
||||||
$json = json_decode($raw, true);
|
|
||||||
|
|
||||||
if (empty($json)) return false; // cache had empty save
|
if (empty($json)) return false; // cache had empty save
|
||||||
|
if (self::is_hash_invalid($json, $hash)) return false;
|
||||||
if ($hash !== false && $hash != ($json["hash"] ?? false)) return false; // hash check failed
|
|
||||||
|
|
||||||
return $json;
|
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
|
public function set_cache(string $path, array $content) : void
|
||||||
{
|
{
|
||||||
$cache_path = $this->get_cached_path($path);
|
$cache_path = $this->get_cached_path($path);
|
||||||
$cache_dir = dirname($cache_path);
|
self::set_hash_if_not_present($content, $path);
|
||||||
if (!file_exists($cache_dir)) mkdir($cache_dir, 0777, true);
|
|
||||||
|
|
||||||
if (!isset($content["hash"])) $content["hash"] = hash_file(self::$hash_function, $path);
|
utils::make_directory(dirname($cache_path));
|
||||||
|
file_put_contents($cache_path, json_encode($content));
|
||||||
$encoded = json_encode($content);
|
|
||||||
file_put_contents($cache_path, $encoded);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -28,7 +28,7 @@ class osu_library
|
|||||||
$root = rtrim($root, "/"); // remove trailing slash(es)
|
$root = rtrim($root, "/"); // remove trailing slash(es)
|
||||||
|
|
||||||
// giving a different library should always cause a full rescan
|
// 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);
|
$this->rescan_library($root);
|
||||||
}
|
}
|
||||||
@@ -176,7 +176,7 @@ class osu_library
|
|||||||
if (isset($this->db["library"][$key])) unset($this->db["library"][$key]);
|
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"] ?? "";
|
return $this->db["root"] ?? "";
|
||||||
}
|
}
|
||||||
@@ -228,7 +228,7 @@ class osu_library
|
|||||||
|
|
||||||
public function get_library_folder() : string
|
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
|
public function get_library() : array
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -172,7 +172,7 @@ if ($display == "main")
|
|||||||
}
|
}
|
||||||
|
|
||||||
$mapset_count = count($lib->get_library());
|
$mapset_count = count($lib->get_library());
|
||||||
$osu_root = $lib->get_root();
|
$osu_root = $lib->get_osu_root();
|
||||||
$parse_time = round($parse_time, 3);
|
$parse_time = round($parse_time, 3);
|
||||||
$scan_time = round($lib->get_scan_time(), 3);
|
$scan_time = round($lib->get_scan_time(), 3);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user