Add function for opening redis connection using default settings

This commit is contained in:
Joshua Ramon Enslin 2021-11-22 23:44:53 +01:00
parent eb869071b8
commit 80af1ef260
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE

View File

@ -16,6 +16,20 @@ final class MD_STD_CACHE {
/** @var integer */ /** @var integer */
public static int $redis_port = 6379; public static int $redis_port = 6379;
/**
* Opens a connection to redis.
*
* @return Redis
*/
public static function open_redis_default():Redis {
$redis = new Redis();
$redis->connect(self::$redis_host, self::$redis_port, 1, null, 0, 0, ['auth' => [MD_CONF::$redis_pw]]);
return $redis;
}
/** /**
* Shutdown function for caching contents of output buffer. * Shutdown function for caching contents of output buffer.
* *
@ -29,8 +43,8 @@ final class MD_STD_CACHE {
$outputT = trim(MD_STD::minimizeHTMLString(MD_STD::ob_get_clean())); $outputT = trim(MD_STD::minimizeHTMLString(MD_STD::ob_get_clean()));
echo $outputT; echo $outputT;
$redis = new Redis(); $redis = self::open_redis_default();
$redis->connect(self::$redis_host, self::$redis_port, 1, null, 0, 0, ['auth' => [MD_CONF::$redis_pw]]);
$redis->set($redisKey, $outputT); $redis->set($redisKey, $outputT);
$redis->expire($redisKey, $expiry); $redis->expire($redisKey, $expiry);
$redis->close(); $redis->close();
@ -52,8 +66,8 @@ final class MD_STD_CACHE {
return ''; return '';
} }
$redis = new Redis(); $redis = self::open_redis_default();
$redis->connect(self::$redis_host, self::$redis_port, 1, null, 0, 0, ['auth' => [MD_CONF::$redis_pw]]);
if ($redis->ping() !== false) { if ($redis->ping() !== false) {
ob_start(); ob_start();