Skip to main content

Module cache

Module cache 

Source
Expand description

Opt-in full-document render cache (cache feature).

CachingProcessor owns one MarkdownOptions and memoises the output of process_markdown and process_markdown_with_toc per input document. Because the options are fixed when the processor is built, the cache key only has to identify the content: build one processor per option set.

§Keying and correctness

Entries are looked up by a 64-bit hash of the input, then the stored input is compared byte for byte before a hit is returned. A hash collision therefore costs a re-render, never a wrong answer. The cache holds a copy of every cached input alongside its HTML, so memory is roughly capacity x (input + output).

§Eviction

The cache is bounded by entry count (DEFAULT_CAPACITY unless set with CachingProcessor::with_capacity). When full, the least recently used entry is evicted. Eviction scans the entries, which is negligible next to a Markdown render at the default bound.

§Concurrency

CachingProcessor is Send + Sync and can be shared behind an Arc. The map sits behind one std::sync::Mutex held only for lookup and insertion; rendering happens outside the lock, so threads rendering different documents do not serialise on each other. Two threads that miss on the same document at the same time both render it and the second insert wins, which is harmless because the outputs are identical. A poisoned lock is recovered rather than propagated: the map holds only finished renders, so a panic elsewhere cannot leave it half-updated.

Hits and misses are reported through the log crate at debug level, and counted in CacheStats.

Structs§

CacheStats
Hit and miss counters for a CachingProcessor.
CachingProcessor
A Markdown processor that caches rendered output per document.

Constants§

DEFAULT_CAPACITY
Default number of documents a CachingProcessor keeps.