Skip to content

Commit

Permalink
Add clear to cache with mutex lock when clearing (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
bir authored Sep 13, 2023
1 parent f8cfc2b commit e730e54
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
9 changes: 8 additions & 1 deletion cache/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Basic[K comparable, V any] struct {
// NewBasic creates a new non-thread safe cache.
func NewBasic[K comparable, V any]() *Basic[K, V] {
return &Basic[K, V]{
items: make(map[K]V, 0),
items: make(map[K]V),
RWMutex: &sync.RWMutex{},
}
}
Expand Down Expand Up @@ -61,3 +61,10 @@ func (c *Basic[K, V]) Delete(key K) {

delete(c.items, key)
}

func (c *Basic[K, V]) Clear() {
c.Lock()
defer c.Unlock()

c.items = make(map[K]V)
}
22 changes: 15 additions & 7 deletions cache/basic_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package cache_test

import (
crand "crypto/rand"
"encoding/base32"
"fmt"
"math/rand"
"sort"
"sync"
"testing"

"github.com/bir/iken/cache"
"github.com/stretchr/testify/assert"

"github.com/bir/iken/cache"
)

func ExampleBasic() {
Expand All @@ -36,7 +38,7 @@ func ExampleBasic() {

func randString(length int) string {
randBytes := make([]byte, length)
_, err := rand.Read(randBytes)
_, err := crand.Read(randBytes)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -96,19 +98,25 @@ func TestCache(t *testing.T) {
assert.Equal(t, []string{"b"}, kk)
}

type foo struct {
c cache.Cache[int, string]
}

func TestMultiThread(t *testing.T) {
c := cache.NewBasic[int, string]()
f := foo{}
f.c = cache.NewBasic[int, string]()
var wg sync.WaitGroup
for i := int64(0); i < 100; i++ {
for i := int64(0); i < 1000; i++ {
wg.Add(1)
go func(i int64) {
defer wg.Done()
f.c.Clear()
m := rand.New(rand.NewSource(i))
for n := 0; n < 10000; n++ {
for n := 0; n < 1000; n++ {
key := m.Intn(100)
value := randString(10)
c.Set(key, value)
c.Get(key)
f.c.Set(key, value)
f.c.Get(key)
}
}(i)
}
Expand Down
2 changes: 2 additions & 0 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ type Cache[K comparable, V any] interface {
Delete(key K)
// Keys returns existing keys, the order is indeterminate.
Keys() []K
// Clear resets the cache.
Clear()
}
4 changes: 4 additions & 0 deletions cache/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ func (c *NoOp[K, _]) Keys() []K {
// Delete no-op.
func (c *NoOp[K, V]) Delete(_ K) {
}

// Clear no-op.
func (c *NoOp[K, V]) Clear() {
}
5 changes: 4 additions & 1 deletion cache/noop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package cache_test
import (
"testing"

"github.com/bir/iken/cache"
"github.com/stretchr/testify/assert"

"github.com/bir/iken/cache"
)

// Type assertion
Expand Down Expand Up @@ -53,4 +54,6 @@ func TestNoOpCache(t *testing.T) {

kk = c.Keys()
assert.Equal(t, 0, len(kk))

c.Clear()
}

0 comments on commit e730e54

Please sign in to comment.