Skip to content

Commit 0d9b1eb

Browse files
committed
RateLimit interface change
1 parent 9248b82 commit 0d9b1eb

File tree

8 files changed

+41
-19
lines changed

8 files changed

+41
-19
lines changed

Readme.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ cross ClassLibrary cache access.
5454
options.KeyPrefixForIsolation = KeyPrefix.AssemblyNamePrefix;
5555
```
5656

57+
**Note:** Even if you don't use key prefixing, you still need to provide the class as a generic type (`T`) when using
58+
`IRateLimitService<T>`. The generic type `T` is used to retrieve the assembly name, which is important for key isolation. If
59+
you choose not to prefix keys by assembly name, this type is still required but will be ignored in the actual
60+
implementation.
61+
5762
### 2. Cached Entity Preparation
5863

5964
Create your cache entity/model in order to inject it in the actual service:
@@ -226,7 +231,7 @@ public static class RateLimitingConfigurations //your shared rate limiting confi
226231
using DistributedCache.Dtos;
227232
using DistributedCache.Services.Interfaces;
228233

229-
public class SendSmsService(IRateLimitService rateLimitService)
234+
public class SendSmsService(IRateLimitService<SendSmsService> rateLimitService)
230235
{
231236
public async Task<RateLimitState> SendSms(CancellationToken cancellationToken = default)
232237
{

src/DistributedCache/DistributedCache.csproj

Lines changed: 4 additions & 4 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.2.3</Version>
11+
<Version>2.0.0</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>Assembly name fix in rate limit</PackageReleaseNotes>
17+
<PackageReleaseNotes>RateLimit interface change</PackageReleaseNotes>
1818
</PropertyGroup>
1919

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

2525
<ItemGroup>
26-
<PackageReference Include="MessagePack" Version="2.5.140"/>
27-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.6"/>
26+
<PackageReference Include="MessagePack" Version="2.5.172" />
27+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" />
2828
<PackageReference Include="StackExchange.Redis.Extensions.AspNetCore" Version="10.2.0"/>
2929
<PackageReference Include="StackExchange.Redis.Extensions.MsgPack" Version="10.2.0"/>
3030
</ItemGroup>

src/DistributedCache/Extensions/WebApplicationBuilderExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public static WebApplicationBuilder AddDistributedCache(this WebApplicationBuild
3333
builder.Services.AddSingleton<IConnectionMultiplexer>(ConnectionMultiplexer.Connect(redisOptions));
3434

3535
builder.Services.AddSingleton(typeof(ICacheService<>), typeof(RedisCacheService<>));
36+
builder.Services.AddSingleton(typeof(IRateLimitService<>), typeof(RedisRateLimitService<>));
3637
builder.Services.AddSingleton<RedisLockService>();
37-
builder.Services.AddScoped<IRateLimitService, RedisRateLimitService>();
3838

3939
var redisConfiguration = new RedisConfiguration
4040
{

src/DistributedCache/Services/Implementations/RedisRateLimitService.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@
1010

1111
namespace DistributedCache.Services.Implementations;
1212

13-
public class RedisRateLimitService(
13+
public class RedisRateLimitService<T>(
1414
IRedisClient redisClient,
1515
IOptions<CacheConfigurationOptions> options,
16-
RedisLockService lockService) : IRateLimitService
16+
RedisLockService lockService) : IRateLimitService<T>
17+
where T : class
1718
{
1819
private readonly IRedisDatabase _redisDatabase = redisClient.GetDefaultDatabase();
1920
private readonly CacheConfigurationOptions _config = options.Value;
20-
21-
private readonly string _moduleName = Assembly.GetCallingAssembly()
22-
.GetName()
23-
.Name!;
21+
22+
private readonly string _moduleName = typeof(T).Assembly.GetName().Name!;
2423

2524
public async ValueTask<RateLimitState> RateLimitAsync(RateLimitConfiguration rateLimitConfiguration,
2625
CancellationToken cancellationToken = default)

src/DistributedCache/Services/Interfaces/IRateLimitService.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,26 @@
22

33
namespace DistributedCache.Services.Interfaces;
44

5-
public interface IRateLimitService
5+
/// <summary>
6+
/// Defines a service for applying rate limiting to specific operations.
7+
/// </summary>
8+
/// <typeparam name="T">
9+
/// The type of the class that is used to infer the assembly name for key isolation.
10+
/// Even if the assembly name is not used in key prefixing, this type must still be provided
11+
/// to comply with the service's requirements.
12+
/// </typeparam>
13+
public interface IRateLimitService<T>
14+
where T : class
615
{
16+
/// <summary>
17+
/// Applies rate limiting based on the provided configuration.
18+
/// </summary>
19+
/// <param name="rateLimitConfiguration">The configuration defining rate limit rules, such as maximum attempts and expiration.</param>
20+
/// <param name="cancellationToken">A token that can be used to propagate notification that the operation should be canceled.</param>
21+
/// <returns>
22+
/// A task representing the asynchronous operation, containing the result of the rate limiting operation,
23+
/// which indicates whether the rate limit has been exceeded or not.
24+
/// </returns>
725
ValueTask<RateLimitState> RateLimitAsync(RateLimitConfiguration rateLimitConfiguration,
826
CancellationToken cancellationToken = default);
927
}

test/DistributedCache.Demo/DistributedCache.Demo.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.6" />
13-
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
12+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" />
13+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.3" />
1414
</ItemGroup>
1515

1616
<ItemGroup>

test/DistributedCache.Demo/TestRateLimiting/SendSmsService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace CacheService.Demo.TestRateLimiting;
55

6-
public class SendSmsService(IRateLimitService rateLimitService)
6+
public class SendSmsService(IRateLimitService<SendSmsService> rateLimitService)
77
{
88
public async Task<RateLimitState> SendSms(CancellationToken cancellationToken = default)
99
{

test/DistributedCache.Tests/DistributedCache.Tests.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
14-
<PackageReference Include="xunit" Version="2.8.1" />
15-
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
14+
<PackageReference Include="xunit" Version="2.9.0" />
15+
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
1616
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1717
<PrivateAssets>all</PrivateAssets>
1818
</PackageReference>

0 commit comments

Comments
 (0)