Skip to content

feat(backend): introduce bounded lru_cache and observability to query#402

Open
SaitejaKommi wants to merge 1 commit intoopenfoodfacts:mainfrom
SaitejaKommi:feat/query-builder-cache-policy
Open

feat(backend): introduce bounded lru_cache and observability to query#402
SaitejaKommi wants to merge 1 commit intoopenfoodfacts:mainfrom
SaitejaKommi:feat/query-builder-cache-policy

Conversation

@SaitejaKommi
Copy link
Copy Markdown

Summary

This PR modernizes query-builder caching in search.py by replacing manually maintained unbounded dictionaries with bounded native lru_cache strategies.

It introduces automatic config-aware cache invalidation and exposes runtime cache metrics for observability, improving memory safety, maintainability, and operational tuning.


Related Issue

Fixes #401


What Changed

Refactored Cache Engine

Removed manually managed in-memory cache dictionaries:

  • _ES_QUERY_BUILDERS
  • _RESULT_PROCESSORS

Introduced Bounded Caching

Wrapped query-builder / processor factories using:

@functools.lru_cache(maxsize=32)

Benefits include:

  • Prevents unbounded memory growth
  • Uses proven Python-native cache semantics
  • Simplifies custom cache management logic

Config-Aware Invalidation

Bound cache keys to a configuration identity/version parameter using:

id(get_config())

When the active config object reloads, the new identity naturally causes cache misses and rebuilds fresh entries.

Added Observability Hooks

Exposed runtime cache metrics via:

.cache_info()._asdict()

Surfaced values include:

  • hits
  • misses
  • maxsize
  • currsize

These metrics are now available through health diagnostics, enabling live tuning and monitoring.

Improved Debug Visibility

Added debug logging around Elasticsearch translation / cache evaluation paths.


Affected Files

  • search.py
  • health.py
  • Related cache behavior tests

Testing

Verification gateway executed successfully:

  • Backend unit tests ✅
  • Backend integration tests ✅
  • Frontend build/tests (dev) ✅
  • Frontend build/tests (prod) ✅

Notes

This is a backend performance and observability enhancement with no intended API behavior changes.

…-builder

Replaced unbounded dict caches in app/search.py with @functools.lru_cache(maxsize=32).
Added config_version parameter to automatically trigger cache invalidation upon configuration reloads.
Exposed cache info dict securely via the /health endpoint and internal debug log statements.
@github-project-automation github-project-automation Bot moved this to Backlog (ready for dev) in 🔎 Search-a-licious Apr 23, 2026
@SaitejaKommi SaitejaKommi changed the title feat(backend): introduce bounded lru_cache and observability to query… feat(backend): introduce bounded lru_cache and observability to query Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog (ready for dev)

Development

Successfully merging this pull request may close these issues.

Feature: Introduce cache policy and observability around query-builder cache

1 participant