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(database/gredis): add Scan method for incremental key retrieval #3451

Merged
merged 20 commits into from
Apr 11, 2024

Conversation

phuonganhniie
Copy link
Contributor

@phuonganhniie phuonganhniie commented Apr 3, 2024

Overview

This pull request introduces the Scan method to the GroupGeneric struct in gredis package, providing an efficient and non-blocking approach to iterate over keys that match a given pattern in Redis. This method is designed to handle large datasets more gracefully than the Keys command, offering incremental retrieval without the risk of blocking the Redis server.

Changes

  • Scan Method: Implements the Redis SCAN command within the GroupGeneric struct. It accepts a cursor, pattern, and count, returning a slice of keys matching the pattern and the next cursor position for continued scanning.

Why It's Needed

The SCAN command is essential for applications dealing with large data sets in Redis, where using the Keys command might lead to performance bottlenecks or blocking the server. By providing a Scan method, it easier and safer to get keys little by little, improving overall application responsiveness.

Available since Redis version: 2.8.0

Usage Example

Below is a simple example demonstrating how to use the new Scan method:

var cursor uint64 = 0
var allKeys []string
var count int = 100

for {
    keys, nextCursor, err := redis.GroupGeneric().Scan(ctx, cursor, "*pattern*", count)
    if err != nil {
        log.Fatalf("Scan failed: %v", err)
    }
    allKeys = append(allKeys, keys...)
    if nextCursor == 0 {
        break
    }
    cursor = nextCursor
}

fmt.Println("Retrieved keys:", allKeys)

Testing

Includes comprehensive unit tests covering:

  • Successful key retrieval with various patterns.
  • Proper handling of negative cursor and count values.
  • Edge cases, such as empty patterns, pattern with no matches.

I'd love to hear your thoughts and feedback on this! Looking forward to your review.

@phuonganhniie
Copy link
Contributor Author

@gqcn Hi John, I'm looking forward to your review. If you need any information, pls don't hesitate contact me.

@houseme houseme requested review from gqcn and hailaz April 7, 2024 02:13
contrib/nosql/redis/redis_group_generic.go Outdated Show resolved Hide resolved
@hailaz hailaz self-requested a review April 7, 2024 02:24
@gqcn gqcn merged commit 4c6b146 into gogf:master Apr 11, 2024
23 checks passed
@gqcn gqcn changed the title feat: add Scan method for incremental key retrieval in gredis feat(database/gredis): add Scan method for incremental key retrieval Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants