Implementing New Methods
Sometimes, your application needs more than just the standard caching operations. REN.Kit makes it effortless to extend the Redis cache service with entirely new methods tailored to your business needs.
How to Extend REN Redis Cache Functionality
1. Create a Custom Interface with New Methods
Define your custom interface, inheriting from IRENCacheService
, and add any extra methods you want:
public interface IExtendedRENRedisCacheService : IRENCacheService
{
void AdditionalMethod();
}
This approach lets you keep all default cache operations while introducing custom ones.
2. Implement Your Interface in a Custom Service
Inherit from RENRedisCacheService
and your new interface, then implement your extra logic:
public class ExtendedRENRedisCacheService(IConnectionMultiplexer connectionMultiplexer) : RENRedisCacheService(connectionMultiplexer), IExtendedRENRedisCacheService
{
public void AdditionalMethod()
{
Console.WriteLine("ExtendedRENRedisCacheService AdditionalMethod called.");
// Implement your additional logic here
}
}
3. Register Your Custom Implementation in DI
Since you now have a custom interface with extra methods, register both your interface and implementation like this:
builder.Services.AddRENCaching<IExtendedRENRedisCacheService, ExtendedRENRedisCacheService>(RegisterRENCaching.CacheType.Redis);
Here’s how the registration method works under the hood:
public static void AddRENCaching<TICacheService, TCacheService>(this IServiceCollection services, CacheType cacheType)
where TICacheService : class, IRENCacheService
where TCacheService : class, TICacheService
{
switch (cacheType)
{
case CacheType.InMemory:
AddInMemoryRENCache<TICacheService, TCacheService>(services);
break;
case CacheType.Redis:
AddRedisRENCache<TICacheService, TCacheService>(services);
break;
default:
throw new ArgumentOutOfRangeException(nameof(cacheType), cacheType, null);
}
}
4. Use Your Extended Redis Cache Service Anywhere
Inject your custom interface and use your new method in your controllers or services:
[Route("api/[controller]")]
[ApiController]
public class ExtendedRedisCacheController(IExtendedRENRedisCacheService cacheService) : ControllerBase
{
[HttpGet("additional-method")]
public IActionResult AdditionalMethod(string key)
{
cacheService.AdditionalMethod();
return Ok();
}
}
Last updated