Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the policy interface #393

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading