gofmt -w -r 'interface{} -> any'

This commit is contained in:
googlc 2022-11-28 02:53:38 +08:00
parent 46f4078530
commit 71870005af
3 changed files with 20 additions and 20 deletions

View File

@ -11,7 +11,7 @@ import (
) )
type Item struct { type Item struct {
Object interface{} Object any
Expiration int64 Expiration int64
} }
@ -41,14 +41,14 @@ type cache struct {
defaultExpiration time.Duration defaultExpiration time.Duration
items map[string]Item items map[string]Item
mu sync.RWMutex mu sync.RWMutex
onEvicted func(string, interface{}) onEvicted func(string, any)
janitor *janitor janitor *janitor
} }
// Add an item to the cache, replacing any existing item. If the duration is 0 // 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 // (DefaultExpiration), the cache's default expiration time is used. If it is -1
// (NoExpiration), the item never expires. // (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 // "Inlining" of set
var e int64 var e int64
if d == DefaultExpiration { if d == DefaultExpiration {
@ -67,7 +67,7 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) {
c.mu.Unlock() 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 var e int64
if d == DefaultExpiration { if d == DefaultExpiration {
d = c.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 // Add an item to the cache, replacing any existing item, using the default
// expiration. // expiration.
func (c *cache) SetDefault(k string, x interface{}) { func (c *cache) SetDefault(k string, x any) {
c.Set(k, x, DefaultExpiration) c.Set(k, x, DefaultExpiration)
} }
// Add an item to the cache only if an item doesn't already exist for the given // 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. // 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() c.mu.Lock()
_, found := c.get(k) _, found := c.get(k)
if found { 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 // Set a new value for the cache key only if it already exists, and the existing
// item hasn't expired. Returns an error otherwise. // 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() c.mu.Lock()
_, found := c.get(k) _, found := c.get(k)
if !found { 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 // Get an item from the cache. Returns the item or nil, and a bool indicating
// whether the key was found. // whether the key was found.
func (c *cache) Get(k string) (interface{}, bool) { func (c *cache) Get(k string) (any, bool) {
c.mu.RLock() c.mu.RLock()
// "Inlining" of get and Expired // "Inlining" of get and Expired
item, found := c.items[k] 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 // 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 // never expires a zero value for time.Time is returned), and a bool indicating
// whether the key was found. // 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() c.mu.RLock()
// "Inlining" of get and Expired // "Inlining" of get and Expired
item, found := c.items[k] 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 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] item, found := c.items[k]
if !found { if !found {
return nil, false 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 c.onEvicted != nil {
if v, found := c.items[k]; found { if v, found := c.items[k]; found {
delete(c.items, k) delete(c.items, k)
@ -924,7 +924,7 @@ func (c *cache) delete(k string) (interface{}, bool) {
type keyAndValue struct { type keyAndValue struct {
key string key string
value interface{} value any
} }
// Delete all expired items from the cache. // 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 // 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 // item is evicted from the cache. (Including when it is deleted manually, but
// not when it is overwritten.) Set to nil to disable. // 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.mu.Lock()
c.onEvicted = f c.onEvicted = f
c.mu.Unlock() c.mu.Unlock()

View File

@ -1231,7 +1231,7 @@ func TestOnEvicted(t *testing.T) {
t.Fatal("tc.onEvicted is not nil") t.Fatal("tc.onEvicted is not nil")
} }
works := false works := false
tc.OnEvicted(func(k string, v interface{}) { tc.OnEvicted(func(k string, v any) {
if k == "foo" && v.(int) == 3 { if k == "foo" && v.(int) == 3 {
works = true works = true
} }
@ -1460,7 +1460,7 @@ func BenchmarkRWMutexMapGet(b *testing.B) {
func BenchmarkRWMutexInterfaceMapGetStruct(b *testing.B) { func BenchmarkRWMutexInterfaceMapGetStruct(b *testing.B) {
b.StopTimer() b.StopTimer()
s := struct{ name string }{name: "foo"} s := struct{ name string }{name: "foo"}
m := map[interface{}]string{ m := map[any]string{
s: "bar", s: "bar",
} }
mu := sync.RWMutex{} mu := sync.RWMutex{}
@ -1474,7 +1474,7 @@ func BenchmarkRWMutexInterfaceMapGetStruct(b *testing.B) {
func BenchmarkRWMutexInterfaceMapGetString(b *testing.B) { func BenchmarkRWMutexInterfaceMapGetString(b *testing.B) {
b.StopTimer() b.StopTimer()
m := map[interface{}]string{ m := map[any]string{
"foo": "bar", "foo": "bar",
} }
mu := sync.RWMutex{} mu := sync.RWMutex{}

View File

@ -66,19 +66,19 @@ func (sc *shardedCache) bucket(k string) *cache {
return sc.cs[djb33(sc.seed, k)%sc.m] 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) 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) 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) 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) return sc.bucket(k).Get(k)
} }