Skip to content

improvement(performance): added revalidation caches on ollama and openrouter models - #1872

Merged
waleedlatif1 merged 2 commits into
stagingfrom
fix/local-models
Nov 10, 2025
Merged

improvement(performance): added revalidation caches on ollama and openrouter models#1872
waleedlatif1 merged 2 commits into
stagingfrom
fix/local-models

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

  • added revalidation caches on ollama and openrouter models
  • avoids refetching models on every re-render

Type of Change

  • Other: performance

Testing

Manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel

vercelBot commented Nov 10, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
ProjectDeploymentPreviewCommentsUpdated (UTC)
docsSkippedSkippedNov 10, 2025 8:08pm

@greptile-apps

greptile-appsBot commented Nov 10, 2025

Copy link
Copy Markdown
Contributor

Greptile Overview

Greptile Summary

This PR implements a two-layer caching strategy to optimize model fetching performance for Ollama and OpenRouter providers.

Key Changes

  • Server-side caching: Added Next.js revalidation cache (60s for Ollama, 300s for OpenRouter) by removing force-dynamic export and adding next: { revalidate } to fetch calls
  • Client-side caching: Added early-return check in Zustand store to prevent re-fetching when models are already loaded

Performance Impact

The implementation reduces unnecessary API calls through dual-layer caching:

  1. Next.js cache prevents repeated external API calls within the revalidation window
  2. Store check prevents redundant fetches on component re-renders

Note

The client-side check in store.ts:84-87 prevents any refetch once models are loaded. This means users won't see updated models until a full page reload, even after the server cache expires. This trade-off prioritizes performance over real-time freshness.

Confidence Score: 5/5

  • Safe to merge - well-implemented performance optimization with appropriate caching strategies
  • The changes are straightforward and follow Next.js best practices for data caching. The dual-layer approach (server + client) is sound, and the implementation correctly removes conflicting cache directives. The trade-off between performance and freshness is acceptable for this use case where model lists change infrequently.
  • No files require special attention

Important Files Changed

File Analysis

FilenameScoreOverview
apps/sim/app/api/providers/ollama/models/route.ts5/5Removed force-dynamic export and added 60-second revalidation cache to reduce unnecessary API calls to Ollama service
apps/sim/app/api/providers/openrouter/models/route.ts5/5Removed force-dynamic export and replaced no-store with 300-second revalidation cache to optimize OpenRouter API calls
apps/sim/stores/providers/store.ts4/5Added client-side check to prevent re-fetching models if they're already loaded in the store

Sequence Diagram

sequenceDiagram
participant Client as React Component
participant Store as Zustand Store
participant API as Next.js API Route
participant External as External Service<br/>(Ollama/OpenRouter)
Note over Client,External: Initial Load (Store Initialization)
Store->>Store: Check if models.length > 0
Store->>Store: Check if isLoading = true
Store->>API: fetch('/api/providers/ollama/models')
API->>API: Check Next.js cache<br/>(revalidate: 60s)
alt Cache Miss or Expired
API->>External: fetch('http://localhost:11434/api/tags')
External-->>API: Return models list
API->>API: Cache response (60s)
else Cache Hit
API->>API: Return cached response
end
API-->>Store: { models: [...] }
Store->>Store: setModels(provider, models)
Store-->>Client: Update state
Note over Client,External: Subsequent Re-render
Client->>Store: fetchModels('ollama')
Store->>Store: Check if models.length > 0
alt Models Already Loaded
Store-->>Client: Skip fetch (early return)
else Models Not Loaded
Store->>API: fetch('/api/providers/ollama/models')
Note over API: Returns cached response<br/>if within 60s window
end
Loading

@greptile-appsgreptile-appsBot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Additional Comments (2)

  1. apps/sim/app/api/providers/ollama/models/route.ts, line 9-24 (link)

    logic:export const dynamic = 'force-dynamic' on line 9 conflicts with next: { revalidate: 60 } on line 24. When dynamic = 'force-dynamic' is set, Next.js ignores fetch-level caching and re-executes the route handler on every request, defeating the purpose of the revalidation cache.

    Remove export const dynamic = 'force-dynamic' to allow the fetch cache to work, or use route-level caching:

  2. apps/sim/app/api/providers/openrouter/models/route.ts, line 7-21 (link)

    logic:export const dynamic = 'force-dynamic' on line 7 conflicts with next: { revalidate: 300 } on line 21. When dynamic = 'force-dynamic' is set, Next.js ignores fetch-level caching and re-executes the route handler on every request, negating the revalidation cache.

    Remove export const dynamic = 'force-dynamic' to enable fetch caching, or use route-level caching:

3 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

@waleedlatif1

Copy link
Copy Markdown
CollaboratorAuthor

@greptile

@greptile-appsgreptile-appsBot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

3 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@waleedlatif1
waleedlatif1 merged commit 81a12e7 into stagingNov 10, 2025
9 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/local-models branch November 10, 2025 20:19
waleedlatif1 added a commit that referenced this pull request Nov 10, 2025
…nrouter models (#1872)
* improvement(performance): added revalidation caches on ollama and openrouter models
* ack PR comments
waleedlatif1 added a commit that referenced this pull request Nov 12, 2025
…nrouter models (#1872)
* improvement(performance): added revalidation caches on ollama and openrouter models
* ack PR comments
waleedlatif1 added a commit that referenced this pull request Nov 12, 2025
…nrouter models (#1872)
* improvement(performance): added revalidation caches on ollama and openrouter models
* ack PR comments
@waleedlatif1waleedlatif1 mentioned this pull request Nov 12, 2025
10 tasks
Sign up for freeto 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.

1 participant

@waleedlatif1