Require externally set up redis connection for caching + serving full pages via
redis
This commit is contained in:
parent
b143845aea
commit
3ece870f0c
@ -33,12 +33,13 @@ final class MD_STD_CACHE {
|
|||||||
/**
|
/**
|
||||||
* Shutdown function for caching contents of output buffer.
|
* Shutdown function for caching contents of output buffer.
|
||||||
*
|
*
|
||||||
|
* @param Redis $redis Redis connection.
|
||||||
* @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.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function shutdown_cache_through_redis(string $redisKey, int $expiry = 3600):void {
|
public static function shutdown_cache_through_redis(Redis $redis, string $redisKey, int $expiry = 3600):void {
|
||||||
|
|
||||||
$outputT = trim(MD_STD::minimizeHTMLString(MD_STD::ob_get_clean()));
|
$outputT = trim(MD_STD::minimizeHTMLString(MD_STD::ob_get_clean()));
|
||||||
echo $outputT;
|
echo $outputT;
|
||||||
@ -55,25 +56,19 @@ 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.
|
* @param Redis $redis Redis connection.
|
||||||
* 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, ?Redis $redis = null):string {
|
public static function serve_page_through_redis_cache(Redis $redis, string $redisKey, int $expiry = 3600):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) {
|
||||||
|
|
||||||
@ -86,26 +81,22 @@ final class MD_STD_CACHE {
|
|||||||
return $redisResult;
|
return $redisResult;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
register_shutdown_function(function(string $redisKey, int $expiry = 3600) :void {
|
register_shutdown_function(function(Redis $redis, string $redisKey, int $expiry) :void {
|
||||||
self::shutdown_cache_through_redis($redisKey, $expiry);
|
self::shutdown_cache_through_redis($redis, $redisKey, $expiry);
|
||||||
}, $redisKey);
|
}, $redis, $redisKey, $expiry);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
||||||
register_shutdown_function(function(string $redisKey, int $expiry = 3600) :void {
|
register_shutdown_function(function(Redis $redis, string $redisKey, int $expiry) :void {
|
||||||
self::shutdown_cache_through_redis($redisKey, $expiry);
|
self::shutdown_cache_through_redis($redis, $redisKey, $expiry);
|
||||||
}, $redisKey);
|
}, $redis, $redisKey, $expiry);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($closeRedis === true) {
|
|
||||||
$redis->close();
|
|
||||||
}
|
|
||||||
|
|
||||||
return '';
|
return '';
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user