Expand description
§hitbox-fn
Function memoization for the Hitbox caching framework.
This crate provides tools for caching async function results using the hitbox FSM. It wraps function arguments and return types into the hitbox pipeline, handling cache key generation, serialization, and upstream execution.
§Overview
Cache- Pre-configured cache with backend, policy, and concurrency settings- [
#[cached]] - Attribute macro that transforms async functions into cacheable functions KeyExtract- Trait for types to describe their cache key contributionArg/Skipped- Wrappers to include or exclude function parameters from cache keysFnExtractor- BridgesKeyExtractto hitbox’sExtractortraitFnUpstream- Adapts async functions to hitbox’sUpstreamtrait
§Quick Start
ⓘ
use hitbox_fn::prelude::*;
use hitbox_moka::MokaBackend;
use std::time::Duration;
#[derive(KeyExtract)]
struct UserId(u64);
#[cached]
async fn fetch_user(id: UserId) -> Result<User, Error> {
// expensive operation
}
// Build a reusable cache
let cache = Cache::builder()
.backend(MokaBackend::builder().max_entries(1000).build())
.policy(PolicyConfig::builder().ttl(Duration::from_secs(60)).build())
.build();
// Call through cache
let user = fetch_user(UserId(42))
.cache(&cache)
.await?;
// With cache context (hit/miss/stale info)
let (user, ctx) = fetch_user(UserId(42))
.cache(&cache)
.with_context()
.await;
// Or call directly without caching (passthrough)
let user = fetch_user(UserId(42)).await?;§Feature Flags
derive- Enable derive macros (#[cached],#[derive(KeyExtract)], etc.) viahitbox-deriverkyv_format- Enable rkyv zero-copy serialization support
Modules§
- prelude
- Prelude for convenient imports.
Structs§
- Arg
- Wrapper for function arguments included in cache key extraction.
- Args
- Wrapper around tuple to satisfy orphan rule.
- Cache
- Pre-configured cache with backend, policy, concurrency manager, and offload manager.
- Cache
Builder - Builder for
Cachewith typestate pattern. - FnExtractor
- Generic extractor that uses
KeyExtracttrait to generate cache keys. - FnUpstream
- Wrapper that adapts an async function to the
Upstreamtrait. - NoBackend
- Marker: no backend configured.
- NoContext
- Marker: no context requested in response.
- NoPolicy
- Marker: no policy configured.
- Skipped
- Wrapper for function arguments excluded from cache key extraction.
- With
Backend - Marker: backend configured.
- With
Context - Marker: context requested in response.
- With
Policy - Marker: policy configured.
Traits§
- Cache
Access - Trait for accessing cache internals.
- Cached
Field Clone - Marker trait for types that can be used as cached (non-skipped) fields in
#[derive(CacheableResponse)]. - KeyExtract
- Trait for types that can contribute to a cache key.
- Skipped
Field Default - Marker trait for types that can be used as skipped fields in
#[derive(CacheableResponse)].