Add class MD_STD_CACHE
This commit is contained in:
parent
14c7ffb8d4
commit
558ed729dc
64
MD_STD_CACHE.php
Normal file
64
MD_STD_CACHE.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?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 {
|
||||
|
||||
/** @var string */
|
||||
public static string $redis_host = '127.0.0.1';
|
||||
/** @var string */
|
||||
public static int $redis_port = 6379;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function serve_page_through_redis_cache(string $redisKey, int $expiry = 3600):bool {
|
||||
|
||||
$redis = new Redis();
|
||||
$redis->connect(self::$redis_host, self::$redis_port, 1, NULL, 0, 0, ['auth' => [MD_CONF::$redis_pw]]);
|
||||
if ($redis->ping() !== false) {
|
||||
|
||||
ob_start();
|
||||
|
||||
if (($redisResult = $redis->get($redisKey))) {
|
||||
echo $redisResult;
|
||||
$redis->close();
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
|
||||
register_shutdown_function(function(string $redisKey, int $expiry = 3600) :void {
|
||||
|
||||
$outputT = MD_STD::minimizeHTMLString(MD_STD::ob_get_clean());
|
||||
echo $outputT;
|
||||
|
||||
$redis = new Redis();
|
||||
$redis->connect(self::$redis_host, self::$redis_port, 1, NULL, 0, 0, ['auth' => [MD_CONF::$redis_pw]]);
|
||||
$redis->set($redisKey, $outputT);
|
||||
$redis->expire($redisKey, $expiry);
|
||||
$redis->close();
|
||||
}, $redisKey);
|
||||
|
||||
}
|
||||
|
||||
$redis->close();
|
||||
}
|
||||
$redis->close();
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user