| | | 1 | | using JetBrains.Annotations; |
| | | 2 | | using Microsoft.Extensions.Caching.Distributed; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection; |
| | | 4 | | |
| | | 5 | | namespace Intentum.AI.Caching.Redis; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Extension methods for registering Redis embedding cache with dependency injection. |
| | | 9 | | /// </summary> |
| | | 10 | | [UsedImplicitly] |
| | | 11 | | public static class RedisCachingExtensions |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Adds Redis-backed distributed embedding cache to the service collection. |
| | | 15 | | /// Configures StackExchangeRedisCache and registers RedisEmbeddingCache as IEmbeddingCache. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="services">The service collection.</param> |
| | | 18 | | /// <param name="configure">Action to configure Redis cache options (connection string, instance name, etc.).</param |
| | | 19 | | /// <returns>The service collection for chaining.</returns> |
| | | 20 | | [UsedImplicitly] |
| | | 21 | | public static IServiceCollection AddIntentumRedisCache( |
| | | 22 | | this IServiceCollection services, |
| | | 23 | | Action<IntentumRedisCacheOptions>? configure = null) |
| | 0 | 24 | | { |
| | 0 | 25 | | var options = new IntentumRedisCacheOptions(); |
| | 0 | 26 | | configure?.Invoke(options); |
| | | 27 | | |
| | 0 | 28 | | services.AddStackExchangeRedisCache(redisOptions => |
| | 0 | 29 | | { |
| | 0 | 30 | | redisOptions.Configuration = options.ConnectionString; |
| | 0 | 31 | | redisOptions.InstanceName = options.InstanceName ?? "Intentum:"; |
| | 0 | 32 | | }); |
| | | 33 | | |
| | 0 | 34 | | services.AddSingleton<IEmbeddingCache>(sp => |
| | 0 | 35 | | { |
| | 0 | 36 | | var cache = sp.GetRequiredService<IDistributedCache>(); |
| | 0 | 37 | | var entryOptions = new DistributedCacheEntryOptions |
| | 0 | 38 | | { |
| | 0 | 39 | | AbsoluteExpirationRelativeToNow = options.DefaultExpiration |
| | 0 | 40 | | }; |
| | 0 | 41 | | return new RedisEmbeddingCache(cache, entryOptions); |
| | 0 | 42 | | }); |
| | | 43 | | |
| | 0 | 44 | | return services; |
| | 0 | 45 | | } |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Options for Intentum Redis embedding cache. |
| | | 50 | | /// </summary> |
| | | 51 | | public sealed class IntentumRedisCacheOptions |
| | | 52 | | { |
| | | 53 | | /// <summary> |
| | | 54 | | /// Redis connection string (e.g. "localhost:6379" or "contoso.redis.cache.windows.net:6380,password=..."). |
| | | 55 | | /// </summary> |
| | | 56 | | public string ConnectionString { get; set; } = "localhost:6379"; |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Optional instance name prefix for keys (default "Intentum:"). |
| | | 60 | | /// </summary> |
| | | 61 | | public string? InstanceName { get; set; } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Default expiration for cached embeddings (default 24 hours). |
| | | 65 | | /// </summary> |
| | | 66 | | public TimeSpan DefaultExpiration { get; set; } = TimeSpan.FromHours(24); |
| | | 67 | | } |