Skip to content

Commit ce8a27b

Browse files
authored
Merge pull request #29 from PandaTechAM/development
FusionCache Support
2 parents 75cb9a6 + 803e235 commit ce8a27b

File tree

6 files changed

+69
-16
lines changed

6 files changed

+69
-16
lines changed

Readme.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,9 @@ builder
138138
.AddMediatrWithBehaviors(AssemblyRegistry.ToArray())
139139
.AddResilienceDefaultPipeline()
140140
.MapDefaultTimeZone()
141-
.AddRedis(KeyPrefix.AssemblyNamePrefix)
142-
.AddDistributedSignalR("DistributedSignalR") // or .AddSignalR()
141+
.AddDistributedFusionCache("redis://localhost:6379",
142+
builder.Environment.GetShortEnvironmentName() + "_" + "app_name") // or .AddFusionCache(...)
143+
.AddDistributedSignalR("redis://localhost:6379","app_name:") // or .AddSignalR()
143144
.AddCors()
144145
.AddHealthChecks();
145146

@@ -621,14 +622,14 @@ This package includes various extensions and utilities to aid development:
621622
retrieves DefaultTimeZone from `appsettings.json` and sets it as the default time zone.
622623
- **UrlBuilder:** A utility for building URLs with query parameters.
623624
- **Language ISO Code Helper:** Validate, query, and retrieve information about ISO language codes.
625+
- **FusionCache Extensions:** Simplify the configuration of in memory or distributed caching with Redis.
624626

625627
### Related NuGet Packages
626628

627629
- **Pandatech.Crypto:** Provides cryptographic utilities.
628630
- **Pandatech.FluentMinimalApiMapper:** Simplifies mapping in minimal APIs.
629631
- **Pandatech.RegexBox:** A collection of useful regular expressions.
630632
- **Pandatech.ResponseCrafter:** A utility for crafting consistent API responses.
631-
- **Pandatech.DistributedCache:** A distributed cache provider for Redis.
632633

633634
## License
634635

Shared.Kernel.Demo/Program.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using DistributedCache.Options;
12
using FluentMinimalApiMapper;
23
using Microsoft.AspNetCore.Mvc;
34
using SharedKernel.Demo2;
@@ -25,8 +26,9 @@
2526
.AddControllers(AssemblyRegistry.ToArray())
2627
.AddMediatrWithBehaviors(AssemblyRegistry.ToArray())
2728
.AddResilienceDefaultPipeline()
28-
// .AddRedis(KeyPrefix.AssemblyNamePrefix)
29-
//.AddDistributedSignalR("DistributedSignalR") // or .AddSignalR()
29+
.AddDistributedFusionCache("redis://localhost:6379",
30+
builder.Environment.GetShortEnvironmentName() + "_" + "app_name") // or .AddFusionCache(...)
31+
.AddDistributedSignalR("redis://localhost:6379","app_name:") // or .AddSignalR()
3032
.MapDefaultTimeZone()
3133
.AddCors()
3234
.AddOutboundLoggingHandler()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.Extensions.Caching.StackExchangeRedis;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using ZiggyCreatures.Caching.Fusion;
5+
6+
namespace SharedKernel.Extensions;
7+
8+
public static class FusionCacheExtensions
9+
{
10+
private static IFusionCacheBuilder AddBaseFusionCache(WebApplicationBuilder builder, string instanceName)
11+
{
12+
return builder.Services
13+
.AddFusionCache()
14+
.WithRegisteredLogger()
15+
.WithNeueccMessagePackSerializer()
16+
.WithDefaultEntryOptions(new FusionCacheEntryOptions())
17+
.WithCacheKeyPrefix(instanceName);
18+
}
19+
20+
21+
public static WebApplicationBuilder AddDistributedFusionCache(this WebApplicationBuilder builder,
22+
string redisUrl,
23+
string instanceName)
24+
{
25+
AddBaseFusionCache(builder, instanceName)
26+
.WithDistributedCache(new RedisCache(new RedisCacheOptions
27+
{
28+
Configuration = redisUrl,
29+
InstanceName = instanceName
30+
}))
31+
.WithStackExchangeRedisBackplane(o => o.Configuration = redisUrl)
32+
.AsHybridCache();
33+
34+
return builder;
35+
}
36+
37+
public static WebApplicationBuilder AddFusionCache(this WebApplicationBuilder builder, string instanceName)
38+
{
39+
AddBaseFusionCache(builder, instanceName);
40+
return builder;
41+
}
42+
}

src/SharedKernel/Extensions/OpenTelemetryExtension.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,20 @@ public static WebApplicationBuilder AddOpenTelemetry(this WebApplicationBuilder
2727
.WithMetrics(metrics =>
2828
{
2929
metrics.AddRuntimeInstrumentation()
30+
.AddFusionCacheInstrumentation()
3031
.AddAspNetCoreInstrumentation()
3132
.AddHttpClientInstrumentation()
3233
.AddPrometheusExporter();
3334
})
3435
.WithTracing(tracing =>
3536
{
36-
tracing.AddAspNetCoreInstrumentation()
37-
.AddHttpClientInstrumentation()
38-
.AddEntityFrameworkCoreInstrumentation();
37+
tracing
38+
.AddFusionCacheInstrumentation()
39+
.AddAspNetCoreInstrumentation()
40+
.AddHttpClientInstrumentation()
41+
.AddEntityFrameworkCoreInstrumentation();
3942
});
40-
43+
4144
var otlpEnabled = !string.IsNullOrEmpty(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
4245

4346
if (!otlpEnabled)

src/SharedKernel/Extensions/SignalRExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ public static WebApplicationBuilder AddSignalR(this WebApplicationBuilder builde
1616
}
1717

1818
public static WebApplicationBuilder AddDistributedSignalR(this WebApplicationBuilder builder,
19+
string redisUrl,
1920
string redisChannelName)
2021
{
2122
builder.AddSignalRWithFiltersAndMessagePack()
22-
.AddStackExchangeRedis(builder.Configuration.GetRedisUrl(),
23+
.AddStackExchangeRedis(redisUrl,
2324
options =>
2425
{
25-
options.Configuration.ChannelPrefix = RedisChannel.Literal("FinHub:SignalR:");
26+
options.Configuration.ChannelPrefix = RedisChannel.Literal(redisChannelName);
2627
});
2728

2829

src/SharedKernel/SharedKernel.csproj

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
<PackageReadmeFile>Readme.md</PackageReadmeFile>
99
<Authors>Pandatech</Authors>
1010
<Copyright>MIT</Copyright>
11-
<Version>1.1.2</Version>
11+
<Version>1.2.0</Version>
1212
<PackageId>Pandatech.SharedKernel</PackageId>
1313
<Title>Pandatech Shared Kernel Library</Title>
1414
<PackageTags>Pandatech, shared kernel, library, OpenAPI, Swagger, utilities, scalar</PackageTags>
1515
<Description>Pandatech.SharedKernel provides centralized configurations, utilities, and extensions for ASP.NET Core projects. For more information refere to readme.md document.</Description>
1616
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-sharedkernel</RepositoryUrl>
17-
<PackageReleaseNotes>Added dictinory extensions</PackageReleaseNotes>
17+
<PackageReleaseNotes>Added fusion cache support</PackageReleaseNotes>
1818
</PropertyGroup>
1919

2020
<ItemGroup>
@@ -38,10 +38,11 @@
3838
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="9.0.1" />
3939
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="9.0.1" />
4040
<PackageReference Include="Microsoft.AspNetCore.SignalR.StackExchangeRedis" Version="9.0.1" />
41+
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="9.0.1" />
4142
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="9.1.0" />
42-
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.11.0" />
43+
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.11.1" />
4344
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.8.0-rc.1"/>
44-
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.11.0" />
45+
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.11.1" />
4546
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.10.1" />
4647
<PackageReference Include="OpenTelemetry.Instrumentation.EntityFrameworkCore" Version="1.0.0-beta.12" />
4748
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.10.0" />
@@ -52,9 +53,12 @@
5253
<PackageReference Include="Pandatech.PandaVaultClient" Version="4.0.3" />
5354
<PackageReference Include="Pandatech.RegexBox" Version="3.0.0" />
5455
<PackageReference Include="Pandatech.ResponseCrafter" Version="5.1.5" />
55-
<PackageReference Include="Scalar.AspNetCore" Version="2.0.2" />
56+
<PackageReference Include="Scalar.AspNetCore" Version="2.0.4" />
5657
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
5758
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="7.2.0" />
59+
<PackageReference Include="ZiggyCreatures.FusionCache.Backplane.StackExchangeRedis" Version="2.0.0" />
60+
<PackageReference Include="ZiggyCreatures.FusionCache.OpenTelemetry" Version="2.0.0" />
61+
<PackageReference Include="ZiggyCreatures.FusionCache.Serialization.NeueccMessagePack" Version="2.0.0" />
5862
</ItemGroup>
5963

6064
</Project>

0 commit comments

Comments
 (0)