Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(redis): Add support for Keys() to Redis #164

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,46 @@ 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

tctx, cancel := context.WithTimeout(context.Background(), c.timeOut)
defer cancel()

for {
var batch []string

// Check for Timeout here (this is better then using tctx.Done)
if err := tctx.Err(); err != nil {
return nil, err
}

// Scan returns the next batch of keys
if batch, cursor, err = c.c.Scan(tctx, cursor, "*", 10).Result(); err != nil {
return nil, err
}

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

// Break if the cursor is 0
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
Loading