Skip to content

Conversation

@ponderingdemocritus
Copy link
Contributor

@ponderingdemocritus ponderingdemocritus commented Jan 18, 2026

Summary

Adds a query result caching layer to improve read performance for frequently accessed data.

What this PR adds:

  • Query cache module (torii-cache): New caching infrastructure with a QueryCache trait and multiple backend implementations
  • In-memory cache: Thread-safe cache using DashMap with TTL-based expiration (always available)
  • Redis cache: Optional distributed cache backend for multi-instance deployments
  • Tiered caching: Tries Redis first, falls back to in-memory - writes to both for redundancy
  • CachingPool: Wrapper around SQLite pool that intercepts SELECT queries and caches results
  • Cache key generation: SHA-256 hash of SQL + bind parameters, with table name extraction for pattern-based invalidation
  • Automatic invalidation: Caches are invalidated by table pattern when data is modified

CLI options:

--cache.enabled      Enable query result caching (default: false)
--cache.ttl          Cache TTL in seconds (default: 60)
--cache.redis_url    Optional Redis URL for distributed caching

Integration:

  • gRPC SqlQuery endpoint now uses cached queries via CachingPool
  • Write operations trigger pattern-based cache invalidation (e.g., torii:query:entities:*)

Test plan

  • cargo check -p torii-sqlite -p torii-grpc-server -p torii-runner
  • Unit tests for cache backends (cargo test -p torii-cache)
  • gRPC integration tests (currently blocked by katana timeout / db version mismatch)

@ponderingdemocritus ponderingdemocritus changed the title feat(cache): wire query cache into reads and invalidation feat(cache): add tiered query cache with in-memory and optional Redis backends Jan 18, 2026
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.

2 participants