Skip to content

Conversation

@niemyjski
Copy link
Member

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.

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.
@niemyjski niemyjski requested review from Copilot and ejsmith October 21, 2025 21:07
@niemyjski niemyjski self-assigned this Oct 21, 2025
Copy link

Copilot AI left a 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 SortedSetRangeByRankAsync with SortedSetRangeByScoreAsync for 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();
Copy link

Copilot AI Oct 21, 2025

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).

Copilot uses AI. Check for mistakes.
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();
Copy link

Copilot AI Oct 21, 2025

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.

Copilot uses AI. Check for mistakes.
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.
Comment on lines +133 to +136
catch (Exception ex)
{
_logger.LogError(ex, "Unable to delete {HashSlot} keys ({Keys}): {Message}", hashSlotGroup.Key, hashSlotKeys, ex.Message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants