-
-
Notifications
You must be signed in to change notification settings - Fork 31
Improves Redis cache list retrieval performance #116
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
base: main
Are you sure you want to change the base?
Conversation
Optimizes list retrieval by using SortedSetRangeByScoreAsync instead of SortedSetRangeByRankAsync, utilizing timestamp-based scores for efficient expiration management. Handles legacy sets by migrating them to sorted sets when encountering WRONGTYPE exceptions, ensuring compatibility and preventing data loss during the transition. Logs trace information when a sorted set is empty during expiration setting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR optimizes Redis cache list retrieval performance by switching from rank-based to score-based queries, using timestamps as scores for efficient expiration management. It also adds backward compatibility by migrating legacy sets to sorted sets when WRONGTYPE errors occur.
Key Changes:
- Replaced
SortedSetRangeByRankAsyncwithSortedSetRangeByScoreAsyncfor improved query performance - Added exception handling to migrate legacy sets to sorted sets automatically
- Extracted migration logic into a separate reusable method
| return RedisValuesToCacheValue<T>(set); | ||
| if (!page.HasValue) | ||
| { | ||
| var set = await Database.SortedSetRangeByScoreAsync(key, _timeProvider.GetUtcNow().ToUnixTimeMilliseconds() + 1, flags: _options.ReadMode).AnyContext(); |
Copilot
AI
Oct 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The magic number '+1' added to the timestamp lacks explanation. Consider adding a comment explaining why this offset is necessary (e.g., to include items with scores equal to the current timestamp).
| else | ||
| { | ||
| long skip = (page.Value - 1) * pageSize; | ||
| var set = await Database.SortedSetRangeByScoreAsync(key, _timeProvider.GetUtcNow().ToUnixTimeMilliseconds() + 1, skip: skip, take: pageSize, flags: _options.ReadMode).AnyContext(); |
Copilot
AI
Oct 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The magic number '+1' added to the timestamp lacks explanation. Consider adding a comment explaining why this offset is necessary for consistency with the non-paginated query.
Updates the sorted set range query for retrieving cached lists to use Double.PositiveInfinity for the maximum score, ensuring all valid items are returned. This change enhances performance by avoiding unnecessary additions of 1 millisecond to the timestamp, streamlining the retrieval process and improving efficiency.
Optimizes list retrieval by using SortedSetRangeByScoreAsync instead of SortedSetRangeByRankAsync, utilizing timestamp-based scores for efficient expiration management.
Handles legacy sets by migrating them to sorted sets when encountering WRONGTYPE exceptions, ensuring compatibility and preventing data loss during the transition.
Logs trace information when a sorted set is empty during expiration setting.