Implementation Of Redis Cache In .NET Console Application

Introduction

 
In most applications, performance is the key factor to keep attracting end-users to use that application, and to make it performance-efficient there are a lot of techniques that need to be followed at various points.

On the frontend side of things, following are the techniques,
  1. Cache
  2. Throttling/Debouncing
  3. Managing component re-rendering
On the backend side following are the techniques,
  1. Cache
  2. Managing smooth connection
  3. Memory leakage
So, as you can see caching data is one of the most-used and efficient techniques to make the performance of our application efficient.

To talk more about caching, we will discuss the Backend caching technique with the help of Redis.

Redis is an open-source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyper logs, geospatial indexes with radius queries, and streams.
 
Let's start by installing Redis on our machines.

If you are using the Windows operating system then you need to download the Redis zip folder from the open-source GitHub account, otherwise you can download it from redis.io/ downloads URL.

I am using the Windows operating system so I downloaded the Redis zip folder and extracted it.
 
There you can select either 32bit or 64bit according to your machine configurations.
 
Implementation Of Redis Cache In .NET Console Application
 
Once open the folder either 32bit or 64bit, you can see multiple Redis exe files.
 
Implementation Of Redis Cache In .NET Console Application
 
To start the Redis in our local machine, we need to start the Redis server first, and to do that double-click the Redis-server.exe file.
 
Implementation Of Redis Cache In .NET Console Application
 
Once the Redis server is up and running then open Redis-cli.exe to perform the operations or just to verify the server.

Redis server will default run on port 6379. You can ping the server and in response, the server will return PONG. 
 
Implementation Of Redis Cache In .NET Console Application
 
You can perform all the Redis operations through Redis-CLI just like setting and getting the values with the help of key, and many more operations.
 
Implementation Of Redis Cache In .NET Console Application
 
Let’s start using Redis in our .Net console application.

Create a new console application.
 
Implementation Of Redis Cache In .NET Console Application
 
To use the Redis in our application, we need to install the Redis package in our project and for that, you can right –click on the solution and select Manage NuGet Packages for the solution.
 
Implementation Of Redis Cache In .NET Console Application
 
Browse “redis” and select StackExchange.Redis to act as Redis client to connect Redis with the application. Install StackExchange.Redis package in your project.
 
Implementation Of Redis Cache In .NET Console Application
 
To make a connection between Redis and the application two steps needs to be done
  1. Set up the connection bypassing the server name where Redis is hosted.
  2. Get connected with the databases of the Redis server
After establishing the Redis connection, you can perform any data related operations on the Redis database instance just like setting up Redis cache key-value and getting the values.

Through this, once you can retrieve the data from your database then you can store that data in Redis which is much faster in filtering the data and avails the data whenever it's required without making database calls.
  1. static void Main(string[] args) {  
  2.     //setting up connection to the redis server  
  3.     ConnectionMultiplexer conn = ConnectionMultiplexer.Connect("localhost");  
  4.     //getting database instances of the redis  
  5.     IDatabase database = conn.GetDatabase();  
  6.     //set value in redis server  
  7.     database.StringSet("redisKey""redisvalue");  
  8.     //get value from redis server  
  9.     var value = database.StringGet("redisKey");  
  10.     Console.WriteLine("Value cached in redis server is: " + value);  
  11.     Console.ReadLine();  
  12. }  
Implementation Of Redis Cache In .NET Console Application
 
If you run the above solution, you will get the expected value of a particular key.
 
Implementation Of Redis Cache In .NET Console Application
 
You see how easy it is to use Redis cache in our application.
 
Hope your application will give you the desired output.
 
Thanks
 
Keep Learning :)