-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathClientSideCacheBuilder.php
More file actions
162 lines (147 loc) · 4.54 KB
/
Copy pathClientSideCacheBuilder.php
File metadata and controls
162 lines (147 loc) · 4.54 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
declare(strict_types=1);
namespace ValkeyGlide\Cache;
use ValkeyGlideException;
/**
* Builder for ClientSideCache.
*/
class ClientSideCacheBuilder
{
private ?int $maxCacheKb = null;
private ?int $entryTtlMs = null;
private ?int $evictionPolicy = null;
private bool $enableMetrics = false;
/**
* Sets the maximum cache size in kilobytes.
*
* This limits the total memory used by cached keys and values.
* When this limit is reached, entries are evicted based on the eviction policy.
*
* @param int $maxCacheKb Maximum cache size in KB (must be positive).
* @return self This builder instance for method chaining.
*/
public function maxCacheKb(int $maxCacheKb): self
{
if ($maxCacheKb <= 0) {
throw new ValkeyGlideException("maxCacheKb must be positive");
}
$this->maxCacheKb = $maxCacheKb;
return $this;
}
/**
* Sets the Time-To-Live for cached entries in milliseconds.
*
* After this duration, entries automatically expire and are removed
* from the cache. Set to 0 to disable TTL expiration (entries remain
* until evicted or invalidated).
*
* @param int $entryTtlMs TTL in milliseconds (must be non-negative, 0 = no expiration).
* @return self This builder instance for method chaining.
*/
public function entryTtlMs(int $entryTtlMs): self
{
if ($entryTtlMs < 0) {
throw new ValkeyGlideException("entryTtlMs must be non-negative (0 = no expiration)");
}
$this->entryTtlMs = $entryTtlMs;
return $this;
}
/**
* Sets the eviction policy for when the cache reaches its maximum size.
*
* @param int $evictionPolicy One of ClientSideCache::EVICTION_LRU or ClientSideCache::EVICTION_LFU.
* @return self This builder instance for method chaining.
*/
public function evictionPolicy(int $evictionPolicy): self
{
if (
$evictionPolicy !== ClientSideCache::EVICTION_LRU
&& $evictionPolicy !== ClientSideCache::EVICTION_LFU
) {
throw new ValkeyGlideException(
"evictionPolicy must be ClientSideCache::EVICTION_LRU or ClientSideCache::EVICTION_LFU"
);
}
$this->evictionPolicy = $evictionPolicy;
return $this;
}
/**
* Enables collection of cache metrics such as hit/miss rates.
*
* @param bool $enableMetrics Whether to enable metrics.
* @return self This builder instance for method chaining.
*/
public function enableMetrics(bool $enableMetrics = true): self
{
$this->enableMetrics = $enableMetrics;
return $this;
}
/**
* Gets the maximum cache size in kilobytes.
*
* @return int The maximum cache size in KB.
*/
public function getMaxCacheKb(): int
{
if ($this->maxCacheKb === null) {
throw new ValkeyGlideException("maxCacheKb is required");
}
return $this->maxCacheKb;
}
/**
* Gets the entry TTL in milliseconds.
*
* @return int The entry TTL in milliseconds (0 = no expiration).
*/
public function getEntryTtlMs(): int
{
if ($this->entryTtlMs === null) {
throw new ValkeyGlideException("entryTtlMs is required");
}
return $this->entryTtlMs;
}
/**
* Gets the eviction policy.
*
* @return int|null The eviction policy constant, or null for default.
*/
public function getEvictionPolicy(): ?int
{
return $this->evictionPolicy;
}
/**
* Gets whether metrics collection is enabled.
*
* @return bool True if metrics are enabled.
*/
public function getEnableMetrics(): bool
{
return $this->enableMetrics;
}
/**
* Gets the auto-generated unique cache ID.
*
* Each call to build() generates a new unique cache ID using a GUID.
*
* @return string The cache ID.
*/
public function getCacheId(): string
{
return bin2hex(random_bytes(16));
}
/**
* Builds the ClientSideCache configuration.
*
* @return ClientSideCache The immutable client-side cache configuration.
*/
public function build(): ClientSideCache
{
if ($this->maxCacheKb === null) {
throw new ValkeyGlideException("maxCacheKb is required");
}
if ($this->entryTtlMs === null) {
throw new ValkeyGlideException("entryTtlMs is required");
}
return new ClientSideCache($this);
}
}