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,
- Cache
- Throttling/Debouncing
- Managing component re-rendering
On the backend side following are the techniques,
- Cache
- Managing smooth connection
- 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.
Once open the folder either 32bit or 64bit, you can see multiple Redis exe files.
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.
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.
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.
Let’s start using Redis in our .Net console application.
Create a new 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.
Browse “redis” and select StackExchange.Redis to act as Redis client to connect Redis with the application. Install StackExchange.Redis package in your project.
To make a connection between Redis and the application two steps needs to be done
- Set up the connection bypassing the server name where Redis is hosted.
- 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.
- static void Main(string[] args) {
-
- ConnectionMultiplexer conn = ConnectionMultiplexer.Connect("localhost");
-
- IDatabase database = conn.GetDatabase();
-
- database.StringSet("redisKey", "redisvalue");
-
- var value = database.StringGet("redisKey");
- Console.WriteLine("Value cached in redis server is: " + value);
- Console.ReadLine();
- }
If you run the above solution, you will get the expected value of a particular key.
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 :)