Skip to content

feat: add LRU eviction and size cap to image cache#734

Merged
RUKAYAT-CODER merged 1 commit into
rinafcode:mainfrom
Sughter99:feat/image-cache-lru-eviction
Jun 28, 2026
Merged

feat: add LRU eviction and size cap to image cache#734
RUKAYAT-CODER merged 1 commit into
rinafcode:mainfrom
Sughter99:feat/image-cache-lru-eviction

Conversation

@Sughter99

Copy link
Copy Markdown
Contributor

Closes #677

What this PR does

Adds LRU eviction and a hard 100 MB size cap to the image cache,
preventing unbounded disk usage during heavy course catalog browsing.

Changes

src/utils/imageCache.ts

  • Added EVICTION_THRESHOLD_BYTES = 80 MB (80% of MAX_CACHE_SIZE_BYTES)
  • Updated evictLRU() to accept targetBytes parameter (defaults to 70% of max)
  • Added maybeEvict() — called after every prefetch write, triggers LRU
    eviction when cache reaches 80 MB threshold
  • Added clearNonCritical() — removes all entries without clearing the
    metadata structure, called on critical memory pressure
  • Added getCacheSizeBytes() — exposes current cache size as a metric
  • Added getEvictionThresholdBytes() — exposes threshold for testing

src/services/memoryPressureService.ts

  • Updated handleHighMemoryPressure() to call clearNonCritical() instead
    of clearCache() to preserve metadata structure during pressure events

src/tests/utils/imageCacheLRU.test.ts (new)

  • 6 tests covering:
    • Eviction threshold is 80% of 100 MB
    • getCacheSizeBytes returns 0 on empty cache
    • Eviction fires when 81st MB is pushed past 80 MB threshold
    • clearNonCritical removes all entries and resets size to 0
    • getCacheSizeBytes returns correct total after prefetch
    • Cache never exceeds 100 MB after many prefetches

Acceptance Criteria

  • Image cache does not exceed 100 MB on disk
  • LRU eviction fires when cache reaches 80 MB
  • Critical memory pressure clears all non-pinned cached images
  • Unit test confirms eviction at 80% threshold
  • 6 tests passing

@drips-wave

drips-wave Bot commented Jun 28, 2026

Copy link
Copy Markdown

@Sughter99 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@RUKAYAT-CODER

Copy link
Copy Markdown
Contributor

Thank you for contributing to the project.

@RUKAYAT-CODER RUKAYAT-CODER merged commit 944f498 into rinafcode:main Jun 28, 2026
1 of 14 checks passed
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.

[Performance] Image cache unbounded — fills device storage during heavy course catalog browsing

2 participants