Skip to content

[Feature]: Add server-side in-memory cache for frequently accessed API queries #231

@Vraj1234

Description

@Vraj1234

Pre-flight checklist

  • I have searched the existing issues

Problem to solve

High-traffic discovery endpoints (/crops, /resources, /geoids, /parameters) execute the same DB queries on every request. While materialized views make individual queries fast, there's no application-layer cache preventing redundant round-trips to Cloud SQL for identical inputs within a request window.

Proposed solution or API

Use a simple TTL cache (e.g. cachetools.TTLCache or functools.lru_cache with a time wrapper) on the service layer functions. TTL should be keyed to the ETL refresh interval. No Redis required — an in-process cache is sufficient given Cloud Run's single-process-per-instance model.

Alternatively, evaluate Redis via Cloud Memorystore if cross-instance consistency becomes a requirement when min-instances > 1.

Alternatives considered

HTTP caching headers (see related issue) — complementary, not a substitute. Headers help clients; in-memory cache helps the server avoid DB round-trips.

Additional context

Ref: #135 (caching sub-task).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions