ddn.util.monaco.cache

TTL-based caching mechanism for performance optimization.

This module provides a generic, thread-safe cache implementation with time-to-live (TTL) support. It is designed to reduce the overhead of frequently accessing system information that doesn't change rapidly.

Types 7

classCacheException : Exception

Exception thrown when cache operations fail.

Constructors
this(string msg, string file = __FILE__, size_t line = __LINE__)
structCacheEntry(T)

Represents a cached entry with its expiration time.

Fields
T valueThe cached value
SysTime createdAtWhen this entry was created
SysTime expiresAtWhen this entry expires
Methods
bool isExpired() @safeChecks if this entry has expired.
Duration remainingTime() @safeReturns the remaining time before this entry expires.

Statistics about cache usage.

Fields
ulong hitsNumber of successful cache hits
ulong missesNumber of cache misses
ulong evictionsNumber of entries evicted due to expiration
size_t sizeNumber of entries currently in the cache
ulong totalStoredTotal number of entries ever stored
Methods
double hitRate() @safe pure nothrowReturns the cache hit rate as a percentage.
void reset() @safe pure nothrowResets all statistics to zero.

Configuration options for the cache.

Fields
Duration defaultTtlDefault TTL for cache entries (1 second by default)
size_t maxSizeMaximum number of entries in the cache (0 = unlimited)
bool autoCleanupWhether to automatically cleanup expired entries on access
Duration cleanupIntervalInterval for automatic cleanup (0 = disabled)
classCache(T)

A thread-safe, generic cache with TTL support.

This cache implementation stores values with associated time-to-live (TTL) values, automatically expiring entries when they become stale. It is designed to reduce the overhead of frequently querying system information.

Example:

auto cache = new Cache!CpuStats(dur!"seconds"(2));
cache.set("cpu", cpuStats());
auto stats = cache.get!("cpu");

Fields
private CacheEntry!T[string] entries
private Mutex mutex
private CacheStats stats
private CacheConfig config
private SysTime lastCleanup
Methods
void set(string key, T value) @safeStores a value in the cache with the default TTL.
void set(string key, T value, Duration ttl) @safeStores a value in the cache with a custom TTL.
Tuple!(bool, "found", T, "value") get(string key) @safeRetrieves a value from the cache.
T getOrCompute(string key, T delegate() @safe compute, Duration ttl = Duration.zero) @safeGets a value from the cache, or computes and stores it if not present.
bool contains(string key) @safeChecks if a key exists in the cache and is not expired.
bool remove(string key) @safeRemoves a specific entry from the cache.
void clear() @safeClears all entries from the cache.
size_t cleanupExpired() @safeRemoves all expired entries from the cache.
size_t length() @safeReturns the current number of entries in the cache.
CacheStats statistics() @safeReturns cache statistics.
void resetStatistics() @safeResets cache statistics.
CacheConfig configuration() @safe pure nothrowReturns the configuration for this cache.
Constructors
this(Duration defaultTtl = dur!"seconds"(1), size_t maxSize = 0)Creates a new cache with the specified default TTL.
this(CacheConfig cfg)Creates a new cache with the specified configuration.

Global cache manager for system monitoring data.

This singleton class manages multiple caches for different types of system information, each with appropriate TTL values.

Fields
private CacheManager instance
private Mutex instanceMutex
private Cache!ulong ulongCache
private Cache!double doubleCache
private Cache!string stringCache
private Mutex mutex
Methods
CacheManager getInstance() @safeGets the singleton instance of the cache manager.
Cache!ulong getUlongCache() @safe nothrowGets the ulong cache.
Cache!double getDoubleCache() @safe nothrowGets the double cache.
Cache!string getStringCache() @safe nothrowGets the string cache.
void clearAll() @safeClears all caches.
size_t cleanupAll() @safeCleans up all expired entries in all caches.
void resetAllStatistics() @safeResets statistics for all caches.
Constructors

RAII helper for timing cache operations.

Fields
private SysTime start
private string operation
Methods
Duration elapsed() @safeReturns the elapsed time since construction.
Constructors
this(string op)Starts timing an operation.