diff --git a/src/classes/HypixelObject.php b/src/classes/HypixelObject.php index 9713863..6d35fc8 100644 --- a/src/classes/HypixelObject.php +++ b/src/classes/HypixelObject.php @@ -4,6 +4,7 @@ use Plancke\HypixelPHP\fetch\Response; use Plancke\HypixelPHP\HypixelPHP; +use Plancke\HypixelPHP\util\CacheUtil; use Plancke\HypixelPHP\util\Utilities; abstract class HypixelObject extends APIObject { @@ -49,11 +50,11 @@ public function getCachedTime() { } /** - * @param int $extra + * @param int $extra extra time to be added to the check * @return bool */ public function isCacheExpired($extra = 0) { - return time() - $extra - $this->getHypixelPHP()->getCacheHandler()->getCacheTime($this->getCacheTimeKey()) > $this->getCachedTime(); + return CacheUtil::isExpired($this->getCachedTime() * 1000, $this->getHypixelPHP()->getCacheHandler()->getCacheTime($this->getCacheTimeKey()) * 1000, $extra); } abstract function getCacheTimeKey(); diff --git a/src/util/CacheUtil.php b/src/util/CacheUtil.php new file mode 100644 index 0000000..5805bf7 --- /dev/null +++ b/src/util/CacheUtil.php @@ -0,0 +1,74 @@ + 0; + } + + /** + * Check how much time is left in the cache + * All times supplied should be in milliseconds + * + * @param int $cachedTime timestamp of when something was cached + * @param int $duration duration of cache + * @param array|int $offset offset can be a number or array of numbers where a random will be picked + * + * @return int remaining time to keep cache + */ + public static function getRemainingTime($cachedTime, $duration, $offset = 0) { + $offsetValue = 0; + if (is_array($offset)) { + if (sizeof($offset) == 2) { + $offsetValue = random_int($offset[0], $offset[1]); + } + } elseif (is_numeric($offset)) { + $offsetValue = $offset; + } + + return (time() * 1000) + $offsetValue - $duration - $cachedTime; + } + + /** + * Generate a filename for a given input, first few characters + * become directories so less files per directory. + * This improves speed for the OS + * + * @param $input + * @param int $dirs + * + * @return string + */ + public static function getCacheFileName($input, $dirs = 2) { + $input = strtolower($input); + $input = trim($input); + $input = str_replace(' ', '%20', $input); + + if (strlen($input) <= $dirs) { + $parts = str_split($input, 1); + } else { + $parts = []; + for ($i = 0; $i < $dirs; $i++) { + array_push($parts, substr($input, $i, 1)); + } + array_push($parts, substr($input, $dirs)); + } + + return implode(DIRECTORY_SEPARATOR, $parts); + } + +} \ No newline at end of file diff --git a/src/util/Utilities.php b/src/util/Utilities.php index c25a103..6f2e90e 100644 --- a/src/util/Utilities.php +++ b/src/util/Utilities.php @@ -48,50 +48,6 @@ abstract class Utilities { "RESET" => '§r' ]; - public static function ensureDashedUUID($uuid) { - if (strpos($uuid, "-")) { - if (strlen($uuid) == 32) { - return $uuid; - } - $uuid = Utilities::ensureNoDashesUUID($uuid); - } - return substr($uuid, 0, 8) . "-" . substr($uuid, 8, 12) . substr($uuid, 12, 16) . "-" . substr($uuid, 16, 20) . "-" . substr($uuid, 20, 32); - } - - public static function ensureNoDashesUUID($uuid) { - return str_replace("-", "", $uuid); - } - - /** - * @param $filename - * - * @return null|string - */ - public static function getFileContent($filename) { - $content = null; - if (file_exists($filename)) { - $file = fopen($filename, 'r+'); - if (filesize($filename) > 0) { - $content = fread($file, filesize($filename)); - } - fclose($file); - } - return $content; - } - - /** - * @param $filename - * @param $content - */ - public static function setFileContent($filename, $content) { - if (!file_exists(dirname($filename))) { - @mkdir(dirname($filename), 0777, true); - } - $file = fopen($filename, 'w+'); - fwrite($file, $content); - fclose($file); - } - /** * Parses MC encoded colors to HTML * @param $string @@ -184,34 +140,6 @@ public static function hex2rgb($hex) { return [$r, $g, $b]; } - /** - * - * Generate a filename for a given input, first few characters - * become directories so less files per directory. - * This improves speed for the OS - * - * @param $input - * @param int $dirs - * @return string - */ - public static function getCacheFileName($input, $dirs = 2) { - $input = strtolower($input); - $input = trim($input); - $input = str_replace(' ', '%20', $input); - - if (strlen($input) <= $dirs) { - $parts = str_split($input, 1); - } else { - $parts = []; - for ($i = 0; $i < $dirs; $i++) { - array_push($parts, substr($input, $i, 1)); - } - array_push($parts, substr($input, $dirs)); - } - - return implode(DIRECTORY_SEPARATOR, $parts); - } - /** * Get a value recursively in an array * @@ -228,4 +156,48 @@ public static function getRecursiveValue($array, $key, $default = null, $delimit } return $return ? $return : $default; } + + public static function ensureDashedUUID($uuid) { + if (strpos($uuid, "-")) { + if (strlen($uuid) == 32) { + return $uuid; + } + $uuid = Utilities::ensureNoDashesUUID($uuid); + } + return substr($uuid, 0, 8) . "-" . substr($uuid, 8, 12) . substr($uuid, 12, 16) . "-" . substr($uuid, 16, 20) . "-" . substr($uuid, 20, 32); + } + + public static function ensureNoDashesUUID($uuid) { + return str_replace("-", "", $uuid); + } + + /** + * @param $filename + * + * @return null|string + */ + public static function getFileContent($filename) { + $content = null; + if (file_exists($filename)) { + $file = fopen($filename, 'r+'); + if (filesize($filename) > 0) { + $content = fread($file, filesize($filename)); + } + fclose($file); + } + return $content; + } + + /** + * @param $filename + * @param $content + */ + public static function setFileContent($filename, $content) { + if (!file_exists(dirname($filename))) { + @mkdir(dirname($filename), 0777, true); + } + $file = fopen($filename, 'w+'); + fwrite($file, $content); + fclose($file); + } } \ No newline at end of file diff --git a/tests/CacheTest.php b/tests/CacheTest.php new file mode 100644 index 0000000..57d98e2 --- /dev/null +++ b/tests/CacheTest.php @@ -0,0 +1,22 @@ +assertTrue(CacheUtil::isExpired($cachedTime, 0, 5000)); + + // will it still be valid in 5 seconds? + $this->assertFalse(CacheUtil::isExpired($cachedTime, 2000, -5000)); + + $remain = CacheUtil::getRemainingTime($cachedTime, 0, [2000, 3000]); + $this->assertTrue($remain >= 2000 && $remain <= 3000); + } + +} \ No newline at end of file diff --git a/tests/util/TestUtil.php b/tests/util/TestUtil.php index 0413670..a2b56d3 100644 --- a/tests/util/TestUtil.php +++ b/tests/util/TestUtil.php @@ -7,7 +7,7 @@ class TestUtil { static function getHypixelPHP() { - $HypixelPHP = new HypixelPHP(""); + $HypixelPHP = new HypixelPHP(''); // log to error $HypixelPHP->setLogger(new CustomLogger($HypixelPHP)); // only fetching