Skip to content

perf: implement Redis caching layer for gist queries#86

Merged
snowrugar-beep merged 1 commit into
VertexChainLabs:mainfrom
anonfedora:feat/redis-caching-layer
Jun 23, 2026
Merged

perf: implement Redis caching layer for gist queries#86
snowrugar-beep merged 1 commit into
VertexChainLabs:mainfrom
anonfedora:feat/redis-caching-layer

Conversation

@anonfedora

Copy link
Copy Markdown
Contributor

Title: perf: implement Redis caching layer for gist queries

Description:
This PR implements a Redis caching layer for gist queries to reduce read load on PostgreSQL and improve response times for repeated queries.

Changes

Core Implementation

  • Added ioredis dependency for Redis client connection
  • Created CacheModule and CacheService with graceful degradation when Redis is unavailable
  • Cache key format:
    • Nearby queries: gist:nearby:{lat}:{lon}:{radius}:{limit}
    • Single gist: gist:one:{id}
  • TTL configuration:
    • Nearby queries: 60 seconds
    • Single gist: 300 seconds (5 minutes)

Service Integration

  • Modified GistsService.findNearby() to cache results (excludes paginated queries with cursor)
  • Modified GistsService.findOne() to cache individual gists
  • Added invalidateNearbyCache() method called on gist creation
  • Cache invalidation uses pattern-based deletion for geographic area

Configuration

  • Added REDIS_URL environment variable to .env.example
  • Integrated CacheModule into AppModule and GistsModule
  • Redis connection includes retry strategy and error handling

Testing

  • Unit tests: 9 tests for CacheService covering initialization, get/set/delete operations, metrics, and graceful degradation
  • Integration tests: Added to gists.e2e.spec.ts to verify graceful degradation when Redis is unavailable
  • All existing unit tests pass (67/67)

Acceptance Criteria Met

✅ First query misses cache, hits DB, populates cache
✅ Second identical query returns from cache (no DB hit)
✅ New gist creation invalidates nearby cache for that area
✅ Redis unavailability does not crash the application
✅ Cache hit rate is logged/metrics available
✅ All existing tests pass

Technical Details

Graceful Degradation

  • If REDIS_URL is not configured, caching is disabled and all queries hit the database
  • Redis connection errors are caught and logged; service continues without caching
  • No application crashes due to Redis unavailability

Cache Invalidation Strategy

  • When a new gist is created, all nearby cache keys for that geographic area are invalidated
  • Uses pattern matching: gist:nearby:{lat:.4f}:{lon:.4f}:*
  • Ensures stale data is not served after new gists are added

Metrics

  • CacheService tracks hits, misses, and hit rate percentage
  • Metrics can be retrieved via getMetrics() method
  • Useful for monitoring cache effectiveness

Testing Strategy

Unit Tests

  • Mock Redis client to test service logic without actual Redis
  • Test graceful degradation scenarios
  • Verify metrics tracking

Integration Tests

  • Test that application works correctly when Redis is unavailable
  • Verify no crashes or errors during cache operations

CI/CD

  • All tests pass in CI environment
  • Linting passes with no errors
  • Build succeeds with TypeScript compilation

Closes #25

Copy link
Copy Markdown
Contributor

Nice work @anonfedora! 🎉 CI is green across the board and the Redis caching layer is a really solid improvement — clear TTLs, graceful fallback when REDIS_URL is unset, and hit-rate metrics baked in. Thanks for tackling #25, merging this in now.

@snowrugar-beep snowrugar-beep merged commit bf37c60 into VertexChainLabs:main Jun 23, 2026
6 checks passed
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.

perf: implement Redis caching layer for gist queries

2 participants