Require cached contents in MD_STD_CACHE to be 3 chars long

An empty json array is 2 chars long
This commit is contained in:
Joshua Ramon Enslin 2020-12-01 00:05:59 +01:00
parent a38c3c6fae
commit 35c0fe4723
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE

View File

@ -16,6 +16,27 @@ final class MD_STD_CACHE {
/** @var integer */ /** @var integer */
public static int $redis_port = 6379; public static int $redis_port = 6379;
/**
* Shutdown function for caching contents of output buffer.
*
* @param string $redisKey Key to cache by in redis.
* @param integer $expiry Expiration time in seconds.
*
* @return void
*/
public static function shutdown_cache_through_redis(string $redisKey, int $expiry = 3600):void {
$outputT = trim(MD_STD::minimizeHTMLString(MD_STD::ob_get_clean()));
echo $outputT;
$redis = new Redis();
$redis->connect(self::$redis_host, self::$redis_port, 1, NULL, 0, 0, ['auth' => [MD_CONF::$redis_pw]]);
$redis->set($redisKey, $outputT);
$redis->expire($redisKey, $expiry);
$redis->close();
}
/** /**
* Caches and serves a page through redis. Should be called at the start * Caches and serves a page through redis. Should be called at the start
* of the script generating a page. * of the script generating a page.
@ -34,26 +55,26 @@ final class MD_STD_CACHE {
ob_start(); ob_start();
if (($redisResult = $redis->get($redisKey))) { if (($redisResult = $redis->get($redisKey))) {
if (strlen($redisResult) > 3) {
$redis->close(); $redis->close();
return $redisResult; return $redisResult;
} }
else {
register_shutdown_function(function(string $redisKey, int $expiry = 3600) :void {
self::shutdown_cache_through_redis($redisKey, $expiry);
}, $redisKey);
}
}
else { else {
register_shutdown_function(function(string $redisKey, int $expiry = 3600) :void { register_shutdown_function(function(string $redisKey, int $expiry = 3600) :void {
self::shutdown_cache_through_redis($redisKey, $expiry);
$outputT = MD_STD::minimizeHTMLString(MD_STD::ob_get_clean());
echo $outputT;
$redis = new Redis();
$redis->connect(self::$redis_host, self::$redis_port, 1, NULL, 0, 0, ['auth' => [MD_CONF::$redis_pw]]);
$redis->set($redisKey, $outputT);
$redis->expire($redisKey, $expiry);
$redis->close();
}, $redisKey); }, $redisKey);
} }
$redis->close();
} }
$redis->close(); $redis->close();