Skip to content

feat: Implement 1-minute in-memory cache for Montpellier CSV data #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2025

Conversation

aslafy-z
Copy link
Owner

No description provided.

This commit introduces an in-memory caching mechanism for the Montpellier
real-time transit data CSV. Instead of fetching and parsing the CSV on
every API request, the data is now cached for 1 minute.

Key changes:
- Fetched CSV data is stored in a module-level cache (`cachedData`).
- A timestamp (`lastCacheTime`) tracks the age of the cache.
- If a request arrives and the cache is older than 1 minute or empty,
  the data is re-fetched and the cache is updated.
- An initial attempt to populate the cache is made when a function
  instance starts.
- The `api/query.js` handler now uses this caching logic.
- Added comments explaining the in-memory cache behavior on Vercel's
  serverless environment, noting it's per-instance and suggesting
  Vercel KV and Cron Jobs for persistent, shared caching.
- Introduced a test file `api/query.test.js` with unit tests for
  the caching logic (initial fetch, cache hit, cache expiration) and
  data filtering.
- Modified `api/query.js` to allow testability of cache internals
  when `NODE_ENV=test`.

This change aims to reduce redundant downloads and improve API response
times for frequent requests to the same function instance.
Copy link

vercel bot commented May 29, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
montpellier-tam-api ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 10, 2025 1:46pm

@aslafy-z aslafy-z merged commit 1f82c00 into main Jul 10, 2025
3 checks passed
@aslafy-z aslafy-z deleted the feat/montpellier-csv-cache branch July 10, 2025 14:32
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.

1 participant