-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
144 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters