Allow passing existing redis connections to MD_STD_CACHE

This commit is contained in:
Joshua Ramon Enslin 2022-09-04 23:27:04 +02:00
parent bbac217aa0
commit c38f0146dc
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE

View File

@ -57,16 +57,23 @@ final class MD_STD_CACHE {
* *
* @param string $redisKey Key to cache by in redis. * @param string $redisKey Key to cache by in redis.
* @param integer $expiry Expiration time in seconds. * @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 * @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') { if (PHP_SAPI === 'cli') {
return ''; return '';
} }
if ($redis === null) {
$redis = self::open_redis_default(); $redis = self::open_redis_default();
$closeRedis = true;
}
else $closeRedis = false;
if ($redis->ping() !== false) { if ($redis->ping() !== false) {
@ -94,7 +101,10 @@ final class MD_STD_CACHE {
} }
} }
if ($closeRedis === true) {
$redis->close(); $redis->close();
}
return ''; return '';