Cache Management
Typst uses comemo, a memoized function cache. This significantly speeds up repeated compilations.
How Cache Eviction Works
Section titled “How Cache Eviction Works”The comemo cache is global and shared across all template instances. To prevent unbounded memory growth, Oicana provides configurable cache eviction based on an aging mechanism:
- Each cache entry has an age counter
- Age increases by 1 during each eviction call
- Age resets to 0 when the entry is accessed (during a template compilation)
- Entries with age ≥ max_age are removed when running cache eviction
Default Behavior
Section titled “Default Behavior”By default, Oicana integrations automatically evict the cache after each compilation with a maximum age of 10.
Configuring Cache Eviction
Section titled “Configuring Cache Eviction”All integrations provide two APIs for cache management: one to configure or disable automatic eviction after each compilation, and one to manually trigger cache eviction with a specific age threshold.
| Integration | Configure automatic eviction | Manual eviction |
|---|---|---|
| Browser / Node.js | configureAutomaticCacheEviction(maxAge) |
evictCache(maxAge) |
| C# | Configuration.ConfigureAutomaticCacheEviction(maxAge) |
Template.EvictCache(maxAge) |
| Java | Template.configureAutomaticCacheEviction(maxAge) |
Template.evictCache(maxAge) |
| PHP | Template::configureAutomaticCacheEviction($maxAge) |
Template::evictCache($maxAge) |
| Python | oicana.configure_automatic_cache_eviction(max_age) |
oicana.evict_cache(max_age) |
| Rust | oicana::configure_automatic_cache_eviction(max_age) |
oicana::evict_cache(max_age) |
maxAge is the age threshold described above: 0 clears the whole cache after every compilation, n keeps entries used within the last n evictions. The default is 10.
Consider adjusting the default cache settings to a higher maximum entry age if you have a large number of templates and enough available memory to support a larger cache.
Disabling automatic eviction
Section titled “Disabling automatic eviction”You can also turn automatic eviction off. How exactly depends on the language:
| Integration | Disable automatic eviction |
|---|---|
| Browser / Node.js | configureAutomaticCacheEviction(null) |
| C# | Configuration.ConfigureAutomaticCacheEviction(-1) |
| Java | Template.configureAutomaticCacheEviction(-1) |
| PHP | Template::configureAutomaticCacheEviction(null) |
| Python | oicana.configure_automatic_cache_eviction(None) |
| Rust | oicana::configure_automatic_cache_eviction(None) |
The cache is then never cleared until you call the manual eviction API.
Sometimes, it makes sense to disable automatic cache eviction and only run it manually. For example, during large batch compilations, you can disable automatic eviction and only evict between batches for better performance. The default should work well in most scenarios, but you can experiment with different approaches for fine-tuning.