Skip to content

Commit

Permalink
Remove the policy interface (#393)
Browse files Browse the repository at this point in the history
Remove the policy interface, as only one policy is being maintained.
  • Loading branch information
xiaoqingwanga authored Oct 14, 2024
1 parent e4edd4c commit f4f0d7e
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 36 deletions.
2 changes: 1 addition & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 1 addition & 32 deletions policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
}
Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion ttl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit f4f0d7e

Please sign in to comment.