Skip to content

Replace per-process in-memory cache with Flask-Caching (Redis backend) - #48

Merged
Jackass4life merged 2 commits into
mainfrom
copilot/fix-in-memory-cache-redis
Mar 24, 2026
Merged

Replace per-process in-memory cache with Flask-Caching (Redis backend)#48
Jackass4life merged 2 commits into
mainfrom
copilot/fix-in-memory-cache-redis

Conversation

Copilot AI commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

SimpleCache via a plain dict is per-process. With 4 Gunicorn workers each maintains its own cache — 4× API load, inconsistent state across requests.

Changes

  • app.py: Replace _cache dict + manual TTL logic with flask_caching.Cache. Backend configurable via CACHE_TYPE / CACHE_REDIS_URL env vars; defaults to SimpleCache for single-worker/dev use.
  • docker-compose.yml: Add redis:7-alpine service; wire nautobot-maps to use RedisCache by default.
  • requirements.txt: Add Flask-Caching==2.3.1, redis==5.3.0.
  • .env.example / README.md: Document CACHE_TYPE and CACHE_REDIS_URL.
  • Tests: _cache.clear()cache.clear(); rewrite expiry test to use Flask-Caching timeout instead of manipulating internal dict timestamps.

Usage

# .env
CACHE_TYPE=RedisCache
CACHE_REDIS_URL=redis://redis:6379/0

No env vars needed for dev — falls back to SimpleCache automatically.

Original prompt

This section details on the original issue you should resolve

<issue_title>Issue: Fix the in-memory cache — it breaks with multiple workers</issue_title>
<issue_description>Flask-Caching's SimpleCache is per-process. With 4 Gunicorn workers, each has its own cache — you get 4x the Nautobot API load and inconsistent state. Add Redis:
yaml# docker-compose.yml additions
redis:
image: redis:7-alpine
restart: unless-stopped


Add to `.env`:

CACHE_TYPE=RedisCache
CACHE_REDIS_URL=redis://redis:6379/0
Wire it in app.py via Flask-Caching config.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

- Add Flask-Caching and redis to requirements.txt
- Configure Flask-Caching from CACHE_TYPE and CACHE_REDIS_URL env vars
- Replace custom _cache dict with Flask-Caching cache object
- Add redis service to production docker-compose.yml
- Update .env.example and README.md with new config vars
- Update all tests to use cache.clear() instead of _cache.clear()

Co-authored-by: Jackass4life <94110786+Jackass4life@users.noreply.github.com>
Agent-Logs-Url: https://github.com/Jackass4life/Nautobot-maps/sessions/a4c3f513-0056-46da-a185-d09ced34fff2
Copilot AI changed the title [WIP] Fix in-memory cache by adding Redis support Replace per-process in-memory cache with Flask-Caching (Redis backend) Mar 24, 2026
Copilot AI requested a review from Jackass4life March 24, 2026 10:31
@Jackass4life
Jackass4life marked this pull request as ready for review March 24, 2026 10:37
@Jackass4life
Jackass4life merged commit 83dd373 into main Mar 24, 2026
3 checks passed
@Jackass4life
Jackass4life deleted the copilot/fix-in-memory-cache-redis branch March 24, 2026 11:45
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.

Issue: Fix the in-memory cache — it breaks with multiple workers

2 participants