Skip to content

Commit

Permalink
feat(redis): Add support for Keys() to Redis
Browse files Browse the repository at this point in the history
See https://pkg.go.dev/github.com/redis/go-redis/v9#Client.Scan

Related to #9

Signed-off-by: Boris Glimcher <Boris.Glimcher@emc.com>
  • Loading branch information
glimchb committed Dec 28, 2023
1 parent 0b9cc3b commit 16c0a34
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,35 @@ func (c Client) Close() error {
return c.c.Close()
}

// Return all the keys
func (c Client) Keys() ([]string, error) {
var keys []string
var cursor uint64
var err error

for {
var batch []string

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

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

if cursor == 0 {
break
}
}

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

return keys, nil
}

// Options are the options for the Redis client.
type Options struct {
// Address of the Redis server, including the port.
Expand Down

0 comments on commit 16c0a34

Please sign in to comment.