English
cache
The cache file (the cache section, in sing-box — experimental.cache_file) in its own format: path is a directory of journals, not a bbolt database. The owner of the state keeps it in memory and appends it to disk in the background, so a response never waits for the disk.
| Crate | ours-mod-cache, modules/system/cache |
| Binary feature | the module is always in the list; cache → ours-mod-cache/persistence |
| Crate features | persistence (crc32fast, rustix, tracing) |
requires
Nothing. It is required by router (remote rule-sets), dns (responses, RDRC, FakeIP), group (the saved choice), subscriptions (the directory its saved downloads live in). None of them depends on the crate: they take the cache file through the contract, ours_api::cache.
provides
Provide::Unit(Cache)— during the build it opens aCacheFileaccording to the options; if the options did not change, it takes the one already open (reload does not reopen the file); withBuild::checking(thecheckcommand) it does not open the file. It publishes the file asArc<dyn ours_api::cache::Cache>(ours_api::cache::publish; consumers take it throughours_api::cache::of, that is, the cache is optional).commitremembers the current file,closeflushes the journals.Provide::Section { key: "cache" }— the whole section (ours_config::options::CacheFileOptions, kindCache), enabled by presence. The old nameexperimental.cache_fileis rewritten tocacheby the loader (crates/native-config/src/version.rs), the module does not see it.Provide::Schema { name: "cache" }.
ours_config::options::file_options(sections) — the file options before the instance starts (subscriptions read what they saved earlier).
Syntax: cache.
Dependencies
ours-journal (a journal of records with checksums), ours-api, ours-config, ours-codec, ours-json.
Design
src/module.rs—MODULE, theCacheunit, parsing the section.src/enabled.rs—CacheFile, theCacheof the contract (withpersistence). The directory's contents:lock(held while the process uses the directory),fakeip/<server>(shared by allcache_ids, as in sing-box),<scope>/dns,<scope>/rdrc,<scope>/rule-set/<tag>, where<scope>isdefaultor thecache_id;subscriptionskeeps<scope>/subscription/<name>there itself.src/preferences.rs— the process state (choices in groups, the mode) in a journal, percache_id.src/disabled.rs— a build withoutpersistence: uninhabited handles keep the interface.src/options.rs— the section's schema.
Tests
modules/system/cache/tests/preferences.rs (cache_id scopes, hand-over on reload, recovery after a torn journal tail); unit tests in enabled.rs (among them files written before the contract), disabled.rs.