Advertisement
Поиск  
Always will be ready notify the world about expectations as easy as possible: job change page
Feb 7, 2021

Simple In-Memory Caching in .Net Core with IMemoryCache

Автор:
Sahan Serasinghe
Источник:
Просмотров:
1026

Caching is the process of storing the data that’s frequently used so that data can be served faster for any future requests. Suppose we have a very lightweight process which talks to another server whose data is not going to change frequently; “Our service” and “Users Service” (which returns an array of users) respectively.

Without any caching in place, we would be making multiple requests which will ultimately result in timeouts or making the remote server unnecessarily busy.

Let’s have a look at how we can improve the performance of these requests by using a simple caching implementation. .NET Core provides 2 cache implementations under Microsoft.Extensions.Caching.Memory library:

  1. IMemoryCache - Simplest form of cache which utilises the memory of the web server.
  2. IDistributedCache - Usually used if you have a server farm with multiple app servers.

In this example we will be using the IMemoryCache along with latest version of .NET Core as of yet, which is version 3.1.

These are the steps we are going to follow:

  1. Create/Clone a sample .NET Core app.
  2. Naive implementation.
  3. Refactoring our code to use locking.

1. Create/Clone a sample .NET Core app

You can simply clone In-memory cache sample code repo I have made for the post. If not, make sure that you scaffold a new ASP.NET Core MVC app to follow along.

First, we need to inject the in-memory caching service into the constructor of the HomeController.

public class HomeController : Controller
{
    private readonly IMemoryCache _cache;

    public HomeController(ILogger<HomeController> logger, IMemoryCache memoryCache)
    {
        _cache = memoryCache;
    }
}

Note: With .Net Core 3.1 you don’t need to specifically register the memory caching service. However, if you are using a prior version such as 2.1, then you will need to add the following line in the Startups.cs:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMemoryCache(); // Add this line

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }
}

2. Naive implementation

For the sake of this tutorial, we’ll use a free external API such as reqres.in/api/users. Let’s imagine we want to cache the response of the API. For simplicity I have used the example code provided by Microsoft Docs.

// Code removed for brevity
...

// Look for cache key.
if (!_cache.TryGetValue(CacheKeys.Entry, out cacheEntry))
{
    // Key not in cache, so get data.
    cacheEntry = DateTime.Now;

    // Set cache options.
    var cacheEntryOptions = new MemoryCacheEntryOptions()
        // Keep in cache for this time, reset time if accessed.
        .SetSlidingExpiration(TimeSpan.FromSeconds(3));

    // Save data in cache.
    _cache.Set(CacheKeys.Entry, cacheEntry, cacheEntryOptions);
}

Explanation

The code is pretty straightforward. We first check whether we have the value for the given key present in our in-memory cache store. If not, we do the request to get the data and store in our cache. What SetSlidingExpiration does is that as long as no one accesses the cache value, it will eventually get deleted after 10 seconds. But if someone accesses it, the expiration will get renewed.

Here’s my implementation with a bit of code re-structuring:

var users = _cacheProvider.GetFromCache<IEnumerable<User>>(cacheKey);
if (users != null) return users;

// Key not in cache, so get data.
users = await func();

var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(10));
_cacheProvider.SetCache(cacheKey, users, cacheEntryOptions);

return users;

await func() will wait and return the response from our external API endpoint (provided that it’s passed into our method) so that we can use that value to store in our cache.

Have a look at the full implementation of my GetCachedResponse() in CachedUserService.cs for a more generic solution to handle any type of data.

This gets the job done for a very simple workload. But how can we make this more reliable if there are multiple threads accessing our cache store? Let’s have a look in our next step.

3. Refactoring our code to use locking

Now, let’s assume that we have several users accessing our service which means there could be multiple accessing our in-memory cache. One way to make sure that no two different users get different results is by utilising .Net locks. Refer the following scenario:

Let’s breakdown the sequence of requests and responses:

  1. User A makes a request to our web service.
  2. In-memory cache doesn’t have a value in place, it enters in to lock state and makes a request to the Users Service.
  3. User B makes a request to our web service and waits till the lock is released.
  4. This way, we can reduce the number of calls being made to the external web service. returns the response to our web service and the value is cached.
  5. Lock is released, User A gets the response.
  6. User B enters the lock and the cache provides the value (as long it’s not expired).
  7. User B gets the response.

The above depiction is a very high-level abstraction over all the awesome stuff that happens under the covers. Please use this as a guide only. Let’s implement this!

...
var users = _cacheProvider.GetFromCache<IEnumerable<User>>(cacheKey);

if (users != null) return users;
try
{
    await semaphore.WaitAsync();

    // Recheck to make sure it didn't populate before entering semaphore
    users = _cacheProvider.GetFromCache<IEnumerable<User>>(cacheKey);

    if (users != null) return users;
    users = await func();
    
    var cacheEntryOptions = new MemoryCacheEntryOptions()
        .SetSlidingExpiration(TimeSpan.FromSeconds(10));

    _cacheProvider.SetCache(cacheKey, users, cacheEntryOptions);
}
finally
{
    // It's important to do this, otherwise we'll be locked forever
    semaphore.Release();
}

return users;

Explanation

Same as in our previous example we first check our cache for the presence of the value for a key provided. if not, we then asynchronously wait to enter the Semaphore. Once our thread has been granted access to the Semaphore, we recheck if the value has been populated previously for safety. If we still don’t have a value, we then call our external service and store the value in the cache.

Have a look at the CachedUserService.cs for the full implementation.

Hope you enjoyed this tutorial. Happy to know your thoughts! 🙂

References

  1. https://www.blexin.com/en-US/Article/Blog/In-memory-caching-in-ASPNET-Core-45
  2. https://www.infoworld.com/article/3230129/how-to-use-in-memory-caching-in-aspnet-core.html
  3. https://blog.cdemi.io/async-waiting-inside-c-sharp-locks/
Похожее
Feb 17, 2023
Author: Juldhais Hengkyawan
A Guide to Building Scalable, Maintainable Web API using ASP .NET CoreThe term “Clean Architecture” has become increasingly popular in software development in recent years. Clean Architecture is a software design pattern that prioritizes the separation of concerns, making it...
Feb 15
Author: Pranaya Rout
Kestrel Web Server in ASP.NET Core ApplicationIn this article, I will discuss the Kestrel Web Server in ASP.NET Core application with examples. Please read our previous article discussing the ASP.NET Core InProcess Hosting Model with examples. At the end of...
Aug 25, 2020
In my 2018 series, we covered EF Core Migrations to explain how to add, remove and apply Entity Framework Core Migrations in an ASP .NET Core web application project. In this article, we’ll continue to look at the newer 2020...
Aug 25, 2020
Why Share Code Across Projects/Assemblies?There are multiple reasons why you may want to share code between multiple projects/assemblies. Code reuse: This should be pretty self-explanatory. You shouldn’t have to rewrite the same code more than once. Placing...
Написать сообщение
Почта
Имя
*Сообщение


© 1999–2024 WebDynamics
1980–... Sergey Drozdov
Area of interests: .NET Framework | .NET Core | C# | ASP.NET | Windows Forms | WPF | HTML5 | CSS3 | jQuery | AJAX | Angular | React | MS SQL Server | Transact-SQL | ADO.NET | Entity Framework | IIS | OOP | OOA | OOD | WCF | WPF | MSMQ | MVC | MVP | MVVM | Design Patterns | Enterprise Architecture | Scrum | Kanban
Почему айтишники переходят из одной компании в другую
Как лучше проводить one-to-one со своими сотрудниками: 5 лайфхаков из личного опыта
Выгорание эволюционирует. Что такое «тихий уход» — новый тренд среди офисных сотрудников
Замыкания в JavaScript для начинающих
Первый рабочий день: инструкция по выживанию — 4 совета, как с комфортом выйти на новую работу
Почему в вашем коде так сложно разобраться
Почему сеньоры ненавидят собеседования с кодингом, и что компании должны использовать вместо них
Жестокая правда будней программиста: 8 наблюдений
101 вопрос, на которые должен ответить Python-разработчик
5 ошибок при обратной связи с пользователями
LinkedIn
Boosty
Donate to support the project
GitHub account
GitHub profile