Skip to content

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.

Crateours-mod-cache, modules/system/cache
Binary featurethe module is always in the list; cache → ours-mod-cache/persistence
Crate featurespersistence (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 a CacheFile according to the options; if the options did not change, it takes the one already open (reload does not reopen the file); with Build::checking (the check command) it does not open the file. It publishes the file as Arc<dyn ours_api::cache::Cache> (ours_api::cache::publish; consumers take it through ours_api::cache::of, that is, the cache is optional). commit remembers the current file, close flushes the journals.
  • Provide::Section { key: "cache" } — the whole section (ours_config::options::CacheFileOptions, kind Cache), enabled by presence. The old name experimental.cache_file is rewritten to cache by 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, the Cache unit, parsing the section.
  • src/enabled.rs — CacheFile, the Cache of the contract (with persistence). The directory's contents: lock (held while the process uses the directory), fakeip/<server> (shared by all cache_ids, as in sing-box), <scope>/dns, <scope>/rdrc, <scope>/rule-set/<tag>, where <scope> is default or the cache_id; subscriptions keeps <scope>/subscription/<name> there itself.
  • src/preferences.rs — the process state (choices in groups, the mode) in a journal, per cache_id.
  • src/disabled.rs — a build without persistence: 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.

GPL-3.0-or-later