Added Redis cache
This commit is contained in:
parent
b7563d3ee9
commit
282ad20a6b
5 changed files with 53 additions and 3 deletions
31
helpers/CacheEngines/RedisCache.php
Normal file
31
helpers/CacheEngines/RedisCache.php
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
namespace Helpers\CacheEngines;
|
||||
|
||||
use \Redis;
|
||||
|
||||
class RedisCache {
|
||||
private \Redis $client;
|
||||
function __construct(string $host, int $port, ?string $password) {
|
||||
$this->client = new Redis();
|
||||
$this->client->connect($host, $port);
|
||||
if ($password) {
|
||||
$this->client->auth($password);
|
||||
}
|
||||
}
|
||||
|
||||
function __destruct() {
|
||||
$this->client->close();
|
||||
}
|
||||
|
||||
public function get(string $cache_key): object|false {
|
||||
if ($this->client->exists($cache_key)) {
|
||||
$data_string = $this->client->get($cache_key);
|
||||
return json_decode($data_string);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function set(string $cache_key, mixed $data, $timeout = 3600) {
|
||||
$this->client->set($cache_key, json_encode($data), $timeout);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue