cache_entry
Stable type · Cache
Tier: stable · Kind: type · Topic: Cache
| Python | Julia | Rust |
|---|---|---|
CacheEntry | CacheEntry | CachedBlob |
Python
class CacheEntry
CacheEntry(key: str, path: pathlib.Path, manifest: Optional[Manifest], status: str)
The result of a fetch: where the blob is + how it got there.
status is "hit" (served from cache, no network), "downloaded" (a
fresh GET), or "not_modified" (a conditional GET returned 304 and the
cached blob was reused). manifest may be None only for an offline hit
against a blob whose sidecar manifest is absent.
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 CacheEntry
CacheEntry(key::String, path::String, manifest::Union{Nothing, Manifest}, status::Symbol)
CacheEntry(key, path, manifest, status)
The outcome of [fetch_blob]: the resolved blob plus how it was obtained.
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 CachedBlob {
pub key: String,
pub path: PathBuf,
pub manifest: Manifest,
}
A resolved cache entry: the blob path plus its manifest.
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);