Overriding Existing Methods
public class MyCacheService : RENInMemoryCacheService
{
public MyCacheService(IMemoryCache cache) : base(cache) { }
public override async Task<T> GetAsync<T>(string cacheKey, CancellationToken cancellationToken = default)
{
Console.WriteLine("Getting custom...");
//custom implementations
return await base.GetAsync<T>(cacheKey, cancellationToken);
}
}// builder.Services.RegisterRENCacheAccessHelpers<RENInMemoryCacheService>(); // SINCE WE ARE NOT USING STANDARD APPROACH ANYMORE
builder.Services.RegisterRENCacheAccessHelpers<MyCacheService>();public class HomeController : ControllerBase
{
private readonly IRENCacheService _customCacheService;
private readonly IRENRepository<User> _userRepository;
public HomeController(IRENUnitOfWork<RENDbContext> uow, IRENCacheService customCacheService)
{
_userRepository = uow.GetRENRepository<User>();
_customCacheService = customCacheService;
}
[HttpGet]
[Route("Index")]
public async Task<IActionResult> Index()
{
var cacheKey = "users";
var users = await _customCacheService.GetAsync<List<User>>(cacheKey);
if (users != null) return Ok(users);
users = await _userRepository.GetListAsync();
await _customCacheService.SetAsync(cacheKey, users);
return Ok(users);
}
}Last updated