Skip to content

Commit

Permalink
add some cache utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Plancke committed Nov 18, 2016
1 parent db40443 commit c232147
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 75 deletions.
5 changes: 3 additions & 2 deletions src/classes/HypixelObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down
74 changes: 74 additions & 0 deletions src/util/CacheUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Plancke\HypixelPHP\util;

abstract class CacheUtil {

/**
* Check whether or not a time has expired
* 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,
* positive offset to check whether or not it has been expired for X,
* negative offset to check if it will expire in X
*
* @return bool whether or not it was expired
*/
public static function isExpired($cachedTime, $duration, $offset = 0) {
return self::getRemainingTime($cachedTime, $duration, $offset) > 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);
}

}
116 changes: 44 additions & 72 deletions src/util/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand All @@ -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);
}
}
22 changes: 22 additions & 0 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Plancke\Tests;

use Plancke\HypixelPHP\util\CacheUtil;

class CacheTest extends \PHPUnit_Framework_TestCase {

function testExpire() {
$cachedTime = time() * 1000;

// has it been expired for 5 seconds?
$this->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);
}

}
2 changes: 1 addition & 1 deletion tests/util/TestUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c232147

Please sign in to comment.