2020-11-30 19:08:20 +01:00
|
|
|
<?PHP
|
|
|
|
/**
|
|
|
|
* Provides static functions for simple caching.
|
|
|
|
*
|
|
|
|
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
|
|
|
|
*/
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides caching functions.
|
|
|
|
*/
|
|
|
|
final class MD_STD_CACHE {
|
2020-12-01 00:05:59 +01:00
|
|
|
/**
|
|
|
|
* Shutdown function for caching contents of output buffer.
|
|
|
|
*
|
2023-10-05 16:45:35 +02:00
|
|
|
* @param Redis $redis Redis connection.
|
2020-12-01 00:05:59 +01:00
|
|
|
* @param string $redisKey Key to cache by in redis.
|
|
|
|
* @param integer $expiry Expiration time in seconds.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2023-10-05 16:45:35 +02:00
|
|
|
public static function shutdown_cache_through_redis(Redis $redis, string $redisKey, int $expiry = 3600):void {
|
2020-12-01 00:05:59 +01:00
|
|
|
|
|
|
|
$outputT = trim(MD_STD::minimizeHTMLString(MD_STD::ob_get_clean()));
|
|
|
|
echo $outputT;
|
|
|
|
|
|
|
|
$redis->set($redisKey, $outputT);
|
|
|
|
$redis->expire($redisKey, $expiry);
|
|
|
|
$redis->close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-30 19:08:20 +01:00
|
|
|
/**
|
|
|
|
* Caches and serves a page through redis. Should be called at the start
|
|
|
|
* of the script generating a page.
|
|
|
|
*
|
2023-10-05 16:45:35 +02:00
|
|
|
* @param string $redisKey Key to cache by in redis.
|
|
|
|
* @param integer $expiry Expiration time in seconds.
|
|
|
|
* @param Redis $redis Redis connection.
|
2020-11-30 19:08:20 +01:00
|
|
|
*
|
2020-11-30 22:36:17 +01:00
|
|
|
* @return string
|
2020-11-30 19:08:20 +01:00
|
|
|
*/
|
2023-10-05 16:45:35 +02:00
|
|
|
public static function serve_page_through_redis_cache(Redis $redis, string $redisKey, int $expiry = 3600):string {
|
2020-11-30 19:08:20 +01:00
|
|
|
|
2021-02-06 20:08:37 +01:00
|
|
|
if (PHP_SAPI === 'cli') {
|
|
|
|
return '';
|
|
|
|
}
|
2020-12-02 09:39:43 +01:00
|
|
|
|
2020-11-30 19:08:20 +01:00
|
|
|
if ($redis->ping() !== false) {
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
|
2021-12-24 02:01:40 +01:00
|
|
|
if ($redisResult = $redis->get($redisKey)) {
|
2020-12-01 00:05:59 +01:00
|
|
|
|
2021-02-09 15:18:40 +01:00
|
|
|
if (strlen($redisResult) > 3 and strpos($redisResult, ' id="errorPage"') === false) {
|
2020-12-01 00:05:59 +01:00
|
|
|
$redis->close();
|
|
|
|
return $redisResult;
|
|
|
|
}
|
|
|
|
else {
|
2023-10-05 16:45:35 +02:00
|
|
|
register_shutdown_function(function(Redis $redis, string $redisKey, int $expiry) :void {
|
|
|
|
self::shutdown_cache_through_redis($redis, $redisKey, $expiry);
|
|
|
|
}, $redis, $redisKey, $expiry);
|
2020-12-01 00:05:59 +01:00
|
|
|
}
|
|
|
|
|
2020-11-30 19:08:20 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
2023-10-05 16:45:35 +02:00
|
|
|
register_shutdown_function(function(Redis $redis, string $redisKey, int $expiry) :void {
|
|
|
|
self::shutdown_cache_through_redis($redis, $redisKey, $expiry);
|
|
|
|
}, $redis, $redisKey, $expiry);
|
2020-11-30 19:08:20 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-09-04 23:27:04 +02:00
|
|
|
|
2020-11-30 22:36:17 +01:00
|
|
|
return '';
|
2020-11-30 19:08:20 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|