Wrapper update + Search by music id

This commit is contained in:
Pablo Ferreiro 2022-01-17 21:11:40 +01:00
parent fe5c2dc459
commit 4f35d68049
No known key found for this signature in database
GPG key ID: 41FBCE65B779FA24
9 changed files with 77 additions and 17 deletions

View file

@ -5,9 +5,13 @@ class RedisCache {
private \Redis $client;
function __construct(string $host, int $port, ?string $password) {
$this->client = new \Redis();
$this->client->connect($host, $port);
if (!$this->client->connect($host, $port)) {
throw new \Exception('REDIS: Could not connnect to server');
}
if ($password) {
$this->client->auth($password);
if (!$this->client->auth($password)) {
throw new \Exception('REDIS: Could not authenticate');
}
}
}
@ -15,12 +19,12 @@ class RedisCache {
$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);
public function get(string $cache_key): ?object {
$data = $this->client->get($cache_key);
if ($data) {
return json_decode($data);
}
return false;
return null;
}
public function set(string $cache_key, mixed $data, $timeout = 3600) {