Class CachingCompressor
- Namespace
- AiDotNet.PromptEngineering.Compression
- Assembly
- AiDotNet.dll
Wrapper compressor that caches compression results for frequently used prompts.
public class CachingCompressor : PromptCompressorBase, IPromptCompressor
- Inheritance
-
CachingCompressor
- Implements
- Inherited Members
Remarks
This compressor wraps another compressor and caches the results. When the same prompt is compressed multiple times, the cached result is returned instead of re-computing the compression. This is particularly useful for LLM-based compressors where each compression call is expensive.
For Beginners: Remembers compressions so the same prompt doesn't need to be compressed twice.
Example:
var llmCompressor = new LLMSummarizationCompressor(myLlmFunc);
var cachingCompressor = new CachingCompressor(llmCompressor, maxCacheSize: 1000);
// First call - compresses using LLM (slow)
var result1 = await cachingCompressor.CompressAsync(prompt);
// Second call with same prompt - returns cached result (fast)
var result2 = await cachingCompressor.CompressAsync(prompt);
// result1 == result2, but second call was instant
When to use:
- Wrapping expensive compressors (LLM-based)
- When the same prompts are used repeatedly
- In production systems to reduce API calls
Constructors
CachingCompressor(IPromptCompressor, int, TimeSpan?, Func<string, int>?)
Initializes a new instance of the CachingCompressor class.
public CachingCompressor(IPromptCompressor innerCompressor, int maxCacheSize = 1000, TimeSpan? cacheExpiry = null, Func<string, int>? tokenCounter = null)
Parameters
innerCompressorIPromptCompressorThe compressor to wrap with caching.
maxCacheSizeintMaximum number of entries in the cache.
cacheExpiryTimeSpan?How long cached entries remain valid.
tokenCounterFunc<string, int>Optional custom token counter function.
Properties
CacheCount
Gets the number of items currently in the cache.
public int CacheCount { get; }
Property Value
CacheHitRatio
Gets the cache hit ratio (hits / total requests).
public double CacheHitRatio { get; }
Property Value
Methods
ClearCache()
Clears all entries from the cache.
public void ClearCache()
CompressAsync(string, CompressionOptions?, CancellationToken)
Compresses the prompt asynchronously, using cache if available.
public override Task<string> CompressAsync(string prompt, CompressionOptions? options = null, CancellationToken cancellationToken = default)
Parameters
promptstringoptionsCompressionOptionscancellationTokenCancellationToken
Returns
CompressCore(string, CompressionOptions)
Compresses the prompt, using cache if available.
protected override string CompressCore(string prompt, CompressionOptions options)
Parameters
promptstringoptionsCompressionOptions
Returns
GetCacheStats()
Gets statistics about the cache.
public Dictionary<string, object> GetCacheStats()
Returns
- Dictionary<string, object>
A dictionary with cache statistics.
PurgeExpiredEntries()
Removes expired entries from the cache.
public int PurgeExpiredEntries()
Returns
- int
The number of entries removed.