mirror of
https://github.com/sablierapp/sablier.git
synced 2025-12-24 06:28:21 +01:00
feat(tinykv): add Delete method
This commit is contained in:
@@ -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()
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user