Cache and HTTP client
Include cache and outbound HTTP connections in the application's DI and resource lifecycle.
Use Lily's DI services instead of recreating a cache or outbound HTTP client for every request. Select these integrations through lilyrs features; the HTTP host does not enable them automatically.
Redis cache#
lilyrs::redis provides typed JSON/binary envelopes, TTL, bounded scans, operation contexts, and factory mode. For a single Redis setup, use the dependency/configuration example on the rate limiting page.
Create a configured operation context through CacheService. Use the explicit bridge in the guard example to connect request cancellation to a Redis token. Short convenience APIs do not automatically receive HTTP cancellation.
For multiple Redis cells, select redis-factory instead of redis and use lilyrs::redis::CacheFactory with named-cell configuration. Both modes cannot be enabled in the same dependency graph. The configuration section remains [cache].
Adding the cache integration does not create automatic HTTP response caching or a session store. Define cache keys, TTL, and invalidation policy in your domain layer.
Named HTTP clients#
lilyrs::http_client::LilyHttpClientFactory prepares named profiles from configuration and manages client resources. Enable http-client on the existing lilyrs dependency and inject the factory into your application service. If you also use Redis, include your selected redis or redis-factory feature in the same list.
lilyrs = { version = "0.1.0", features = ["http-api", "http-client"] }[http_client_factory]
connect_timeout_secs = 5
request_timeout_secs = 15
[http_client_factory.clients.payment_api]
base_address = "https://payments.example.com"Replace the example address with your own external service's address. base_address is the profile's URL field.
async fn send_payment_request(
factory: &lilyrs::http_client::LilyHttpClientFactory,
) -> Result<(), Box<dyn std::error::Error>> {
let client = factory.get("payment_api")?;
let request = client.get("/transactions")?.build()?;
let response = client.execute(request).await?;
println!("status={}", response.status().as_u16());
Ok(())
}This snippet requires a profile named payment_api. Fill in its base URL, request/response bounds, and timeouts according to the lily_http_client configuration contract. Do not initialize the factory manually in a handler; the container owns it.
Response models#
The outbound HTTP client's response model is separate from the server action's response model. Convert the external service's body into a domain DTO and respond through your own endpoint contract. Do not pass external errors or headers into the public API without control.
The client's extensive profile and transport options belong to the ecosystem reference; enable only the integration your HTTP application uses.
Sources for this page v0.1.0
This guide is based on the reviewed framework commit.