In-Memory Cache Consideration

In-Memory cache allows storing data within the application's memory.

We all know using a caching strategy can drastically improve performance and it's quite reasonable as you're getting data from memory not external sources.

The flow is straight, trying to get data from the cache if data is presented then return it directly to the caller otherwise call an external DataSource, update the cache, and finally return the data to the caller, no magic!

Here is a very simple code for the implementation:

public class CacheExample
{
    private readonly IMemoryCache _memoryCache;

    public CacheExample(IMemoryCache memoryCache)
    {
        _memoryCache = memoryCache;
    }

    public T? GetDataFromCacheOrSource<T>(string cacheKey)
    {
        // Try to get data from the cache
        if (_memoryCache.TryGetValue(cacheKey, out T? cachedData))
        {
            // Data found in cache, return it
            return cachedData;
        }

        // Data not found in cache, fetch it from the source
        var dataFromSource = GetExpensiveDataFromSource<T>();

        // Store the fetched data in the cache
        var cacheEntryOptions = new MemoryCacheEntryOptions
        {
            // Cache data for 30 minutes
            AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5)
        };

        _memoryCache.Set(cacheKey, dataFromSource, cacheEntryOptions);

        return dataFromSource;
    }
}

Here is the flow of getting data from using cache or external source:

Let's talk about some consideration:

  • Sticky Sessions: When using in-memory caching in a server farm (multiple servers), ensure that sessions are sticky. Sticky sessions route requests from a client to the same server, maintaining cache consistency.

  • Memory Resource: Remember that memory is a scarce resource. Limit cache growth to avoid excessive memory usage.

  • Expiration and Size Control: Use expirations and methods like SetSize to manage cache size. The ASP.NET Core runtime does not automatically limit cache size based on memory pressure; developers must handle this.

1
An error has occurred. This application may no longer respond until reloaded. Reload x