Install Nuget package Microsoft. Extensions. Caching. StackExchangeRedis Creating a cache attribute [Cached(time: 10)] public async Task<IActionResult> GetAll() => Ok(await SalesdbContext.Employees.ToListAsync()); We create an attribute called cached that accepts the expiry time for the cached data. Once the expiry time is over the response returned will be null and we will fetch the data from the database and update the cahce. [AttributeUsage(AttributeTargets.Method)] public class CachedAttribute : Attribute, IAsyncActionFilter { public int Time { get; set; } public CachedAttribute(int time) { Time = time; } public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) ...
I jot down whatever I learn from different sources.Written for personal use. But anyone can learn.