CacheModule gives you an injectable CacheService that works out of the box with an in-memory store and optionally upgrades to Redis for multi-instance deployments. The API is the same regardless of backend: get, set, del, and ttl. Enable the cache feature in Cargo.toml, register the module with CacheModule::register(CacheOptions::in_memory()), and inject CacheService into any provider.
Enable the feature
Register CacheModule
PassCacheOptions::in_memory() to CacheModule::register in your module’s imports list. The returned DynamicModule exports CacheService for injection.
Inject and use CacheService
CacheService is injected as Arc<CacheService>. Use get::<T> for typed deserialization or get_json when you want a raw serde_json::Value.
Operations reference
set — store a value with optional TTL
set serializes any Serialize type and stores it under a key. Pass Some(Duration::from_secs(60)) to expire the entry after 60 seconds, or None to keep it indefinitely.get — retrieve a typed value
get::<T> deserializes the stored JSON into your type. Returns Ok(None) when the key is missing or expired.get_json — retrieve raw JSON
When you want the raw
serde_json::Value without a concrete type, use get_json. Returns None on a cache miss or expiry.del — remove an entry
del removes the key and returns true if the key existed, false if it was already absent.CacheOptions
CacheOptions is an enum with two variants. You select the backend at registration time, not at injection time.
| Variant | Method | Description |
|---|---|---|
InMemory | CacheOptions::in_memory() | Single-process store backed by a tokio::sync::RwLock<HashMap>. Zero dependencies. |
Redis | CacheOptions::redis(url) | Shared store backed by a multiplexed Redis connection. Requires the cache-redis feature. |
Redis backend
Enable thecache-redis feature alongside cache, then pass CacheOptions::redis(url) to register. The Redis client is lazy — the connection is established on the first cache operation.
RedisCacheOptions with a key prefix
For applications that share one Redis instance across multiple services, useRedisCacheOptions directly to add a prefix. All keys will be stored as prefix:key.
Redis TTLs use millisecond precision internally (via the
PX option on SET) to match the granularity of the in-memory backend.set_json — low-level raw JSON store
If you already have aserde_json::Value and want to skip the serialization step, use set_json directly:
Troubleshooting
| Symptom | What to check |
|---|---|
get returns None immediately after set | The key may have been set with a very short TTL, or the value failed to serialize. |
| Redis connection refused | Confirm the URL scheme (redis:// or rediss://) and that the server is reachable. |
feature not enabled at compile time | Add both cache and cache-redis to the features list in Cargo.toml. |
| Multiple services not sharing cache state | Use the Redis backend — in-memory state is not shared across processes or threads outside the single Arc<CacheService> instance. |