Allow passing existing redis connections to MD_STD_CACHE
This commit is contained in:
parent
bbac217aa0
commit
c38f0146dc
@ -55,18 +55,25 @@ final class MD_STD_CACHE {
|
|||||||
* 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.
|
||||||
*
|
*
|
||||||
* @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 '';
|
||||||
}
|
}
|
||||||
|
|
||||||
$redis = self::open_redis_default();
|
if ($redis === null) {
|
||||||
|
$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 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
$redis->close();
|
|
||||||
|
if ($closeRedis === true) {
|
||||||
|
$redis->close();
|
||||||
|
}
|
||||||
|
|
||||||
return '';
|
return '';
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user