Skip to content

Eviction sorts by highest access count (backwards for LRU) #105

@lilith

Description

@lilith

Summary

The eviction logic in CleanupManager.cs and Shard.cs appears to sort deletion candidates by descending access count, which would delete the most accessed items first - the opposite of LRU behavior.

Code Locations

Shard.cs:107-110:

.OrderBy(r => (byte)r.Flags)
.Select(r => new Tuple<CacheDatabaseRecord, ushort>(r, getUsageCount(r.AccessCountKey)))
.OrderByDescending(t => t.Item2)  // Highest access count first
.Select(t => (ICacheDatabaseRecord) t.Item1)

CleanupManager.cs:184-189:

(await Database.GetDeletionCandidates(shard, deletionCutoff, creationCutoff, Options.CleanupSelectBatchSize, AccessCounter.Get))
.Select(r => // I'm confused, GetDeletionCandidates already does this sort...
    new Tuple<ushort, ICacheDatabaseRecord>(
        AccessCounter.Get(r.AccessCountKey), r))
.OrderByDescending(r => r.Item1)  // Highest access count first (again)
.Select(r => r.Item2).ToArray();

Note the existing comment "I'm confused, GetDeletionCandidates already does this sort..." suggesting this was noticed before.

Expected Behavior

For LRU eviction, items with the lowest access count should be deleted first. The sort should be OrderBy (ascending), not OrderByDescending.

Questions

  1. Is this intentional for some reason I'm not seeing?
  2. Should both sorts be changed to ascending?
  3. Why is the sort duplicated in both locations?

🤖 Generated with Claude Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions