Skip to content

perf(core): implement multi-level LRU caching for expensive computations and data retrieval (fixes #48) - #54

Merged
Yuvraj-Sarathe merged 2 commits into
Omnikon-Org:mainfrom
Diwakar-odds:perf/issue-48-multi-level-caching
Jul 23, 2026
Merged

perf(core): implement multi-level LRU caching for expensive computations and data retrieval (fixes #48)#54
Yuvraj-Sarathe merged 2 commits into
Omnikon-Org:mainfrom
Diwakar-odds:perf/issue-48-multi-level-caching

Conversation

@Diwakar-odds

Copy link
Copy Markdown
Contributor

Summary

Implemented a multi-level LRU (Least Recently Used) caching mechanism with TTL support to optimize expensive computations and data retrieval, specifically targeting the high-latency AI model list fetches.

Motivation

Closes #48

Performance profiling revealed that repeated data fetching for remote resources (like HF models and OpenRouter models) was a primary bottleneck. Implementing this caching layer optimizes these hot paths, provides instant response times after the initial fetch, and reduces unnecessary load on primary data endpoints.

Changes

  • Cache Utility (src/utils/cache.ts):
    • Implemented generic ICache<T> interface.
    • Implemented LRUCache<T> using Map to maintain LRU eviction semantics and built-in TTL expiration.
  • Cache Tests (src/utils/cache.test.ts):
    • Added comprehensive unit tests covering hits, misses, TTL expiry, and capacity-based eviction.
  • Store Integration (src/stores/aiStore.ts):
    • Instantiated modelCache with a 5-minute TTL.
    • Refactored getHfModels() and getOpenRouterFreeModels() to check the cache before invoking backend API requests.

Acceptance Criteria

  • Implement a generic Caching Interface (with .get(), .set(), .invalidate()).
  • Provide an in-memory LRU cache implementation of this interface.
  • Integrate the cache into a known high-latency or high-frequency read path.
  • Add tests to ensure cache hits/misses function correctly and TTL is respected.

Impact & Side Effects

  • No breaking changes or side effects. Model fetches are now significantly faster after the first request.

How to Test

  1. Run tests with npx vitest run src/utils/cache.test.ts to verify caching behavior (hit vs miss).
  2. Start the app. Observe that calling getHfModels or getOpenRouterFreeModels triggers a backend fetch only once, and subsequent calls resolve instantly from the LRU cache.

Quality Checklist

  • I have run the linter and it passes.
  • I have run the tests and they pass.
  • I have reviewed my own code.

Copilot AI review requested due to automatic review settings July 23, 2026 08:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Yuvraj-Sarathe
Yuvraj-Sarathe merged commit 6c9b0e2 into Omnikon-Org:main Jul 23, 2026
2 of 3 checks passed
@Diwakar-odds

Copy link
Copy Markdown
Contributor Author

I have pushed a fix to resolve this issue and implement the requested caching layer. This PR strictly contains only the file changes necessary for the in-memory LRU cache integration.

Technical Analysis

  • What was done:
    1. Implemented a generic LRUCache<K, V> class utilizing JavaScript's native Map to guarantee LRU eviction tracking and O(1) performance for cache ops, alongside a discrete TTL expiry mechanism.
    2. Integrated the LRUCache into src/stores/aiStore.ts with a 5-minute TTL to wrap the high-latency backend Tauri calls (get_hf_models and get_openrouter_free_models).
    3. Added extensive unit testing in cache.test.ts to independently verify the eviction algorithm and TTL boundaries.

ECSoC26 Label Request

Please review and add the appropriate labels. Based on the ECSoC26 guidelines, I believe this qualifies for Level 3 (Core/Arch/Perf) because it directly implements a core performance optimization (caching layer) to reduce algorithmic latency and redundant expensive computations. Additionally, please consider adding the good-pr and good-backend (or equivalent performance/architecture bonus) labels as this directly optimizes internal application behavior.

Please review when you get a chance!

@Diwakar-odds

Copy link
Copy Markdown
Contributor Author

I have successfully resolved the merge conflicts and merged the latest upstream/main.

@Diwakar-odds

Copy link
Copy Markdown
Contributor Author

Hi @Omnikon-Org, I noticed this PR was merged/closed but seems to be missing the good pr and good backend labels (as this involved backend work). Could you please take a look and add the appropriate labels if it qualifies? Thank you!

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.

perf(core): implement multi-level caching mechanism for expensive computations and data retrieval

3 participants