< Summary

Information
Class: Intentum.AI.Caching.MemoryEmbeddingCache
Assembly: Intentum.AI
File(s): /home/runner/work/Intentum/Intentum/src/Intentum.AI/Caching/MemoryEmbeddingCache.cs
Line coverage
85%
Covered lines: 30
Uncovered lines: 5
Coverable lines: 35
Total lines: 72
Line coverage: 85.7%
Branch coverage
66%
Covered branches: 4
Total branches: 6
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
.ctor(...)50%22100%
Get(...)100%22100%
Set(...)50%2270%
Remove(...)100%11100%
Clear()100%110%
GetCacheKey(...)100%11100%

File(s)

/home/runner/work/Intentum/Intentum/src/Intentum.AI/Caching/MemoryEmbeddingCache.cs

#LineLine coverage
 1using Intentum.AI.Embeddings;
 2using Microsoft.Extensions.Caching.Memory;
 3
 4namespace Intentum.AI.Caching;
 5
 6/// <summary>
 7/// Memory-based cache implementation for embeddings using IMemoryCache.
 8/// </summary>
 9public sealed class MemoryEmbeddingCache : IEmbeddingCache
 10{
 11    private readonly IMemoryCache _memoryCache;
 12    private readonly MemoryCacheEntryOptions? _defaultOptions;
 13
 14    /// <summary>
 15    /// Creates a memory embedding cache with default options (1 hour expiration).
 16    /// </summary>
 17    public MemoryEmbeddingCache(IMemoryCache memoryCache)
 518        : this(memoryCache, new MemoryCacheEntryOptions
 519        {
 520            AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1),
 521            SlidingExpiration = TimeSpan.FromMinutes(30)
 522        })
 523    {
 524    }
 25
 26    /// <summary>
 27    /// Creates a memory embedding cache with custom cache entry options.
 28    /// </summary>
 629    public MemoryEmbeddingCache(IMemoryCache memoryCache, MemoryCacheEntryOptions? defaultOptions)
 630    {
 631        _memoryCache = memoryCache ?? throw new ArgumentNullException(nameof(memoryCache));
 632        _defaultOptions = defaultOptions;
 633    }
 34
 35    public IntentEmbedding? Get(string behaviorKey)
 1036    {
 1037        return _memoryCache.TryGetValue<IntentEmbedding>(GetCacheKey(behaviorKey), out var embedding)
 1038            ? embedding
 1039            : null;
 1040    }
 41
 42    public void Set(string behaviorKey, IntentEmbedding embedding)
 643    {
 644        var key = GetCacheKey(behaviorKey);
 645        if (_defaultOptions != null)
 646        {
 647            _memoryCache.Set(key, embedding, _defaultOptions);
 648        }
 49        else
 050        {
 051            _memoryCache.Set(key, embedding);
 052        }
 653    }
 54
 55    public void Remove(string behaviorKey)
 156    {
 157        _memoryCache.Remove(GetCacheKey(behaviorKey));
 158    }
 59
 60    public void Clear()
 061    {
 62        // IMemoryCache doesn't have a Clear method, so we need to track keys
 63        // For simplicity, this implementation doesn't support Clear
 64        // Users can create a new cache instance or use a custom implementation
 065        throw new NotSupportedException("Clear is not supported by IMemoryCache. Create a new cache instance or use a cu
 66    }
 67
 68    private static string GetCacheKey(string behaviorKey)
 1769    {
 1770        return $"Intentum:Embedding:{behaviorKey}";
 1771    }
 72}