From f4f0d7ee3f99fa363b8a4b478f8820f7cf926dc3 Mon Sep 17 00:00:00 2001 From: Wang Xiaoqing Date: Mon, 14 Oct 2024 19:12:10 +0800 Subject: [PATCH] Remove the policy interface (#393) Remove the policy interface, as only one policy is being maintained. --- cache.go | 2 +- policy.go | 33 +-------------------------------- store.go | 4 ++-- ttl.go | 2 +- 4 files changed, 5 insertions(+), 36 deletions(-) diff --git a/cache.go b/cache.go index 7a40b0e7..86ef5813 100644 --- a/cache.go +++ b/cache.go @@ -52,7 +52,7 @@ type Cache[K Key, V any] struct { // storedItems is the central concurrent hashmap where key-value items are stored. storedItems store[V] // cachePolicy determines what gets let in to the cache and what gets kicked out. - cachePolicy policy[V] + cachePolicy *defaultPolicy[V] // getBuf is a custom ring buffer implementation that gets pushed to when // keys are read. getBuf *ringBuffer diff --git a/policy.go b/policy.go index 290488d8..08963b1e 100644 --- a/policy.go +++ b/policy.go @@ -30,38 +30,7 @@ const ( lfuSample = 5 ) -// policy is the interface encapsulating eviction/admission behavior. -// TODO: remove this interface and just rename defaultPolicy to policy, as we -// are probably only going to use/implement/maintain one policy. -type policy[V any] interface { - ringConsumer - // Add attempts to Add the key-cost pair to the Policy. It returns a slice - // of evicted keys and a bool denoting whether or not the key-cost pair - // was added. If it returns true, the key should be stored in cache. - Add(uint64, int64) ([]*Item[V], bool) - // Has returns true if the key exists in the Policy. - Has(uint64) bool - // Del deletes the key from the Policy. - Del(uint64) - // Cap returns the available capacity. - Cap() int64 - // Close stops all goroutines and closes all channels. - Close() - // Update updates the cost value for the key. - Update(uint64, int64) - // Cost returns the cost value of a key or -1 if missing. - Cost(uint64) int64 - // Optionally, set stats object to track how policy is performing. - CollectMetrics(*Metrics) - // Clear zeroes out all counters and clears hashmaps. - Clear() - // MaxCost returns the current max cost of the cache policy. - MaxCost() int64 - // UpdateMaxCost updates the max cost of the cache policy. - UpdateMaxCost(int64) -} - -func newPolicy[V any](numCounters, maxCost int64) policy[V] { +func newPolicy[V any](numCounters, maxCost int64) *defaultPolicy[V] { return newDefaultPolicy[V](numCounters, maxCost) } diff --git a/store.go b/store.go index e2ca800c..dfcfaa8d 100644 --- a/store.go +++ b/store.go @@ -50,7 +50,7 @@ type store[V any] interface { // successful. Update(*Item[V]) (V, bool) // Cleanup removes items that have an expired TTL. - Cleanup(policy policy[V], onEvict func(item *Item[V])) + Cleanup(policy *defaultPolicy[V], onEvict func(item *Item[V])) // Clear clears all contents of the store. Clear(onEvict func(item *Item[V])) } @@ -103,7 +103,7 @@ func (sm *shardedMap[V]) Update(newItem *Item[V]) (V, bool) { return sm.shards[newItem.Key%numShards].Update(newItem) } -func (sm *shardedMap[V]) Cleanup(policy policy[V], onEvict func(item *Item[V])) { +func (sm *shardedMap[V]) Cleanup(policy *defaultPolicy[V], onEvict func(item *Item[V])) { sm.expiryMap.cleanup(sm, policy, onEvict) } diff --git a/ttl.go b/ttl.go index 08e77c16..ccb0d7cb 100644 --- a/ttl.go +++ b/ttl.go @@ -121,7 +121,7 @@ func (m *expirationMap[_]) del(key uint64, expiration time.Time) { // cleanup removes all the items in the bucket that was just completed. It deletes // those items from the store, and calls the onEvict function on those items. // This function is meant to be called periodically. -func (m *expirationMap[V]) cleanup(store store[V], policy policy[V], onEvict func(item *Item[V])) { +func (m *expirationMap[V]) cleanup(store store[V], policy *defaultPolicy[V], onEvict func(item *Item[V])) { if m == nil { return }