| | | 1 | | using Intentum.AI.Embeddings; |
| | | 2 | | using Microsoft.Extensions.Caching.Memory; |
| | | 3 | | |
| | | 4 | | namespace Intentum.AI.Caching; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Memory-based cache implementation for embeddings using IMemoryCache. |
| | | 8 | | /// </summary> |
| | | 9 | | public 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) |
| | 5 | 18 | | : this(memoryCache, new MemoryCacheEntryOptions |
| | 5 | 19 | | { |
| | 5 | 20 | | AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1), |
| | 5 | 21 | | SlidingExpiration = TimeSpan.FromMinutes(30) |
| | 5 | 22 | | }) |
| | 5 | 23 | | { |
| | 5 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Creates a memory embedding cache with custom cache entry options. |
| | | 28 | | /// </summary> |
| | 6 | 29 | | public MemoryEmbeddingCache(IMemoryCache memoryCache, MemoryCacheEntryOptions? defaultOptions) |
| | 6 | 30 | | { |
| | 6 | 31 | | _memoryCache = memoryCache ?? throw new ArgumentNullException(nameof(memoryCache)); |
| | 6 | 32 | | _defaultOptions = defaultOptions; |
| | 6 | 33 | | } |
| | | 34 | | |
| | | 35 | | public IntentEmbedding? Get(string behaviorKey) |
| | 10 | 36 | | { |
| | 10 | 37 | | return _memoryCache.TryGetValue<IntentEmbedding>(GetCacheKey(behaviorKey), out var embedding) |
| | 10 | 38 | | ? embedding |
| | 10 | 39 | | : null; |
| | 10 | 40 | | } |
| | | 41 | | |
| | | 42 | | public void Set(string behaviorKey, IntentEmbedding embedding) |
| | 6 | 43 | | { |
| | 6 | 44 | | var key = GetCacheKey(behaviorKey); |
| | 6 | 45 | | if (_defaultOptions != null) |
| | 6 | 46 | | { |
| | 6 | 47 | | _memoryCache.Set(key, embedding, _defaultOptions); |
| | 6 | 48 | | } |
| | | 49 | | else |
| | 0 | 50 | | { |
| | 0 | 51 | | _memoryCache.Set(key, embedding); |
| | 0 | 52 | | } |
| | 6 | 53 | | } |
| | | 54 | | |
| | | 55 | | public void Remove(string behaviorKey) |
| | 1 | 56 | | { |
| | 1 | 57 | | _memoryCache.Remove(GetCacheKey(behaviorKey)); |
| | 1 | 58 | | } |
| | | 59 | | |
| | | 60 | | public void Clear() |
| | 0 | 61 | | { |
| | | 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 |
| | 0 | 65 | | 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) |
| | 17 | 69 | | { |
| | 17 | 70 | | return $"Intentum:Embedding:{behaviorKey}"; |
| | 17 | 71 | | } |
| | | 72 | | } |