Expand description
A cache of compiled files: what is read from a file at run time and costly to compile (a YAML token file, say) is compiled once and kept, never next to its source.
The store is not specific to tokens. An entry is the compiled form of one source file, made by one compiler; it is keyed by the source’s path, a hash of its contents and the compiler’s version:
- a hit (same path, same contents, same compiler version) gives the compiled bytes back;
- a miss or a stale entry (the source changed, or the compiler is another version) gives nothing: the caller compiles the source and puts the result, which replaces the stale entry.
Nothing here is fatal (ADR 0003): an entry that cannot be read is a miss, and one that cannot be written is an error the caller reports while going on with what it compiled.
The entries live under <dir>/<compiler>/, one file per source path.
The directory is the platform’s cache directory under the
application’s id (Cache::for_app, platform_dir):
| Cache | |
|---|---|
| Linux and BSD | $XDG_CACHE_HOME/<app>/egui-pop (~/.cache/<app>/egui-pop) |
| macOS | ~/Library/Caches/<app>/egui-pop |
| Windows | %LOCALAPPDATA%\<app>\cache\egui-pop |
egui-pop does not know the application’s id, so the cache is off
until the application gives one: set_default at start-up for every
file read at run time (KitData::load,
Themes::add_path), or a Cache passed
where a reader takes one. Cache::at puts it elsewhere (a test’s
temporary directory), Cache::off turns it off.
use egui_pop_tokens::cache::{Cache, Compiler};
let dir = std::env::temp_dir().join(format!("egui-pop-cache-doc-{}", std::process::id()));
let cache = Cache::at(&dir);
let upper = Compiler { name: "upper", version: "1" };
let source = dir.join("notes.txt");
assert_eq!(cache.get(&upper, &source, b"hello"), None);
cache.put(&upper, &source, b"hello", b"HELLO").unwrap();
assert_eq!(cache.get(&upper, &source, b"hello").as_deref(), Some(&b"HELLO"[..]));
// The source changed: stale.
assert_eq!(cache.get(&upper, &source, b"hello!"), None);Structs§
- Cache
- Where compiled files are kept, or nowhere (off).
- Cache
Error - An entry that could not be written: the source was compiled all the same, and is compiled again next time.
- Compiler
- What made an entry: its name (a folder under the cache, so letters,
digits,
-,_and.) and its version. An entry made by another version is stale. A compiler raises its version whenever what it writes changes.
Functions§
- default
- The cache the readers taking none use: what
set_defaultset, off before. - platform_
dir - The cache directory for an application (see the module).
- platform_
dir_ with - The cache directory for an application, read from the environment
envgives (see the module); none when the variables it needs are missing. - set_
default - Sets the cache the readers taking none use, for the whole process
(an application calls it once at start-up, with
Cache::for_app).