Implementing New Methods

Sometimes your project requires cache operations that aren’t covered by the default methods. Thanks to REN.Kit’s extensible design, you can easily add brand new methods to your custom cache service.

Simply create a new class inheriting from RENInMemoryCacheService, and add your own public methods—no need to override anything!


How to Extend?

1. Create a New Service Interface to Implement New Method

public interface IExtendedRENInMemoryCacheService : IRENCacheService
{
    void AdditionalMethod();
}

This interface introduces new method while keep using the default ones


2. Create a New Service to Implement New Method

public class ExtendedRENInMemoryCacheService(IMemoryCache memoryCache) 
    : RENInMemoryCacheService(memoryCache), 
    IExtendedRENInMemoryCacheService
{
    public void AdditionalMethod()
    {
        Console.WriteLine("ExtendedRENInMemoryCacheService AdditionalMethod called.");
        // Implement your additional logic here
    }
}

3. Register Your Custom Implementation

Since you are not using the default interface now (you've introduced new method and this method should be taken into account) You should register interface and classes like:

builder.Services.AddRENCaching<IExtendedRENInMemoryCacheService, ExtendedRENInMemoryCacheService>(RegisterRENCaching.CacheType.InMemory);

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 Custom Service In Action

[ApiController]
[Route("api/[controller]")]
public class ExtendedInMemoryCacheController(IExtendedRENInMemoryCacheService cacheService) : ControllerBase
{
    [HttpGet("additional-method")]
    public IActionResult AdditionalMethod(string key)
    {
        cacheService.AdditionalMethod();
        return Ok();
    }
}

Last updated