Caching Techniques in C# .NET Core: A Step-by-Step Guide

Caching Techniques in C# .NET Core: A Step-by-Step Guide

Caching is a technique that stores frequently accessed data in a temporary storage location to improve the performance of an application. In C# .NET Core, there are several ways to implement caching, and in this guide, we will explore the most popular techniques. Here are the cache implementations in C# .NET Core.

In-Memory Caching

In-memory caching is the simplest and most efficient caching technique, as it stores data in the memory of the application. The data is stored in a Dictionary object, which is accessed using a key-value pair. To implement in-memory caching, you can use the Microsoft.Extensions.Caching.Memory package.


using Microsoft.Extensions.Caching.Memory;

IMemoryCache cache = new MemoryCache(new MemoryCacheOptions());
cache.Set("key", "value", TimeSpan.FromMinutes(10));

Distributed Caching

Distributed caching is a technique that stores data in a shared cache that can be accessed by multiple servers. This technique is useful in web farm scenarios, where multiple servers are used to handle the load of an application. To implement distributed caching, you can use the Microsoft.Extensions.Caching.Distributed package.


using Microsoft.Extensions.Caching.Distributed;

IDistributedCache cache = new RedisCache(new RedisCacheOptions());
byte[] value = Encoding.UTF8.GetBytes("value");
cache.Set("key", value);

SQL Server Caching

SQL Server caching is a technique that stores data in a SQL Server database. This technique is useful when the data needs to be shared across multiple servers or when the data needs to be persisted. To implement SQL Server caching, you can use the Microsoft.Extensions.Caching.SqlServer package.


using Microsoft.Extensions.Caching.SqlServer;

SqlServerCache cache = new SqlServerCache(new SqlServerCacheOptions());
cache.Set("key", "value", TimeSpan.FromMinutes(10));

when using SQL Server caching in C# .NET Core, you need to configure the connection string to access the database. The connection string is typically defined in the appsettings.json file or in the configuration file of your application.

For example, in the appsettings.json file, you can add the following JSON object to define the connection string:

"SqlServerCacheOptions": {
  "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=CacheDB;Trusted_Connection=True;"

And then in the Startup.cs file, you can configure the SQL Server caching service in the ConfigureServices method:

services.AddDistributedSqlServerCache(options =>
    options.ConnectionString = Configuration.GetConnectionString("SqlServerCacheOptions:ConnectionString");
    options.SchemaName = "dbo";
    options.TableName = "CacheTable";

In this example, I used AddDistributedSqlServerCache which is used for distributed caching and also defined the schema name and table name for the cache table.

Cache Update and Delete

To update and delete cache, you can use the Set and Remove methods provided by the cache implementation.


cache.Set("key", "new value", TimeSpan.FromMinutes(10));

Avoiding Hardcoded Cache Keys

It is a best practice to avoid hardcoding cache keys as it can lead to maintenance issues and make the code less readable. A good way to avoid hardcoded keys is to use a constant or a string variable to store the key.


const string Key = "key";
cache.Set(Key, "value", TimeSpan.FromMinutes(10));

Final Words

Caching is an important technique that can significantly improve the performance of an application. C# .NET Core provides several caching techniques, including in-memory caching, distributed caching, and SQL Server caching. Each technique has its own advantages and use cases, and it is important to choose the right one for your specific scenario.

In this guide, we have provided examples of how to implement caching in C# .NET Core, including how to store, update, and delete cache, and how to avoid hardcoded cache keys for optimal performance. By following these techniques, you can improve the performance of your application and provide a better user experience.

Post a Comment

Previous Post Next Post