Expand description
§lintel-schema-cache
Disk-backed schema cache with HTTP fetching and JSON parsing. Part of the Lintel project. Fetches JSON schemas over HTTP and stores results locally for fast subsequent lookups.
§Features
- SHA-256 keyed caching — schemas are stored as
<cache_dir>/<sha256>.jsonwhere<sha256>is the hex digest of the URI, avoiding hash collisions - Conditional requests — uses
ETag/If-None-Matchheaders to avoid re-downloading unchanged schemas - TTL support — configurable time-to-live for cache entries based on file modification time
- In-memory layer — frequently accessed schemas are also kept in memory for zero-IO lookups
- jsonschema integration — implements
jsonschema::AsyncRetrievefor seamless use as a schema resolver - Test-friendly —
SchemaCache::memory()constructor creates a memory-only cache with no HTTP or disk I/O
§Usage
ⓘ
use lintel_schema_cache::SchemaCache;
use std::time::Duration;
// Uses sensible defaults: system cache dir, 12h TTL
let cache = SchemaCache::builder().build();
// Or customize:
let cache = SchemaCache::builder()
.force_fetch(true)
.ttl(Duration::from_secs(3600))
.build();
let (schema, status) = cache.fetch("https://json.schemastore.org/tsconfig.json").await?;
// status: Hit (from disk/memory), Miss (fetched and cached), or Disabled (no cache dir)§Testing
Use the memory-only constructor to avoid network and disk I/O in tests:
ⓘ
use lintel_schema_cache::SchemaCache;
let cache = SchemaCache::memory();
cache.insert("https://example.com/schema.json", serde_json::json!({"type": "object"}));
let (val, _status) = cache.fetch("https://example.com/schema.json").await?;§License
Apache-2.0
Structs§
- Schema
Cache - A disk-backed schema cache with HTTP fetching and JSON parsing.
- Schema
Cache Builder
Enums§
- Cache
Status - Whether a schema was served from disk cache or fetched from the network.
Constants§
- DEFAULT_
SCHEMA_ CACHE_ TTL - Default TTL for cached schemas (12 hours).
Functions§
- ensure_
cache_ dir - Return a usable cache directory for schemas, creating it if necessary.