diff --git a/cache.go b/cache.go index db88d2f..d76a71d 100644 --- a/cache.go +++ b/cache.go @@ -11,7 +11,7 @@ import ( ) type Item struct { - Object interface{} + Object any Expiration int64 } @@ -41,14 +41,14 @@ type cache struct { defaultExpiration time.Duration items map[string]Item mu sync.RWMutex - onEvicted func(string, interface{}) + onEvicted func(string, any) janitor *janitor } // Add an item to the cache, replacing any existing item. If the duration is 0 // (DefaultExpiration), the cache's default expiration time is used. If it is -1 // (NoExpiration), the item never expires. -func (c *cache) Set(k string, x interface{}, d time.Duration) { +func (c *cache) Set(k string, x any, d time.Duration) { // "Inlining" of set var e int64 if d == DefaultExpiration { @@ -67,7 +67,7 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) { c.mu.Unlock() } -func (c *cache) set(k string, x interface{}, d time.Duration) { +func (c *cache) set(k string, x any, d time.Duration) { var e int64 if d == DefaultExpiration { d = c.defaultExpiration @@ -83,13 +83,13 @@ func (c *cache) set(k string, x interface{}, d time.Duration) { // Add an item to the cache, replacing any existing item, using the default // expiration. -func (c *cache) SetDefault(k string, x interface{}) { +func (c *cache) SetDefault(k string, x any) { c.Set(k, x, DefaultExpiration) } // Add an item to the cache only if an item doesn't already exist for the given // key, or if the existing item has expired. Returns an error otherwise. -func (c *cache) Add(k string, x interface{}, d time.Duration) error { +func (c *cache) Add(k string, x any, d time.Duration) error { c.mu.Lock() _, found := c.get(k) if found { @@ -103,7 +103,7 @@ func (c *cache) Add(k string, x interface{}, d time.Duration) error { // Set a new value for the cache key only if it already exists, and the existing // item hasn't expired. Returns an error otherwise. -func (c *cache) Replace(k string, x interface{}, d time.Duration) error { +func (c *cache) Replace(k string, x any, d time.Duration) error { c.mu.Lock() _, found := c.get(k) if !found { @@ -117,7 +117,7 @@ func (c *cache) Replace(k string, x interface{}, d time.Duration) error { // Get an item from the cache. Returns the item or nil, and a bool indicating // whether the key was found. -func (c *cache) Get(k string) (interface{}, bool) { +func (c *cache) Get(k string) (any, bool) { c.mu.RLock() // "Inlining" of get and Expired item, found := c.items[k] @@ -139,7 +139,7 @@ func (c *cache) Get(k string) (interface{}, bool) { // It returns the item or nil, the expiration time if one is set (if the item // never expires a zero value for time.Time is returned), and a bool indicating // whether the key was found. -func (c *cache) GetWithExpiration(k string) (interface{}, time.Time, bool) { +func (c *cache) GetWithExpiration(k string) (any, time.Time, bool) { c.mu.RLock() // "Inlining" of get and Expired item, found := c.items[k] @@ -165,7 +165,7 @@ func (c *cache) GetWithExpiration(k string) (interface{}, time.Time, bool) { return item.Object, time.Time{}, true } -func (c *cache) get(k string) (interface{}, bool) { +func (c *cache) get(k string) (any, bool) { item, found := c.items[k] if !found { return nil, false @@ -911,7 +911,7 @@ func (c *cache) Delete(k string) { } } -func (c *cache) delete(k string) (interface{}, bool) { +func (c *cache) delete(k string) (any, bool) { if c.onEvicted != nil { if v, found := c.items[k]; found { delete(c.items, k) @@ -924,7 +924,7 @@ func (c *cache) delete(k string) (interface{}, bool) { type keyAndValue struct { key string - value interface{} + value any } // Delete all expired items from the cache. @@ -950,7 +950,7 @@ func (c *cache) DeleteExpired() { // Sets an (optional) function that is called with the key and value when an // item is evicted from the cache. (Including when it is deleted manually, but // not when it is overwritten.) Set to nil to disable. -func (c *cache) OnEvicted(f func(string, interface{})) { +func (c *cache) OnEvicted(f func(string, any)) { c.mu.Lock() c.onEvicted = f c.mu.Unlock() diff --git a/cache_test.go b/cache_test.go index de3e9d6..fc1adf6 100644 --- a/cache_test.go +++ b/cache_test.go @@ -1231,7 +1231,7 @@ func TestOnEvicted(t *testing.T) { t.Fatal("tc.onEvicted is not nil") } works := false - tc.OnEvicted(func(k string, v interface{}) { + tc.OnEvicted(func(k string, v any) { if k == "foo" && v.(int) == 3 { works = true } @@ -1460,7 +1460,7 @@ func BenchmarkRWMutexMapGet(b *testing.B) { func BenchmarkRWMutexInterfaceMapGetStruct(b *testing.B) { b.StopTimer() s := struct{ name string }{name: "foo"} - m := map[interface{}]string{ + m := map[any]string{ s: "bar", } mu := sync.RWMutex{} @@ -1474,7 +1474,7 @@ func BenchmarkRWMutexInterfaceMapGetStruct(b *testing.B) { func BenchmarkRWMutexInterfaceMapGetString(b *testing.B) { b.StopTimer() - m := map[interface{}]string{ + m := map[any]string{ "foo": "bar", } mu := sync.RWMutex{} diff --git a/sharded.go b/sharded.go index bcc0538..6a29eee 100644 --- a/sharded.go +++ b/sharded.go @@ -66,19 +66,19 @@ func (sc *shardedCache) bucket(k string) *cache { return sc.cs[djb33(sc.seed, k)%sc.m] } -func (sc *shardedCache) Set(k string, x interface{}, d time.Duration) { +func (sc *shardedCache) Set(k string, x any, d time.Duration) { sc.bucket(k).Set(k, x, d) } -func (sc *shardedCache) Add(k string, x interface{}, d time.Duration) error { +func (sc *shardedCache) Add(k string, x any, d time.Duration) error { return sc.bucket(k).Add(k, x, d) } -func (sc *shardedCache) Replace(k string, x interface{}, d time.Duration) error { +func (sc *shardedCache) Replace(k string, x any, d time.Duration) error { return sc.bucket(k).Replace(k, x, d) } -func (sc *shardedCache) Get(k string) (interface{}, bool) { +func (sc *shardedCache) Get(k string) (any, bool) { return sc.bucket(k).Get(k) }