Skip to content

Commit

Permalink
Add support for Keys() to Memory Driver
Browse files Browse the repository at this point in the history
  • Loading branch information
gaby committed Oct 30, 2023
1 parent ddac78a commit 7921dc7
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 26 deletions.
1 change: 1 addition & 0 deletions memory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (s *Storage) Delete(key string) error
func (s *Storage) Reset() error
func (s *Storage) Close() error
func (s *Storage) Conn() map[string]entry
func (s *Storage) Keys() ([][]byte, error)
```

### Installation
Expand Down
6 changes: 1 addition & 5 deletions memory/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ module github.com/gofiber/storage/memory/v2

go 1.19

require (
github.com/gofiber/utils/v2 v2.0.0-beta.3
github.com/stretchr/testify v1.8.4
)
require github.com/stretchr/testify v1.8.4

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 0 additions & 4 deletions memory/go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofiber/utils/v2 v2.0.0-beta.3 h1:pfOhUDDVjBJpkWv6C5jaDyYLvpui7zQ97zpyFFsUOKw=
github.com/gofiber/utils/v2 v2.0.0-beta.3/go.mod h1:jsl17+MsKfwJjM3ONCE9Rzji/j8XNbwjhUVTjzgfDCo=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand Down
17 changes: 17 additions & 0 deletions memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,20 @@ func (s *Storage) Conn() map[string]entry {
defer s.mux.RUnlock()
return s.db
}

// Return all the keys
func (s *Storage) Keys() ([][]byte, error) {
s.mux.RLock()
defer s.mux.RUnlock()

if len(s.db) == 0 {
return nil, nil
}

keys := make([][]byte, 0, len(s.db))
for key := range s.db {
keys = append(keys, []byte(key))
}

return keys, nil
}
76 changes: 59 additions & 17 deletions memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,44 @@ import (
"github.com/stretchr/testify/require"
)

var testStore = New()

func Test_Storage_Memory_Set(t *testing.T) {
var (
key = "john"
val = []byte("doe")
testStore = New()
key = "john"
val = []byte("doe")
)

err := testStore.Set(key, val, 0)
require.NoError(t, err)

keys, err := testStore.Keys()
require.NoError(t, err)
require.Len(t, keys, 1)
}

func Test_Storage_Memory_Set_Override(t *testing.T) {
var (
key = "john"
val = []byte("doe")
testStore = New()
key = "john"
val = []byte("doe")
)

err := testStore.Set(key, val, 0)
require.NoError(t, err)

err = testStore.Set(key, val, 0)
require.NoError(t, err)

keys, err := testStore.Keys()
require.NoError(t, err)
require.Len(t, keys, 1)
}

func Test_Storage_Memory_Get(t *testing.T) {
var (
key = "john"
val = []byte("doe")
testStore = New()
key = "john"
val = []byte("doe")
)

err := testStore.Set(key, val, 0)
Expand All @@ -44,53 +53,73 @@ func Test_Storage_Memory_Get(t *testing.T) {
result, err := testStore.Get(key)
require.NoError(t, err)
require.Equal(t, val, result)

keys, err := testStore.Keys()
require.NoError(t, err)
require.Len(t, keys, 1)
}

func Test_Storage_Memory_Set_Expiration(t *testing.T) {
var (
key = "john"
val = []byte("doe")
exp = 1 * time.Second
testStore = New()
key = "john"
val = []byte("doe")
exp = 1 * time.Second
)

err := testStore.Set(key, val, exp)
require.NoError(t, err)

time.Sleep(1100 * time.Millisecond)
}

func Test_Storage_Memory_Get_Expired(t *testing.T) {
key := "john"

result, err := testStore.Get(key)
require.NoError(t, err)
require.Zero(t, len(result))

keys, err := testStore.Keys()
require.NoError(t, err)
require.Zero(t, keys)
}

func Test_Storage_Memory_Get_NotExist(t *testing.T) {
testStore := New()
result, err := testStore.Get("notexist")
require.NoError(t, err)
require.Zero(t, len(result))

keys, err := testStore.Keys()
require.NoError(t, err)
require.Zero(t, keys)
}

func Test_Storage_Memory_Delete(t *testing.T) {
var (
key = "john"
val = []byte("doe")
testStore = New()
key = "john"
val = []byte("doe")
)

err := testStore.Set(key, val, 0)
require.NoError(t, err)

keys, err := testStore.Keys()
require.NoError(t, err)
require.Len(t, keys, 1)

err = testStore.Delete(key)
require.NoError(t, err)

result, err := testStore.Get(key)
require.NoError(t, err)
require.Zero(t, len(result))

keys, err = testStore.Keys()
require.NoError(t, err)
require.Zero(t, keys)
}

func Test_Storage_Memory_Reset(t *testing.T) {
testStore := New()
val := []byte("doe")

err := testStore.Set("john1", val, 0)
Expand All @@ -99,6 +128,10 @@ func Test_Storage_Memory_Reset(t *testing.T) {
err = testStore.Set("john2", val, 0)
require.NoError(t, err)

keys, err := testStore.Keys()
require.NoError(t, err)
require.Len(t, keys, 2)

err = testStore.Reset()
require.NoError(t, err)

Expand All @@ -109,17 +142,24 @@ func Test_Storage_Memory_Reset(t *testing.T) {
result, err = testStore.Get("john2")
require.NoError(t, err)
require.Zero(t, len(result))

keys, err = testStore.Keys()
require.NoError(t, err)
require.Zero(t, keys)
}

func Test_Storage_Memory_Close(t *testing.T) {
testStore := New()
require.Nil(t, testStore.Close())
}

func Test_Storage_Memory_Conn(t *testing.T) {
testStore := New()
require.True(t, testStore.Conn() != nil)
}

func Benchmark_Memory_Set(b *testing.B) {
testStore := New()
b.ReportAllocs()
b.ResetTimer()

Expand All @@ -132,6 +172,7 @@ func Benchmark_Memory_Set(b *testing.B) {
}

func Benchmark_Memory_Get(b *testing.B) {
testStore := New()
err := testStore.Set("john", []byte("doe"), 0)
require.NoError(b, err)

Expand All @@ -146,6 +187,7 @@ func Benchmark_Memory_Get(b *testing.B) {
}

func Benchmark_Memory_SetAndDelete(b *testing.B) {
testStore := New()
b.ReportAllocs()
b.ResetTimer()

Expand Down

0 comments on commit 7921dc7

Please sign in to comment.