-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCache.php
36 lines (28 loc) · 835 Bytes
/
Cache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
namespace API;
use Redis;
class Cache
{
private Redis $redis;
public function __construct()
{
$this->redis = new Redis();
$this->redis->connect($_ENV['REDIS_HOST'], $_ENV['REDIS_PORT']);
if (!empty($_ENV['REDIS_PASSWORD'])) {
$this->redis->auth($_ENV['REDIS_PASSWORD']);
}
}
public function get(string $key): mixed
{
$serializedData = $this->redis->get($key);
return $serializedData !== false ? unserialize($serializedData) : null;
}
public function set(string $key, mixed $data, int $cacheLifeInSeconds = -1): void
{
if ($cacheLifeInSeconds > 0) {
$this->redis->setex($key, $cacheLifeInSeconds, serialize($data));
} else {
$this->redis->set($key, serialize($data));
}
}
}