Cache¶
The local MemoryCache is available without Redis or another service. The optional Redis backend is development functionality, not part of published Fexu 0.2.0.
Local cache¶
Use TypedCache::new(MemoryCache::new(MemoryLimits::default())?, 1) for bounded process-local storage. A separate instance does not see the entries. Typed values include a serialization version. Use explicit namespace invalidation after changes to authoritative data.
Optional Redis cache¶
Enable the facade feature in your development project:
fexu = { version = "0.3.0-dev.1", features = ["redis-cache"] }
Use a matching development source checkout until the candidate is published. Set FEXU_REDIS_URL in the environment (the application reads it explicitly). For example:
use fexu::cache::{RedisCache, RedisLimits, TypedCache};
use std::time::Duration;
let url = std::env::var("FEXU_REDIS_URL")?;
let backend = RedisCache::new(&url, "myshop", RedisLimits::default())?;
let cache = TypedCache::new(backend, 1);
cache.set("tenant.42", "catalog", &vec!["book"], Some(Duration::from_secs(30))).await?;
let value: Option<Vec<String>> = cache.get("tenant.42", "catalog").await?;
cache.invalidate_namespace("tenant.42").await?;
Use the same application prefix across instances, a different prefix across environments, and a separate namespace for each tenant or consistency domain. The backend accepts standalone Redis URLs (redis:// on an isolated trusted network or certificate-verified rediss://); insecure TLS fragments and URL queries are refused. Credentials belong in the environment, not source code. Keep Redis unpublished, use an ACL limited to your cache prefix and required commands, and configure server memory and eviction limits. No connection is made until the first operation. For rediss://, the constructor installs the existing ring crypto provider as the process-wide Rustls default only if the application has not already selected a provider. Applications requiring a different provider must install it before constructing the cache. Certificate validation stays enabled.
Every Redis value has a TTL: by default 300 seconds, with a maximum of 24 hours. None selects the default, rather than permanent storage. Values are limited to 1 MiB by default (including typed serialization headers). Clones of a backend share one lazily opened multiplexed connection. Independent backend instances open their own connections. Concurrent callers share connection establishment; the total operation deadline includes waiting for a connection and running the command (default 2 seconds, maximum 60 seconds). After a connection error or timeout the failed handle is discarded; a later operation can reconnect. Failed mutations are not retried automatically because they might already have executed. Errors are reported as CacheError::Backend, not as misses. The caller decides whether to recompute from authoritative data. Do not use Redis cache failures to bypass authorization.
Consistency and security¶
Redis uses server-side atomic scripts and an exact namespace generation. Namespace invalidation makes the old generation unreachable immediately; the old values expire at their original TTL. A producer with an outdated token cannot publish after a mutation. For bounded metadata, all writes within the namespace advance a shared fence, so unrelated writes can conservatively return Superseded. Different namespaces do not share fences. Redis expiry or eviction is a miss; values can disappear at any time. Metadata loss fails closed through fresh random generations/tokens. Restoring an older Redis snapshot or asynchronous failover is not a supported consistency guarantee: use disposable cache data and invalidate after recovery. The first implementation is standalone Redis only, not Cluster or Sentinel.
get_or_set coalesces concurrent producers within a single TypedCache instance only. Distributed instances can compute the same value more than once. Neither backend guarantees consistency with an external database transaction; invalidate after commit and handle stale data according to your application. Do not cache passwords, tokens, CSRF values or sensitive session-specific HTML. View and fragment caching remain explicit opt-ins.
The repository's Redis CI exercises concurrent cloned handles, independent clients, a real server stop/restart while the original handles stay alive, and TLS with trusted and unrelated test CAs. Use a certificate whose subject alternative name matches the connection host; configure the client trust store for your deployment. The tests do not certify production TLS infrastructure, mutual TLS or cluster failover.