aboutsummaryrefslogtreecommitdiff
path: root/internal/clock/cache_ops.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-06-09 03:05:45 +0000
committerGravatar Runxi Yu2026-06-09 05:07:02 +0000
commit3dd2d95b8347b2b0572a5ad90cbb7c1c84e9a07a (patch)
tree144a3e94bd4a03097bfb29741be1028d1740422b /internal/clock/cache_ops.go
parentinternal/mru: Fewer files (diff)
signatureNo signature
internal/lru: Add sharded CLOCK
Diffstat (limited to 'internal/clock/cache_ops.go')
-rw-r--r--internal/clock/cache_ops.go51
1 files changed, 51 insertions, 0 deletions
diff --git a/internal/clock/cache_ops.go b/internal/clock/cache_ops.go
new file mode 100644
index 00000000..18958202
--- /dev/null
+++ b/internal/clock/cache_ops.go
@@ -0,0 +1,51 @@
+package clock
+
+// Add inserts or replaces key, marking it recently used.
+//
+// It reports whether the entry was admitted;
+// an entry heavier than the per-shard budget is rejected
+// and leaves the cache unchanged.
+func (cache *Cache[K, V]) Add(key K, value V) bool {
+ return cache.shardFor(key).add(key, value, cache.weightFn(key, value))
+}
+
+// Get returns the value for key and marks it recently used.
+//
+//nolint:ireturn
+func (cache *Cache[K, V]) Get(key K) (V, bool) {
+ return cache.shardFor(key).get(key)
+}
+
+// Peek returns the value for key without changing its recency.
+//
+//nolint:ireturn
+func (cache *Cache[K, V]) Peek(key K) (V, bool) {
+ return cache.shardFor(key).peek(key)
+}
+
+// Len returns the number of cached entries.
+func (cache *Cache[K, V]) Len() int {
+ total := 0
+ for _, shard := range cache.shards {
+ total += shard.len()
+ }
+
+ return total
+}
+
+// Weight returns the current total weight across all shards.
+func (cache *Cache[K, V]) Weight() uint64 {
+ var total uint64
+ for _, shard := range cache.shards {
+ total += shard.loadWeight()
+ }
+
+ return total
+}
+
+// Clear removes all entries.
+func (cache *Cache[K, V]) Clear() {
+ for _, shard := range cache.shards {
+ shard.clear()
+ }
+}