Skip to content

Commit

Permalink
Update go 1.23 (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshineplan authored Aug 14, 2024
1 parent 985ae5c commit 0edcc43
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 517 deletions.
4 changes: 2 additions & 2 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ func (c *Cache[Key, Value]) Swap(key Key, value Value) (previous Value, loaded b
return
}

// Empty deletes all values in cache.
func (c *Cache[Key, Value]) Empty() {
// Clear deletes all values in cache.
func (c *Cache[Key, Value]) Clear() {
c.m.Range(func(k Key, i *item[Value]) bool {
c.m.Delete(k)
i.Lock()
Expand Down
4 changes: 2 additions & 2 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestEmpty(t *testing.T) {
t.Error("expected ok; got not")
}
}
cache.Empty()
cache.Clear()
for _, i := range []string{"a", "b", "c"} {
if _, ok := cache.Get(i); ok {
t.Error("expected not ok; got ok")
Expand All @@ -49,7 +49,7 @@ func TestEmpty(t *testing.T) {

func TestRenew(t *testing.T) {
cache := New[string, string](true)
defer cache.Empty()
defer cache.Clear()
expire := make(chan struct{})
cache.Set("renew", "old", 2*time.Second, func() (string, error) {
defer func() { close(expire) }()
Expand Down
15 changes: 12 additions & 3 deletions cache/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,22 @@ func (m *Map[Key, Value]) Store(key Key, value Value) {
m.m.Store(key, value)
}

// Clear deletes all the entries, resulting in an empty Map.
func (m *Map[Key, Value]) Clear() {
m.m.Clear()
}

// LoadOrStore returns the existing value for the key if present.
// Otherwise, it stores and returns the given value.
// The loaded result is true if the value was loaded, false if stored.
func (m *Map[Key, Value]) LoadOrStore(key Key, value Value) (actual Value, loaded bool) {
var v any
if v, loaded = m.m.LoadOrStore(key, value); v != nil {
actual = v.(Value)
if v, loaded = m.m.LoadOrStore(key, value); loaded {
if v != nil {
actual = v.(Value)
}
} else {
actual = value
}
return
}
Expand Down Expand Up @@ -92,10 +101,10 @@ func (m *Map[Key, Value]) CompareAndDelete(key Key, old Value) (deleted bool) {
func (m *Map[Key, Value]) Range(f func(Key, Value) bool) {
m.m.Range(func(key, value any) bool {
var k Key
var v Value
if key != nil {
k = key.(Key)
}
var v Value
if value != nil {
v = value.(Value)
}
Expand Down
274 changes: 0 additions & 274 deletions cache/map_reference_test.go

This file was deleted.

Loading

0 comments on commit 0edcc43

Please sign in to comment.