-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathArrayCache.php
More file actions
52 lines (41 loc) · 1006 Bytes
/
ArrayCache.php
File metadata and controls
52 lines (41 loc) · 1006 Bytes
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
namespace Openapi\Cache;
class ArrayCache implements CacheInterface
{
/**
* @var array<string, mixed>
*/
private array $cache = [];
/**
* @var array<string, int>
*/
private array $expiry = [];
public function get(string $key): mixed
{
if (!isset($this->cache[$key])) {
return null;
}
if (isset($this->expiry[$key]) && time() > $this->expiry[$key]) {
$this->delete($key);
return null;
}
return $this->cache[$key];
}
public function save(string $key, mixed $value, int $ttl = 3600): bool
{
$this->cache[$key] = $value;
$this->expiry[$key] = time() + $ttl;
return true;
}
public function delete(string $key): bool
{
unset($this->cache[$key], $this->expiry[$key]);
return true;
}
public function clear(): bool
{
$this->cache = [];
$this->expiry = [];
return true;
}
}