MARCIN.com
Marcin Jamro, PhD, DSc
How to cache data with memory cache?
C# | .NET | ASP.NET
Caching is a powerful technique that allows you to significantly fasten your web application, especially if it executes complex and long-running database queries that are performed frequently. With the usage of caching you do not need to execute such queries each time, as you can use the cached results, if they are ready and are still up-to-date. You can easily introduce such a technique into your web application with the memory cache mechanism available in ASP.NET Core, as presented below.
Just take a look at the example:
public class HomeController(IMemoryCache cache) : Controller { public IActionResult Index() { if (!cache.TryGetValue("entries", out List? entries) ) { entries = /* Long-running query or calculations */ cache.Set("entries", entries); } return View(entries); } private ListGetEntries() { return new List (); } }
First, you inject IMemoryCache
, as you see in the primary constructor. Then, you use the TryGetValue
method that tries to retrieve an entry with the given key (entries
in this case) from the cache. If the result is received correctly, the method returns true
. At the end, let's take a look at the Set
method that stores a specified value in the cache, with a given key (entries
here). It looks pretty simple, doesn't it?
You also should indicate that you are using the memory cache in the Program.cs
file:
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); builder.Services.AddControllersWithViews(); builder.Services.AddMemoryCache(); (...)
That's all! Good luck with caching in your applications :-)
The content, including any part of code, is presented without warranty, either express or implied. The author cannot be held liable for any damages caused or alleged to have been caused directly or indirectly by any content shown on this website.
Hello, I am Marcin
Reliable entrepreneur with 10+ years of companies operation, such as CEO at a few IT companies. I was an author of a few software & hardware products, still open to new ideas & cooperation.
Helpful expert with 10+ years of experience, together with PhD and DSc in Computer Science. I was an author of books and publications, as well as an expert in international projects.
Experienced developer with 10+ years of development and 100+ completed projects. I worked on various complex international projects, e.g. at Microsoft in USA and as CTO at a few companies. I have MCP, MCTS and MCPD certificates.
You can read more about me in my short bio. I am waiting for contact at [email protected], as well as at my Facebook and LinkedIn profiles.
Books
I am an author of a few books and numerous publications, also in high-quality international scientific journals.
If you want to learn data structures and algorithms in the context of C#, let's take a look at my newest book. It is the second edition of C# Data Structures and Algorithms. You can buy it here.
Projects
Do you like traveling? If so, discover an amazing world with local guides, right here and right now at: https://camaica.com
Travels
I love travels, learning new cultures and regions, meeting outstanding people, as well as taking pictures of beautiful places. Take a look at my travel diary.