Skip to content

Commit d197d53

Browse files
authored
Merge pull request #8 from PandaTechAM/development
added health check
2 parents 010ed84 + ee100ac commit d197d53

File tree

7 files changed

+31
-114
lines changed

7 files changed

+31
-114
lines changed

Readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,16 @@ public class SendSmsService(IRateLimitService<SendSmsService> rateLimitService)
244244
}
245245
```
246246

247+
### 6. Health Check Integration
248+
249+
`Pandatech.DistributedCache` now automatically adds a Redis health check as part of the `AddDistributedCache` method.
250+
This feature leverages the `AspNetCore.HealthChecks.Redis` library to monitor Redis connectivity seamlessly.
251+
252+
**Default Health Check Behavior:**
253+
254+
- A health check for Redis is automatically registered using the provided Redis connection string.
255+
- The health check uses a 3-second timeout to validate the availability of Redis.
256+
247257
Based on rate limit state you can throw exception/return 427 or proceed with the business logic.
248258

249259
## Enforced MessagePack Serialization

src/DistributedCache/DistributedCache.csproj

Lines changed: 3 additions & 2 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>3.0.0</Version>
11+
<Version>3.0.1</Version>
1212
<PackageId>Pandatech.DistributedCache</PackageId>
1313
<Title>Pandatech Distributed Cache</Title>
1414
<PackageTags>Pandatech, library, redis, distributed locks, cache</PackageTags>
1515
<Description>Pandatech.DistributedCache is a comprehensive caching library designed for .NET applications, leveraging the power of Redis. It provides easy-to-use and highly configurable caching mechanisms, including support for tagged cache entries, customizable expiration policies, and robust health check services. The library also features built-in distributed lock mechanisms to ensure data consistency and prevent cache stampedes. This ensures high performance, scalability, and reliability, making it an ideal choice for enterprise-level distributed caching needs.</Description>
1616
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-distributed-cache</RepositoryUrl>
17-
<PackageReleaseNotes>.Net 9 Upgrade</PackageReleaseNotes>
17+
<PackageReleaseNotes>Added healthcheck</PackageReleaseNotes>
1818
</PropertyGroup>
1919

2020
<ItemGroup>
@@ -23,6 +23,7 @@
2323
</ItemGroup>
2424

2525
<ItemGroup>
26+
<PackageReference Include="AspNetCore.HealthChecks.Redis" Version="8.0.1" />
2627
<PackageReference Include="MessagePack" Version="2.5.192" />
2728
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
2829
<PackageReference Include="StackExchange.Redis.Extensions.AspNetCore" Version="10.2.0"/>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.Extensions.DependencyInjection;
3+
4+
namespace DistributedCache.Extensions;
5+
6+
internal static class HealthCheckExtension
7+
{
8+
internal static WebApplicationBuilder AddRedisHealthCheck(this WebApplicationBuilder builder, string connectionString)
9+
{
10+
builder.Services
11+
.AddHealthChecks()
12+
.AddRedis(connectionString, timeout: TimeSpan.FromSeconds(3));
13+
14+
return builder;
15+
}
16+
}

src/DistributedCache/Extensions/WebApplicationBuilderExtension.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public static WebApplicationBuilder AddDistributedCache(this WebApplicationBuild
4545
};
4646

4747
builder.Services.AddStackExchangeRedisExtensions<RedisMsgPackObjectSerializer>(redisConfiguration);
48-
//builder.Services.AddHostedService<RedisHealthCheckService>(); //Discontinued feature
48+
49+
builder.AddRedisHealthCheck(configurations.RedisConnectionString);
4950

5051
return builder;
5152
}
@@ -84,13 +85,6 @@ private static void ValidateOptions(WebApplicationBuilder builder)
8485
{
8586
throw new ArgumentException("AddCacheService options: DefaultExpiration must be greater than 0.");
8687
}
87-
88-
if (options.CacheResetMode == CacheResetMode.ResetFrequentTagsAfterHealthCheckFail &&
89-
options.HealthCheckInterval <= TimeSpan.Zero) //Discontinued feature
90-
{
91-
throw new ArgumentException(
92-
"AddCacheService options: HealthCheckInterval must be greater than 0 when CacheResetMode is ResetFrequentTagsAfterHealthCheckFail.");
93-
}
9488
});
9589
}
9690
}

src/DistributedCache/Options/CacheConfigurationOptions.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,4 @@ public class CacheConfigurationOptions
99
public TimeSpan SyncTimeout { get; set; } = TimeSpan.FromSeconds(5);
1010
public TimeSpan DistributedLockDuration { get; set; } = TimeSpan.FromSeconds(5);
1111
public TimeSpan DefaultExpiration { get; set; } = TimeSpan.FromMinutes(15);
12-
13-
internal CacheResetMode CacheResetMode { get; set; } =
14-
CacheResetMode.ResetFrequentTagsAfterHealthCheckFail; //Discontinued feature
15-
16-
internal TimeSpan HealthCheckInterval { get; set; } = TimeSpan.FromMilliseconds(100); //Discontinued feature
1712
}

src/DistributedCache/Options/CacheResetMode.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/DistributedCache/Services/Implementations/RedisHealthCheckService.cs

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)