cache
Stable type · Cache
Tier: stable · Kind: type · Topic: Cache
| Python | Julia | Rust |
|---|---|---|
Cache | Cache | Cache |
Python
class Cache
Cache(store=None, *, root: Optional[os.PathLike] = None, store_name: str = 'local', offline: Optional[bool] = None, auth=None, verify: bool = False, revalidate_file: Optional[bool] = None)
Cache.fetch(resolved_url: str, *, source_loader: Optional[str] = None, auth_realm: Optional[str] = None, temporal: Optional[validate.Temporal] = None, expected_checksum: Optional[str] = None, mirrors: Sequence[str] = ()) -> CacheEntry
A content-addressed fetch cache over a pluggable Store
.
Parameters
store— An explicit store instance. When omitted, thestore_namebackend is built through thestoreregistry (defaultlocal) rooted atroot(else$EARTHSCIDATADIR/ the/scratch.localdefault).offline(Optional[bool]) —True/Falseforces offline/online;None(default) consults$EARTHSCI_OFFLINE. The explicit argument wins over the environment.auth— AnAuthRegistry, a single resolver, an iterable of resolvers, or a{realm: resolver}dict;Nonemeans no auth.verify(bool) — Re-verifysha256+ byte-length on every read (off by default, on for CI/conformance perspec/cache-format.md§4). This checks the cached blob against its own manifest — cache-internal consistency. It is NOT thefile://source recheck; seerevalidate_file.revalidate_file(Optional[bool]) — Recheck afile://entry against the file it was ingested from before serving it (spec/cache-format.md§4.1).None(default) consults$EARTHSCI_REVALIDATE_FILE, which leaves it on unless explicitly falsey. Turning it off restores the behaviour of EarthSciML/EarthSciAST#293 — a local file replaced in place is served from the warm entry forever, silently and greenly — so do it only for a corpus known to be immutable, and only to save the one extra read. The(size, mtime)memo onSourceRevalidatoralready means that read is paid once per file per process, not once per call.
Members
fetch— Return aCacheEntryforresolved_url, fetching if needed.
Example (fetch)
cache = Cache(root=cache_root, offline=False)
entry = cache.fetch(URL, source_loader="stations")
assert entry.status == "downloaded"
assert entry.path.read_bytes() == (FIXTURES / "stations.csv").read_bytes()
# The blob is addressed by the SHA-256 of the resolved URL.
assert entry.key == earthsciio.cache_key(URL)
# Asking again is a cache hit: no transfer happens.
again = cache.fetch(URL, source_loader="stations")
assert again.status == "hit" and again.path == entry.path
Julia
struct Cache
Cache(store::Store, offline::Bool, auth, verify::Bool, revalidate_file::Bool, revalidator::EarthSciIO.SourceRevalidator)
Cache(store, offline, auth, verify, revalidate_file, revalidator)
Cache(store::Store; offline, auth, verify, revalidate_file)
Cache(; store, root, kwargs...)
Cache(store::Store; offline=nothing, auth=nothing, verify=false, revalidate_file=nothing)
Cache(; store="local", root=datadir(), offline=nothing, auth=nothing, verify=false,
revalidate_file=nothing)
The content-addressed cache. offline=nothing reads EARTHSCI_OFFLINE from the
environment; an explicit offline argument wins. auth is a resolver or a
realm→resolver map. verify=true re-checks sha256_content on every read (off
by default, on for CI/conformance) — that is the CACHED BLOB against its own
manifest, cache-internal consistency, NOT the file:// source recheck.
revalidate_file=nothing reads EARTHSCI_REVALIDATE_FILE and leaves the
file:// source recheck ON unless the knob is an explicit denial (spec §4.1).
Turning it off restores the behaviour of EarthSciML/EarthSciAST#293: a local file
replaced in place is served from the warm entry forever, silently and greenly. Do
it only for a corpus known to be immutable, and only to save the one extra
read.
Example (fetch)
cache = Cache(; root = CACHE_ROOT, offline = false)
entry = fetch_blob(cache, URL; source_loader = "stations")
@assert entry isa CacheEntry
@assert entry.status == :downloaded
@assert read(entry.path) == read(joinpath(FIXTURES, "stations.csv"))
# The blob is addressed by the SHA-256 of the resolved URL.
@assert entry.key == cache_key(URL)
# Asking again is a cache hit: no transfer happens.
again = fetch_blob(cache, URL; source_loader = "stations")
@assert again.status == :hit && again.path == entry.path
Rust
pub struct Cache {
/* private fields */
}
impl Cache { pub fn builder() -> CacheBuilder }
impl Cache { pub fn from_env() -> Result<Cache> }
impl Cache { pub fn is_offline(&self) -> bool }
impl Cache { pub fn store(&self) -> &Arc<dyn Store> }
impl Cache { pub fn fetch(&self, req: &FetchRequest<'_>) -> Result<CachedBlob> }
impl Cache { pub fn get_offline(&self, resolved_url: &str) -> Result<CachedBlob> }
The cache fetcher. Cheap to clone the registries it holds; share one across
threads behind an Arc.
Example (fetch)
let cache = Cache::builder()
.data_dir(cache_root.path())
.offline(false)
.build()?;
let blob = cache.fetch(&FetchRequest::new(&url).loader("stations"))?;
assert_eq!(std::fs::read(&blob.path)?, std::fs::read(&source)?);
// The blob is addressed by the SHA-256 of the resolved URL.
assert_eq!(blob.key, cache_key(&url));
// Asking again returns the same blob without a transfer.
let again = cache.fetch(&FetchRequest::new(&url))?;
assert_eq!(again.path, blob.path);