-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSapiKvOps.php
More file actions
74 lines (62 loc) · 1.83 KB
/
Copy pathSapiKvOps.php
File metadata and controls
74 lines (62 loc) · 1.83 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
65
66
67
68
69
70
71
72
73
74
<?php
declare(strict_types=1);
namespace Ephpm\Cache\Symfony;
/**
* Backend that calls the global `ephpm_kv_*` functions registered by
* the ePHPm SAPI. Refuses to construct if those functions aren't present
* so we fail fast outside the runtime instead of producing
* "Call to undefined function" errors at request time.
*/
final class SapiKvOps implements KvOpsInterface
{
public function __construct()
{
if (!\function_exists('ephpm_kv_get')) {
throw new \RuntimeException(
'ephpm KV SAPI functions are not available. '
. 'This adapter only works inside the ePHPm runtime; '
. 'use Ephpm\\Cache\\Symfony\\InMemoryKvOps in tests.'
);
}
}
public function get(string $key): ?string
{
/** @var string|null */
return \ephpm_kv_get($key);
}
public function set(string $key, string $value, int $ttlSeconds = 0): bool
{
return (bool) \ephpm_kv_set($key, $value, $ttlSeconds);
}
public function del(string $key): int
{
return (int) \ephpm_kv_del($key);
}
public function exists(string $key): bool
{
return (bool) \ephpm_kv_exists($key);
}
public function incrBy(string $key, int $delta): int
{
return (int) \ephpm_kv_incr_by($key, $delta);
}
public function expire(string $key, int $ttlSeconds): bool
{
return (bool) \ephpm_kv_expire($key, $ttlSeconds);
}
public function ttl(string $key): int
{
return (int) \ephpm_kv_ttl($key);
}
public function pttl(string $key): int
{
return (int) \ephpm_kv_pttl($key);
}
public function flush(): bool
{
if (!\function_exists('ephpm_kv_flush_all')) {
return false;
}
return (bool) \ephpm_kv_flush_all();
}
}