From 35c0fe47238d00f90f2b50e12656bfd65a149ced Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Tue, 1 Dec 2020 00:05:59 +0100 Subject: [PATCH] Require cached contents in MD_STD_CACHE to be 3 chars long An empty json array is 2 chars long --- MD_STD_CACHE.php | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/MD_STD_CACHE.php b/MD_STD_CACHE.php index ce35550..89639e2 100644 --- a/MD_STD_CACHE.php +++ b/MD_STD_CACHE.php @@ -16,6 +16,27 @@ final class MD_STD_CACHE { /** @var integer */ 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 * of the script generating a page. @@ -34,26 +55,26 @@ final class MD_STD_CACHE { ob_start(); if (($redisResult = $redis->get($redisKey))) { - $redis->close(); - return $redisResult; + + if (strlen($redisResult) > 3) { + $redis->close(); + return $redisResult; + } + else { + register_shutdown_function(function(string $redisKey, int $expiry = 3600) :void { + self::shutdown_cache_through_redis($redisKey, $expiry); + }, $redisKey); + } + } else { register_shutdown_function(function(string $redisKey, int $expiry = 3600) :void { - - $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(); + self::shutdown_cache_through_redis($redisKey, $expiry); }, $redisKey); } - $redis->close(); } $redis->close();