Caching

The cache is the memory storage that is used to store the frequent access data into the temporary storage, it will improve the performance drastically and avoid the unnecessary database hit and store frequently used data into the buffer whenever we need it.

Basically, there are two types of caching .NET Core supports: In-Memory Caching and Distributed Caching

In-Memory Caching

In-Memory Caching in ASP.NET Core is the simplest form of cache in which the application stores data in the memory of the webserver.\ It runs in-process, so it’s fast.

Distributed Caching

Basically, in the distributed caching, data are stored and shared between multiple servers. Also, it’s easy to improve scalability and performance of the application after managing the load between multiple servers when we use multi-tenant application Suppose, In the future, if one server is crashed and restarted then the application does not have any impact because multiple servers are as per our need if we want Redis is the most popular cache which is used by many companies nowadays to improve the performance and scalability of the application. So, we are going to discuss Redis and usage one by one.

Redis

Redis is an Open Source (BSD Licensed) in-memory Data Structure store used as a database. Basically, it is used to store the frequently used and some static data inside the cache and use and reserve that as per user requirement. There are many data structures present in the Redis which we are able to use like List, Set, Hashing, Stream, and many more to store the data.

Run Redis Stack on Docker

Startup

Note: AddDistributedMemoryCache() is used to add a default implementation of Redis in developement environement. It's not mandatory to setup a Redis server.

private static void AddRedis(IServiceCollection services)
{
    services.AddDistributedMemoryCache();
    string redisServer = AppSettingsHelper.GetSetting("RedisCacheServerUrl");

    if (!string.IsNullOrEmpty(redisServer))
    {
        services.AddStackExchangeRedisCache(options => options.Configuration = redisServer);
    }
}

Service

public class RedisCacheService : IRedisCacheService
{
    private readonly IDistributedCache _distributedCache;

    public RedisCacheService(IDistributedCache distributedCache)
    {
        _distributedCache = distributedCache;
    }

    public async Task<List<TEntity>> RetrieveItems<TEntity>(string cacheKey, IQueryable<TEntity> query) where TEntity : class
    {
        List<TEntity> items;
        var redisItems = await _distributedCache.GetAsync(cacheKey);

        if (redisItems != null)
        {
            string serializedRetrievedItems = Encoding.UTF8.GetString(redisItems);
            items = JsonSerializer.Deserialize<List<TEntity>>(serializedRetrievedItems);
        }
        else
        {
            items = await query.ToListAsync();

            var serializerOptions = new JsonSerializerOptions()
            {
                ReferenceHandler = ReferenceHandler.IgnoreCycles
            };
            string serializedItems = JsonSerializer.Serialize(items, serializerOptions);
            redisItems = Encoding.UTF8.GetBytes(serializedItems);

            var options = new DistributedCacheEntryOptions()
                .SetAbsoluteExpiration(TimeSpan.FromMinutes(10)) // this means that cache will be stayed upto 10 min.
                .SetSlidingExpiration(TimeSpan.FromMinutes(720)); // this means that if search page not used/refresh in 2 min, cache will be expired.

            await _distributedCache.SetAsync(cacheKey, redisItems, options);
        }

        return items;
    }

    public void RemoveByKey(string key)
    {
        _distributedCache.Remove(key);
    }
}

References

results matching ""

    No results matching ""