feat(tinykv): add Delete method

This commit is contained in:
Alexis Couvreur
2022-11-04 16:01:32 +00:00
parent 8096a4e87e
commit eb83d39652
2 changed files with 27 additions and 0 deletions

View File

@@ -57,6 +57,7 @@ type entry[T any] struct {
// KV is a registry for values (like/is a concurrent map) with timeout and sliding timeout // KV is a registry for values (like/is a concurrent map) with timeout and sliding timeout
type KV[T any] interface { type KV[T any] interface {
Delete(k string)
Get(k string) (v T, ok bool) Get(k string) (v T, ok bool)
Keys() (keys []string) Keys() (keys []string)
Values() (values []T) Values() (values []T)
@@ -104,6 +105,13 @@ func (kv *store[T]) Stop() {
kv.stopOnce.Do(func() { close(kv.stop) }) kv.stopOnce.Do(func() { close(kv.stop) })
} }
// Delete deletes an entry
func (kv *store[T]) Delete(k string) {
kv.mx.Lock()
defer kv.mx.Unlock()
delete(kv.kv, k)
}
func (kv *store[T]) Get(k string) (T, bool) { func (kv *store[T]) Get(k string) (T, bool) {
var zero T var zero T
kv.mx.Lock() kv.mx.Lock()

View File

@@ -201,6 +201,25 @@ func Test03(t *testing.T) {
assert.WithinDuration(putAt, putAt.Add(<-elapsed), time.Millisecond*60) assert.WithinDuration(putAt, putAt.Add(<-elapsed), time.Millisecond*60)
} }
func Test04(t *testing.T) {
assert := assert.New(t)
kv := New(
time.Millisecond*10,
func(k string, v interface{}) {
t.Fatal(k, v)
})
err := kv.Put("1", 1, time.Millisecond*10000)
assert.NoError(err)
<-time.After(time.Millisecond * 50)
kv.Delete("1")
kv.Delete("1")
<-time.After(time.Millisecond * 100)
_, ok := kv.Get("1")
assert.False(ok)
}
func Test05(t *testing.T) { func Test05(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
N := 10000 N := 10000