mongoose.core.cache
- class mongoose.core.cache.Cache(*args, **kwargs)[source]
Bases:
Generic[K,V]Sharded LRU cache with optional TTL implemented as a singleton per class.
The cache partitions keys into a fixed number of shards. Each shard maintains its own OrderedDict and lock so operations for different shards can proceed concurrently without contending on a single global lock. Each shard enforces its own capacity (approximately max_size / num_shards), so the global max_size is a soft bound but should be close when keys distribute evenly.
This design provides better concurrency for high-contention workloads while preserving LRU semantics within each shard.
- Return type:
- get(key)[source]
Retrieve a key from its shard and mark it as recently used.
Returns None if missing or expired.
- Return type:
V | None
- class mongoose.core.cache.SeverityCache(*args, **kwargs)[source]
-
Specialization of Cache for community_id -> severity mappings.
Provides convenience methods set_severity and get_severity that validate input types and keep the simple, explicit API used by the rest of the codebase.
- Return type:
- get_severity(community_id)[source]
Retrieve the cached severity for a community_id or None if missing.
- class mongoose.core.cache.SingletonMeta[source]
Bases:
typeThread-safe metaclass implementing a per-class singleton.
Each class that uses this metaclass will only ever have a single instance created. The first construction’s args/kwargs are used to initialize the singleton; later constructions return the same instance and ignore new args.
- mongoose.core.cache.reset_singletons()[source]
Clear the internal singleton instance registry.
This is primarily intended for use in unit tests so different tests can instantiate singletons with different constructor parameters and get fresh instances. Use with care in production code as clearing singletons while other threads hold references can be unsafe.