diff --git a/src/MD_STD_CACHE.php b/src/MD_STD_CACHE.php index 87e03a1..e498252 100644 --- a/src/MD_STD_CACHE.php +++ b/src/MD_STD_CACHE.php @@ -55,18 +55,25 @@ final class MD_STD_CACHE { * Caches and serves a page through redis. Should be called at the start * of the script generating a page. * - * @param string $redisKey Key to cache by in redis. - * @param integer $expiry Expiration time in seconds. + * @param string $redisKey Key to cache by in redis. + * @param integer $expiry Expiration time in seconds. + * @param Redis|null $redis Redis connection already opened, if one exists. + * If this parameter is not provided, a separate + * redis connection is opened for this function. * * @return string */ - public static function serve_page_through_redis_cache(string $redisKey, int $expiry = 3600):string { + public static function serve_page_through_redis_cache(string $redisKey, int $expiry = 3600, ?Redis $redis = null):string { if (PHP_SAPI === 'cli') { return ''; } - $redis = self::open_redis_default(); + if ($redis === null) { + $redis = self::open_redis_default(); + $closeRedis = true; + } + else $closeRedis = false; if ($redis->ping() !== false) { @@ -94,7 +101,10 @@ final class MD_STD_CACHE { } } - $redis->close(); + + if ($closeRedis === true) { + $redis->close(); + } return '';