diff --git a/Readme.md b/Readme.md index 66ce691..44ef36f 100644 --- a/Readme.md +++ b/Readme.md @@ -138,8 +138,9 @@ builder .AddMediatrWithBehaviors(AssemblyRegistry.ToArray()) .AddResilienceDefaultPipeline() .MapDefaultTimeZone() - .AddRedis(KeyPrefix.AssemblyNamePrefix) - .AddDistributedSignalR("DistributedSignalR") // or .AddSignalR() + .AddDistributedFusionCache("redis://localhost:6379", + builder.Environment.GetShortEnvironmentName() + "_" + "app_name") // or .AddFusionCache(...) + .AddDistributedSignalR("redis://localhost:6379","app_name:") // or .AddSignalR() .AddCors() .AddHealthChecks(); @@ -621,6 +622,7 @@ This package includes various extensions and utilities to aid development: retrieves DefaultTimeZone from `appsettings.json` and sets it as the default time zone. - **UrlBuilder:** A utility for building URLs with query parameters. - **Language ISO Code Helper:** Validate, query, and retrieve information about ISO language codes. +- **FusionCache Extensions:** Simplify the configuration of in memory or distributed caching with Redis. ### Related NuGet Packages @@ -628,7 +630,6 @@ This package includes various extensions and utilities to aid development: - **Pandatech.FluentMinimalApiMapper:** Simplifies mapping in minimal APIs. - **Pandatech.RegexBox:** A collection of useful regular expressions. - **Pandatech.ResponseCrafter:** A utility for crafting consistent API responses. -- **Pandatech.DistributedCache:** A distributed cache provider for Redis. ## License diff --git a/Shared.Kernel.Demo/Program.cs b/Shared.Kernel.Demo/Program.cs index 56a14fe..8687d18 100644 --- a/Shared.Kernel.Demo/Program.cs +++ b/Shared.Kernel.Demo/Program.cs @@ -1,3 +1,4 @@ +using DistributedCache.Options; using FluentMinimalApiMapper; using Microsoft.AspNetCore.Mvc; using SharedKernel.Demo2; @@ -25,8 +26,9 @@ .AddControllers(AssemblyRegistry.ToArray()) .AddMediatrWithBehaviors(AssemblyRegistry.ToArray()) .AddResilienceDefaultPipeline() - // .AddRedis(KeyPrefix.AssemblyNamePrefix) - //.AddDistributedSignalR("DistributedSignalR") // or .AddSignalR() + .AddDistributedFusionCache("redis://localhost:6379", + builder.Environment.GetShortEnvironmentName() + "_" + "app_name") // or .AddFusionCache(...) + .AddDistributedSignalR("redis://localhost:6379","app_name:") // or .AddSignalR() .MapDefaultTimeZone() .AddCors() .AddOutboundLoggingHandler() diff --git a/src/SharedKernel/Extensions/FusionCacheExtensions.cs b/src/SharedKernel/Extensions/FusionCacheExtensions.cs new file mode 100644 index 0000000..e669e7f --- /dev/null +++ b/src/SharedKernel/Extensions/FusionCacheExtensions.cs @@ -0,0 +1,42 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.Caching.StackExchangeRedis; +using Microsoft.Extensions.DependencyInjection; +using ZiggyCreatures.Caching.Fusion; + +namespace SharedKernel.Extensions; + +public static class FusionCacheExtensions +{ + private static IFusionCacheBuilder AddBaseFusionCache(WebApplicationBuilder builder, string instanceName) + { + return builder.Services + .AddFusionCache() + .WithRegisteredLogger() + .WithNeueccMessagePackSerializer() + .WithDefaultEntryOptions(new FusionCacheEntryOptions()) + .WithCacheKeyPrefix(instanceName); + } + + + public static WebApplicationBuilder AddDistributedFusionCache(this WebApplicationBuilder builder, + string redisUrl, + string instanceName) + { + AddBaseFusionCache(builder, instanceName) + .WithDistributedCache(new RedisCache(new RedisCacheOptions + { + Configuration = redisUrl, + InstanceName = instanceName + })) + .WithStackExchangeRedisBackplane(o => o.Configuration = redisUrl) + .AsHybridCache(); + + return builder; + } + + public static WebApplicationBuilder AddFusionCache(this WebApplicationBuilder builder, string instanceName) + { + AddBaseFusionCache(builder, instanceName); + return builder; + } +} \ No newline at end of file diff --git a/src/SharedKernel/Extensions/OpenTelemetryExtension.cs b/src/SharedKernel/Extensions/OpenTelemetryExtension.cs index ede2671..5bb98a7 100644 --- a/src/SharedKernel/Extensions/OpenTelemetryExtension.cs +++ b/src/SharedKernel/Extensions/OpenTelemetryExtension.cs @@ -27,17 +27,20 @@ public static WebApplicationBuilder AddOpenTelemetry(this WebApplicationBuilder .WithMetrics(metrics => { metrics.AddRuntimeInstrumentation() + .AddFusionCacheInstrumentation() .AddAspNetCoreInstrumentation() .AddHttpClientInstrumentation() .AddPrometheusExporter(); }) .WithTracing(tracing => { - tracing.AddAspNetCoreInstrumentation() - .AddHttpClientInstrumentation() - .AddEntityFrameworkCoreInstrumentation(); + tracing + .AddFusionCacheInstrumentation() + .AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddEntityFrameworkCoreInstrumentation(); }); - + var otlpEnabled = !string.IsNullOrEmpty(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); if (!otlpEnabled) diff --git a/src/SharedKernel/Extensions/SignalRExtensions.cs b/src/SharedKernel/Extensions/SignalRExtensions.cs index 1d39c29..d18a9cf 100644 --- a/src/SharedKernel/Extensions/SignalRExtensions.cs +++ b/src/SharedKernel/Extensions/SignalRExtensions.cs @@ -16,13 +16,14 @@ public static WebApplicationBuilder AddSignalR(this WebApplicationBuilder builde } public static WebApplicationBuilder AddDistributedSignalR(this WebApplicationBuilder builder, + string redisUrl, string redisChannelName) { builder.AddSignalRWithFiltersAndMessagePack() - .AddStackExchangeRedis(builder.Configuration.GetRedisUrl(), + .AddStackExchangeRedis(redisUrl, options => { - options.Configuration.ChannelPrefix = RedisChannel.Literal("FinHub:SignalR:"); + options.Configuration.ChannelPrefix = RedisChannel.Literal(redisChannelName); }); diff --git a/src/SharedKernel/SharedKernel.csproj b/src/SharedKernel/SharedKernel.csproj index 7111913..e82fbfe 100644 --- a/src/SharedKernel/SharedKernel.csproj +++ b/src/SharedKernel/SharedKernel.csproj @@ -8,13 +8,13 @@ Readme.md Pandatech MIT - 1.1.2 + 1.2.0 Pandatech.SharedKernel Pandatech Shared Kernel Library Pandatech, shared kernel, library, OpenAPI, Swagger, utilities, scalar Pandatech.SharedKernel provides centralized configurations, utilities, and extensions for ASP.NET Core projects. For more information refere to readme.md document. https://github.com/PandaTechAM/be-lib-sharedkernel - Added dictinory extensions + Added fusion cache support @@ -38,10 +38,11 @@ + - + - + @@ -52,9 +53,12 @@ - + + + +