Module cache

Module cache 

Source
Expand description

Descriptor caching for improved performance.

This module provides in-memory caching of Tor descriptors to avoid repeated downloads from the Tor process. Caching significantly improves performance for applications that frequently query descriptor information.

§Overview

The descriptor cache stores parsed descriptors with automatic expiration based on their validity periods. Different descriptor types have different cache lifetimes:

  • Consensus documents: 3 hours (typical validity period)
  • Server descriptors: 24 hours (published daily)
  • Microdescriptors: 24 hours (referenced by consensus)

§Thread Safety

The cache is thread-safe and can be shared across multiple tasks using Arc<DescriptorCache>. All operations use interior mutability with RwLock for concurrent access.

§Example

use stem_rs::descriptor::cache::DescriptorCache;
use std::time::Duration;

let cache = DescriptorCache::new()
    .with_consensus_ttl(Duration::from_secs(3 * 3600))
    .with_max_entries(1000);

// Cache is automatically used by Controller methods
// when enabled via Controller::with_descriptor_cache()

§Memory Management

The cache automatically evicts expired entries and enforces a maximum entry limit to prevent unbounded memory growth. When the limit is reached, the least recently used entries are evicted.

Structs§

CacheStats
Statistics about cache performance.
DescriptorCache
In-memory cache for Tor descriptors.