-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOpenapiArrayCache.php
More file actions
64 lines (53 loc) · 1.29 KB
/
OpenapiArrayCache.php
File metadata and controls
64 lines (53 loc) · 1.29 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace Openapi\Cache;
/**
* In-memory cache implementation
* Data is stored in PHP arrays and cleared at end of script execution
*/
class OpenapiArrayCache implements OpenapiCacheInterface
{
private array $cache = [];
private array $expiry = [];
/**
* Retrieve value from cache
* Returns null if key doesn't exist or has expired
*/
public function get(string $key): mixed
{
if (!isset($this->cache[$key])) {
return null;
}
// Check if expired
if (isset($this->expiry[$key]) && time() > $this->expiry[$key]) {
$this->delete($key);
return null;
}
return $this->cache[$key];
}
/**
* Save value to cache with expiration
*/
public function save(string $key, mixed $value, int $ttl = 3600): bool
{
$this->cache[$key] = $value;
$this->expiry[$key] = time() + $ttl;
return true;
}
/**
* Delete value from cache
*/
public function delete(string $key): bool
{
unset($this->cache[$key], $this->expiry[$key]);
return true;
}
/**
* Clear all cached values
*/
public function clear(): bool
{
$this->cache = [];
$this->expiry = [];
return true;
}
}