add cache api for cdn apps#69
Merged
Merged
Conversation
Introduced a comprehensive cache module supporting both async and sync handlers, enabling get, set, delete, and other operations with optional TTLs. Includes detailed documentation and examples for various use cases to streamline caching in FastEdge apps.
Introduced two distinct examples demonstrating response caching: an async WASI cache for forwarding requests to an origin and a sync cache-aside example for generating and caching responses on demand.
Added the `Cargo.lock` file for the WASI HTTP cache project, ensuring reproducible builds by locking dependency versions.
Replaces async cache API with synchronous `fastedge::cache` API across examples and removes unused `querystring` dependency. Updates the HTTP examples to utilize consistent patterns and improves clarity in inline documentation.
qrdl
approved these changes
May 6, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a synchronous Cache API surface in the fastedge crate (via re-exports from the generated component-model bindings) and adds new example applications demonstrating cache-aside usage in both the basic FastEdge HTTP model and the WASI-HTTP (wstd) model.
Changes:
- Added
fastedge::cachemodule with documented cache operations (get,set,delete,exists,incr,expire). - Expanded crate-level docs with a “Using the Cache” example.
- Added two new caching examples: a basic cache-aside handler and a WASI-HTTP origin-response caching demo.
Reviewed changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| src/lib.rs | Adds fastedge::cache re-exports and cache usage documentation; tweaks Body intra-doc links. |
| examples/http/basic/cache/src/lib.rs | New basic (sync) cache-aside example handler. |
| examples/http/basic/cache/Cargo.toml | New Cargo manifest for the basic cache example. |
| examples/http/wasi/cache/src/lib.rs | New WASI-HTTP example that fetches from an origin and caches responses. |
| examples/http/wasi/cache/Cargo.toml | New Cargo manifest for the WASI cache example. |
| examples/http/wasi/cache/Cargo.lock | Adds a per-example lockfile for the WASI cache example. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| let body = generate_body(&path); | ||
|
|
||
| // Store in cache with TTL | ||
| cache::set(&cache_key, body.as_bytes(), Some(ttl_ms))?; |
Comment on lines
+48
to
+56
| // Return cached response if available | ||
| if let Some(cached) = cache::get(&cache_key)? { | ||
| println!("cache hit: {cache_key}"); | ||
| return Ok(Response::builder() | ||
| .status(200) | ||
| .header("content-type", "application/octet-stream") | ||
| .header("x-cache", "hit") | ||
| .body(Body::from(cached))?); | ||
| } |
Comment on lines
+5
to
+14
| Example app demonstrating response caching via the cache interface. | ||
|
|
||
| The app reads ORIGIN_HOST from the environment, forwards the incoming request | ||
| to that origin, and caches the response body keyed by the request path. | ||
| On subsequent requests for the same path the cached body is returned directly | ||
| without hitting the origin. | ||
|
|
||
| Cache reads and writes use the synchronous `fastedge::cache` API; upstream | ||
| HTTP I/O still uses the async `wstd` client. | ||
|
|
Comment on lines
+71
to
+76
| let status = upstream_resp.status(); | ||
| let headers: Vec<(String, String)> = upstream_resp | ||
| .headers() | ||
| .iter() | ||
| .map(|(k, v)| (k.to_string(), v.to_str().unwrap_or("").to_string())) | ||
| .collect(); |
|
|
||
| // Only cache successful responses | ||
| if status.is_success() { | ||
| cache::set(&cache_key, &body_bytes, Some(ttl_ms))?; |
| crate-type = ["cdylib"] | ||
|
|
||
| [dependencies] | ||
| fastedge = { path = "../../../../" } |
|
|
||
| [dependencies] | ||
| wstd = "0.6" | ||
| fastedge = { path = "../../../../" } |
Comment on lines
+420
to
+427
| /// # Operations | ||
| /// | ||
| /// - [`cache::get`] — fetch a cached value by key | ||
| /// - [`cache::set`] — write or overwrite a value by key (with optional TTL) | ||
| /// - [`cache::delete`] — remove a key from the cache | ||
| /// - [`cache::exists`] — check whether a key is present | ||
| /// - [`cache::incr`] — atomically increment (or decrement) an integer value | ||
| /// - [`cache::expire`] — update the TTL of an existing key |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.