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

@ -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 '';