Caching Manually
Sometimes it makes sense to cache specifically inside the C# code. For example, if you need to fetch data from an external source or database, it would save time and traffic if you cache the results and don't access the result every time.
Let's try it out by using two different ways to use create and access cache items:
- To try it out we will extend the
HomeController
a little bit. Start by injecting an instance of theIMemoryCache
to the controller and store it in a field:using Microsoft.Extensions.Caching.Memory; public class HomeController : Controller { private readonly ILogger<HomeController> _logger; private readonly IMemoryCache _cache; public HomeController( ILogger<HomeController> logger, IMemoryCache cache ) &...