Overriding Existing Methods

REN’s in-memory cache service is built to be extended and customized. All major methods are marked as virtual—allowing you to override any operation and tailor caching behavior for your project’s unique needs.


How to Override?

1. Create a New Service to Override Existing Method

public class OverridedRENInMemoryCacheService(IMemoryCache memoryCache) 
    : RENInMemoryCacheService(memoryCache)
{
    public override async Task<T> GetAsync<T>(string cacheKey, 
        CancellationToken cancellationToken = default)
    {
        Console.WriteLine($"OverridedRENInMemoryCacheService GetAsync called");
        // You can add custom logic here before calling the base method
        return await base.GetAsync<T>(cacheKey, cancellationToken);
    }
}

2. Register Your Custom Implementation

In the above example, OverridedRENInMemoryCacheService is your custom implementation that inherits from RENInMemoryCacheService. By passing it as the generic type parameter TCacheService, you instruct the DI container to use your custom cache logic for all cache operations.

Here’s how the registration method works under the hood:

  • If you select CacheType.InMemory, your service will be registered as an in-memory cache.

  • If you select CacheType.Redis, your service will be registered as a Redis-based cache.

  • If you pass an unsupported cache type, an exception is thrown.

circle-check

Tip:


3. Use Custom Service In Action

Last updated