-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathClientSideCache.php
More file actions
138 lines (123 loc) · 3.88 KB
/
Copy pathClientSideCache.php
File metadata and controls
138 lines (123 loc) · 3.88 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
<?php
declare(strict_types=1);
namespace ValkeyGlide\Cache;
/**
* Configuration for client-side caching with TTL-based expiration.
*
* This class configures a local cache that stores read command responses
* on the client side to reduce network round-trips and server load. The cache
* uses Time-To-Live (TTL) based expiration, where entries are automatically
* removed after a specified duration.
*
* Cached entries expire based on TTL. Server-side key changes are not propagated
* to the cache, so values may become stale before TTL expires.
* Expiration is lazy — entries are removed when accessed after their TTL, not
* proactively in the background.
* Supported read commands: GET, HGETALL, SMEMBERS.
*
* In order for 2 clients to share the same cache, they must be created with the
* same ClientSideCache instance.
*
* Clients with different ClientSideCache instances will have separate caches,
* even if the configurations are identical.
* Clients using different DBs cannot share the same cache.
* Clients using different ACL users cannot share the same cache.
*/
class ClientSideCache
{
/** @var int Eviction policy: Least Recently Used */
public const EVICTION_LRU = 0;
/** @var int Eviction policy: Least Frequently Used */
public const EVICTION_LFU = 1;
private string $cacheId;
private int $maxCacheKb;
private int $entryTtlMs;
private ?int $evictionPolicy;
private bool $enableMetrics;
/**
* Creates a new ClientSideCache from a builder.
*
* @param ClientSideCacheBuilder $builder The builder containing configuration values.
*/
public function __construct(ClientSideCacheBuilder $builder)
{
$this->cacheId = $builder->getCacheId();
$this->maxCacheKb = $builder->getMaxCacheKb();
$this->entryTtlMs = $builder->getEntryTtlMs();
$this->evictionPolicy = $builder->getEvictionPolicy();
$this->enableMetrics = $builder->getEnableMetrics();
}
/**
* Creates a new ClientSideCache builder.
*
* @return ClientSideCacheBuilder A new builder instance.
*/
public static function builder(): ClientSideCacheBuilder
{
return new ClientSideCacheBuilder();
}
/**
* Gets the unique cache identifier.
*
* @return string The cache ID.
*/
public function getCacheId(): string
{
return $this->cacheId;
}
/**
* Gets the maximum cache size in kilobytes.
*
* @return int The maximum cache size in KB.
*/
public function getMaxCacheKb(): int
{
return $this->maxCacheKb;
}
/**
* Gets the entry TTL in milliseconds.
*
* @return int The entry TTL in milliseconds (0 = no expiration).
*/
public function getEntryTtlMs(): int
{
return $this->entryTtlMs;
}
/**
* Gets the eviction policy.
*
* @return int|null The eviction policy constant, or null for default (LRU).
*/
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;
}
/**
* Converts the configuration to an associative array suitable for passing
* to ValkeyGlide::connect() or ValkeyGlideCluster::__construct().
*
* @return array The configuration as an associative array.
*/
public function toArray(): array
{
$result = [
'cache_id' => $this->cacheId,
'max_cache_kb' => $this->maxCacheKb,
'entry_ttl_ms' => $this->entryTtlMs,
'enable_metrics' => $this->enableMetrics,
];
if ($this->evictionPolicy !== null) {
$result['eviction_policy'] = $this->evictionPolicy;
}
return $result;
}
}