| | | 1 | | using System.Text.Json; |
| | | 2 | | using Intentum.AI.Embeddings; |
| | | 3 | | using Microsoft.Extensions.Caching.Distributed; |
| | | 4 | | |
| | | 5 | | namespace Intentum.AI.Caching.Redis; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Redis-backed distributed cache implementation for embeddings using IDistributedCache. |
| | | 9 | | /// Suitable for multi-node production; use MemoryEmbeddingCache for single-node or development. |
| | | 10 | | /// </summary> |
| | | 11 | | public sealed class RedisEmbeddingCache : IEmbeddingCache |
| | | 12 | | { |
| | | 13 | | private const string KeyPrefix = "Intentum:Embedding:"; |
| | 1 | 14 | | private static readonly JsonSerializerOptions JsonOptions = new() |
| | 1 | 15 | | { |
| | 1 | 16 | | PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| | 1 | 17 | | WriteIndented = false |
| | 1 | 18 | | }; |
| | | 19 | | |
| | | 20 | | private readonly IDistributedCache _cache; |
| | | 21 | | private readonly DistributedCacheEntryOptions _defaultOptions; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Creates a Redis embedding cache with default expiration (24 hours). |
| | | 25 | | /// </summary> |
| | | 26 | | public RedisEmbeddingCache(IDistributedCache cache) |
| | 3 | 27 | | : this(cache, new DistributedCacheEntryOptions |
| | 3 | 28 | | { |
| | 3 | 29 | | AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(24) |
| | 3 | 30 | | }) |
| | 3 | 31 | | { |
| | 3 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Creates a Redis embedding cache with custom cache entry options. |
| | | 36 | | /// </summary> |
| | 3 | 37 | | public RedisEmbeddingCache(IDistributedCache cache, DistributedCacheEntryOptions? defaultOptions) |
| | 3 | 38 | | { |
| | 3 | 39 | | _cache = cache ?? throw new ArgumentNullException(nameof(cache)); |
| | 3 | 40 | | _defaultOptions = defaultOptions ?? new DistributedCacheEntryOptions |
| | 3 | 41 | | { |
| | 3 | 42 | | AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(24) |
| | 3 | 43 | | }; |
| | 3 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc /> |
| | | 47 | | public IntentEmbedding? Get(string behaviorKey) |
| | 3 | 48 | | { |
| | 3 | 49 | | var key = GetCacheKey(behaviorKey); |
| | 3 | 50 | | var bytes = _cache.Get(key); |
| | 3 | 51 | | if (bytes is null || bytes.Length == 0) |
| | 2 | 52 | | return null; |
| | | 53 | | |
| | 1 | 54 | | return Deserialize(bytes); |
| | 3 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | | 58 | | public void Set(string behaviorKey, IntentEmbedding embedding) |
| | 2 | 59 | | { |
| | 2 | 60 | | var key = GetCacheKey(behaviorKey); |
| | 2 | 61 | | var bytes = Serialize(embedding); |
| | 2 | 62 | | _cache.Set(key, bytes, _defaultOptions); |
| | 2 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <inheritdoc /> |
| | | 66 | | public void Remove(string behaviorKey) |
| | 1 | 67 | | { |
| | 1 | 68 | | var key = GetCacheKey(behaviorKey); |
| | 1 | 69 | | _cache.Remove(key); |
| | 1 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <inheritdoc /> |
| | | 73 | | public void Clear() |
| | 0 | 74 | | { |
| | | 75 | | // IDistributedCache has no global Clear; callers can use a dedicated Redis key prefix and FLUSHDB in Redis, or |
| | 0 | 76 | | throw new NotSupportedException( |
| | 0 | 77 | | "Clear is not supported by Redis embedding cache. Use Redis FLUSHDB for the database or remove keys by prefi |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | private static string GetCacheKey(string behaviorKey) |
| | 6 | 81 | | { |
| | 6 | 82 | | return KeyPrefix + behaviorKey; |
| | 6 | 83 | | } |
| | | 84 | | |
| | | 85 | | private static byte[] Serialize(IntentEmbedding embedding) |
| | 2 | 86 | | { |
| | 2 | 87 | | var dto = new EmbeddingDto(embedding.Source, embedding.Score, embedding.Vector?.ToList()); |
| | 2 | 88 | | return JsonSerializer.SerializeToUtf8Bytes(dto, JsonOptions); |
| | 2 | 89 | | } |
| | | 90 | | |
| | | 91 | | private static IntentEmbedding? Deserialize(byte[] bytes) |
| | 1 | 92 | | { |
| | 1 | 93 | | var dto = JsonSerializer.Deserialize<EmbeddingDto>(bytes, JsonOptions); |
| | 1 | 94 | | if (dto is null) |
| | 0 | 95 | | return null; |
| | 1 | 96 | | return new IntentEmbedding(dto.Source, dto.Score, dto.Vector); |
| | 1 | 97 | | } |
| | | 98 | | |
| | 12 | 99 | | private sealed record EmbeddingDto(string Source, double Score, List<double>? Vector); |
| | | 100 | | } |