Skip to content

Commit

Permalink
Merge pull request #1052 from gofiber/redis-keys
Browse files Browse the repository at this point in the history
Add support for Keys() to Redis Driver
  • Loading branch information
ReneWerner87 authored Nov 5, 2023
2 parents 7638e00 + 50bea20 commit d596831
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test-redis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ jobs:
slave3-port: 7005
sleep-duration: 10

- name: Wait for Redis to Start
run: sleep 15

- name: Install Go
uses: actions/setup-go@v4
with:
Expand Down
1 change: 1 addition & 0 deletions redis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (s *Storage) Delete(key string) error
func (s *Storage) Reset() error
func (s *Storage) Close() error
func (s *Storage) Conn() redis.UniversalClient
func (s *Storage) Keys() ([][]byte, error)
```
### Installation
Redis is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet:
Expand Down
31 changes: 29 additions & 2 deletions redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ func New(config ...Config) *Storage {
}
}

// ...

// Get value by key
func (s *Storage) Get(key string) ([]byte, error) {
if len(key) <= 0 {
Expand Down Expand Up @@ -118,3 +116,32 @@ func (s *Storage) Close() error {
func (s *Storage) Conn() redis.UniversalClient {
return s.db
}

// Return all the keys
func (s *Storage) Keys() ([][]byte, error) {
var keys [][]byte
var cursor uint64
var err error

for {
var batch []string

if batch, cursor, err = s.db.Scan(context.Background(), cursor, "*", 10).Result(); err != nil {
return nil, err
}

for _, key := range batch {
keys = append(keys, []byte(key))
}

if cursor == 0 {
break
}
}

if len(keys) == 0 {
return nil, nil
}

return keys, nil
}
106 changes: 97 additions & 9 deletions redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import (
"github.com/stretchr/testify/require"
)

var testStore = New(Config{
Reset: true,
})

func Test_Redis_Set(t *testing.T) {
var (
testStore = New(Config{
Reset: true,
})
key = "john"
val = []byte("doe")
)
Expand All @@ -25,6 +24,9 @@ func Test_Redis_Set(t *testing.T) {

func Test_Redis_Set_Override(t *testing.T) {
var (
testStore = New(Config{
Reset: true,
})
key = "john"
val = []byte("doe")
)
Expand All @@ -34,10 +36,17 @@ func Test_Redis_Set_Override(t *testing.T) {

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_Redis_Get(t *testing.T) {
var (
testStore = New(Config{
Reset: true,
})
key = "john"
val = []byte("doe")
)
Expand All @@ -48,10 +57,17 @@ func Test_Redis_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_Redis_Set_Expiration(t *testing.T) {
func Test_Redis_Expiration(t *testing.T) {
var (
testStore = New(Config{
Reset: true,
})
key = "john"
val = []byte("doe")
exp = 1 * time.Second
Expand All @@ -61,24 +77,30 @@ func Test_Redis_Set_Expiration(t *testing.T) {
require.NoError(t, err)

time.Sleep(1100 * time.Millisecond)
}

func Test_Redis_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.Nil(t, keys)
}

func Test_Redis_Get_NotExist(t *testing.T) {
testStore := New(Config{
Reset: true,
})
result, err := testStore.Get("notexist")
require.NoError(t, err)
require.Zero(t, len(result))
}

func Test_Redis_Delete(t *testing.T) {
var (
testStore = New(Config{
Reset: true,
})
key = "john"
val = []byte("doe")
)
Expand All @@ -92,9 +114,16 @@ func Test_Redis_Delete(t *testing.T) {
result, err := testStore.Get(key)
require.NoError(t, err)
require.Zero(t, len(result))

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

func Test_Redis_Reset(t *testing.T) {
testStore := New(Config{
Reset: true,
})
val := []byte("doe")

err := testStore.Set("john1", val, 0)
Expand All @@ -103,6 +132,10 @@ func Test_Redis_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 @@ -113,13 +146,23 @@ func Test_Redis_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.Nil(t, keys)
}

func Test_Redis_Close(t *testing.T) {
testStore := New(Config{
Reset: true,
})
require.Nil(t, testStore.Close())
}

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

Expand Down Expand Up @@ -184,6 +227,10 @@ func Test_Redis_Initalize_WithURL_TLS(t *testing.T) {
err = testStoreUrl.Delete(key)
require.NoError(t, err)

keys, err := testStoreUrl.Keys()
require.NoError(t, err)
require.Nil(t, keys)

require.Nil(t, testStoreUrl.Close())
}

Expand Down Expand Up @@ -226,6 +273,10 @@ func Test_Redis_Initalize_WithURL_TLS_Verify(t *testing.T) {
err = testStoreUrl.Delete(key)
require.NoError(t, err)

keys, err := testStoreUrl.Keys()
require.NoError(t, err)
require.Nil(t, keys)

require.Nil(t, testStoreUrl.Close())
}

Expand All @@ -249,6 +300,10 @@ func Test_Redis_Initalize_With_Secure_URL(t *testing.T) {
err = testStoreUrl.Delete(key)
require.NoError(t, err)

keys, err := testStoreUrl.Keys()
require.NoError(t, err)
require.Nil(t, keys)

require.Nil(t, testStoreUrl.Close())
}

Expand All @@ -273,6 +328,10 @@ func Test_Redis_Universal_Addrs(t *testing.T) {
err = testStoreUniversal.Delete(key)
require.NoError(t, err)

keys, err := testStoreUniversal.Keys()
require.NoError(t, err)
require.Nil(t, keys)

require.Nil(t, testStoreUniversal.Close())
}

Expand All @@ -299,6 +358,10 @@ func Test_Redis_Universal_With_URL_Undefined(t *testing.T) {
err = testStoreUniversal.Delete(key)
require.NoError(t, err)

keys, err := testStoreUniversal.Keys()
require.NoError(t, err)
require.Nil(t, keys)

require.Nil(t, testStoreUniversal.Close())
}

Expand All @@ -325,6 +388,10 @@ func Test_Redis_Universal_With_URL_Defined(t *testing.T) {
err = testStoreUniversal.Delete(key)
require.NoError(t, err)

keys, err := testStoreUniversal.Keys()
require.NoError(t, err)
require.Nil(t, keys)

require.Nil(t, testStoreUniversal.Close())
}

Expand Down Expand Up @@ -352,6 +419,10 @@ func Test_Redis_Universal_With_HostPort(t *testing.T) {
err = testStoreUniversal.Delete(key)
require.NoError(t, err)

keys, err := testStoreUniversal.Keys()
require.NoError(t, err)
require.Nil(t, keys)

require.Nil(t, testStoreUniversal.Close())
}

Expand Down Expand Up @@ -380,6 +451,10 @@ func Test_Redis_Universal_With_HostPort_And_URL(t *testing.T) {
err = testStoreUniversal.Delete(key)
require.NoError(t, err)

keys, err := testStoreUniversal.Keys()
require.NoError(t, err)
require.Nil(t, keys)

require.Nil(t, testStoreUniversal.Close())
}

Expand Down Expand Up @@ -410,10 +485,17 @@ func Test_Redis_Cluster(t *testing.T) {
err = testStoreUniversal.Delete(key)
require.NoError(t, err)

keys, err := testStoreUniversal.Keys()
require.NoError(t, err)
require.Nil(t, keys)

require.Nil(t, testStoreUniversal.Close())
}

func Benchmark_Redis_Set(b *testing.B) {
testStore := New(Config{
Reset: true,
})
b.ReportAllocs()
b.ResetTimer()

Expand All @@ -426,6 +508,9 @@ func Benchmark_Redis_Set(b *testing.B) {
}

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

Expand All @@ -440,6 +525,9 @@ func Benchmark_Redis_Get(b *testing.B) {
}

func Benchmark_Redis_SetAndDelete(b *testing.B) {
testStore := New(Config{
Reset: true,
})
b.ReportAllocs()
b.ResetTimer()

Expand Down

0 comments on commit d596831

Please sign in to comment.