Deterministic RFC 9111 HTTP cache decision engine for MoonBit
Dependencies
moon add oyjh0381/http-cache///|
let stored = @cache.stored_response(
@cache.request("GET", "https://example.test/catalog"),
@cache.response(
200,
headers=@cache.Headers::empty()
.add("cache-control", "public, max-age=120")
.add("etag", "\"catalog-v1\""),
),
1000L,
1002L,
)
///|
let decision = @cache.evaluate_reuse(
Some(stored),
@cache.request("GET", "https://example.test/catalog"),
@cache.SharedCache,
1060L,
)moon run examples/library-demomoon run --target native cmd/moon-http-cache -- \
analyze examples/scenarios/fresh-hit.json
moon run --target native cmd/moon-http-cache -- \
analyze examples/scenarios/stale-revalidate.json --format jsonmoon check --target all --deny-warn
moon test --target all --deny-warn
moon run examples/library-demo
moon run --target native cmd/moon-http-cache -- \
analyze examples/scenarios/fresh-hit.json
moon fmt --check
moon infotype Timestamp = Int64pub(all) struct AgeResult {
breakdown : AgeBreakdown
trace : Array[TraceStep]
diagnostics : Array[Diagnostic]
} derive(Eq, Debug)pub(all) struct CacheControl {
directives : Array[CacheDirective]
diagnostics : Array[Diagnostic]
} derive(Eq, Debug)pub(all) struct Diagnostic {
level : DiagnosticLevel
code : String
message : String
field_name : String?
} derive(Eq, Debug)pub(all) struct FreshnessResult {
source : FreshnessSource
freshness_lifetime : Int64
current_age : Int64
remaining_freshness : Int64
fresh : Bool
trace : Array[TraceStep]
diagnostics : Array[Diagnostic]
} derive(Eq, Debug)pub(all) struct ReuseDecision {
verdict : ReuseVerdict
current_age : Int64
freshness_lifetime : Int64
trace : Array[TraceStep]
diagnostics : Array[Diagnostic]
} derive(Eq, Debug)pub(all) struct StorageDecision {
verdict : StorageVerdict
fields_to_remove : Array[String]
fields_requiring_validation : Array[String]
trace : Array[TraceStep]
diagnostics : Array[Diagnostic]
} derive(Eq, Debug)pub(all) struct StoredResponse {
request : RequestMetadata
response : ResponseMetadata
request_time : Int64
response_time : Int64
} derive(Eq, Debug)pub(all) struct ValidationMergeResult {
merged : StoredResponse?
trace : Array[TraceStep]
diagnostics : Array[Diagnostic]
} derive(Eq, Debug)fn calculate_freshness(stored : StoredResponse, mode : CacheMode, now : Int64, policy? : CachePolicy) -> FreshnessResultfn evaluate_invalidation(request : RequestMetadata, response : ResponseMetadata) -> InvalidationDecisionfn evaluate_reuse(stored : StoredResponse?, presented : RequestMetadata, mode : CacheMode, now : Int64, origin? : OriginState, policy? : CachePolicy) -> ReuseDecisionfn evaluate_storage(request : RequestMetadata, response : ResponseMetadata, mode : CacheMode, now : Int64) -> StorageDecisionfn format_http_date(value : Int64) -> String?fn merge_not_modified(stored : StoredResponse, not_modified : ResponseMetadata, request_time : Int64, response_time : Int64) -> ValidationMergeResultfn parse_http_date(value : String, now : Int64) -> Int64?fn stored_response(request : RequestMetadata, response : ResponseMetadata, request_time : Int64, response_time : Int64) -> StoredResponseInstall
Download zipDeterministic RFC 9111 HTTP cache decision engine for MoonBit
Dependencies