Skip to content

add cache api for cdn apps#69

Merged
ruslanti merged 6 commits into
mainfrom
feat/add-cache-api-for-cdn-apps
May 6, 2026
Merged

add cache api for cdn apps#69
ruslanti merged 6 commits into
mainfrom
feat/add-cache-api-for-cdn-apps

Conversation

@ruslanti
Copy link
Copy Markdown
Collaborator

@ruslanti ruslanti commented May 6, 2026

No description provided.

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.
@ruslanti ruslanti requested review from godronus and qrdl May 6, 2026 08:45
@ruslanti ruslanti self-assigned this May 6, 2026
Copilot AI review requested due to automatic review settings May 6, 2026 08:45
@ruslanti ruslanti added the enhancement New feature or request label May 6, 2026
@ruslanti ruslanti linked an issue May 6, 2026 that may be closed by this pull request
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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::cache module 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 thread src/lib.rs
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
@ruslanti ruslanti merged commit 7fc63c6 into main May 6, 2026
10 checks passed
@ruslanti ruslanti deleted the feat/add-cache-api-for-cdn-apps branch May 6, 2026 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Cache API for HTTP apps

3 participants