Overriding Existing Methods

As you can see in standard implementation, all methods are marked as virtual which means you can customize their content via overriding them.

You can override the existing methods to create a new class. To do that, newly created classes should inherit from RENRedisCacheService class:

public class MyCacheService : RENRedisCacheService
{
    public MyCacheService(IConnectionMultiplexer connection, IConfiguration configuration) : base(connection, configuration) { }

    public override T Get<T>(string cacheKey, CancellationToken cancellationToken = default);)
    {
        Console.WriteLine("Getting...");
        //custom implementations
        return base.Get<T>(cacheKey, cancellationToken);
    }
}
circle-exclamation

Here, we overrided the existing method to expand it's functionality. From now on, we have register MyCacheService in Program.cs instead of standard registeration:

// builder.Services.RegisterRENCacheAccessHelpers<RENInMemoryCacheService>(); // SINCE WE ARE NOT USING STANDARD APPROACH ANYMORE
builder.Services.RegisterRENCacheAccessHelpers<MyCacheService>();
circle-info

Since we are registering classes that inherits from IRENCacheService, we can use MyCacheService because we inherited from RENRedisCacheService which inherits from ICacheService interface!

And we may inject it to our classes as follows:

Last updated