Redis Cache DB setup with C#

 Redis is a NoSQL key-value cache that stores the information in a hash table format, providing the possibilities to store different types of structured data like strings, hashes, lists, sets, sorted sets, bitmaps and hyperloglogs

Installing REDIS on Windows 

Officially Redis doesn't have an official version for windows, but being an open-source project, there is a fork by MSOpenTech where there is the possibility of running Redis on Windows.

 Requirements to install Redis:

  • 64 bit OS
  • The windows package manager Chocolatey

In order to install Redis on Windows using Chocolatey, we need to run a simple command with the command prompt (as administrator) and just follow the instructions:

C:\> choco install redis-64 

Once the installation is complete, we can run the Redis server instance using the command redis-server.exe. If you have problems running the command and get the error "Redis-server is not recognized as an internal...etc" is because chocolatey failed registering the system path variable indicating where Redis is. You can fix it with the following command:

SET PATH=%PATH%;"c:\Program Files\Redis" 

Now we can run the redis-server.exe to start our Redis instance.

Connecting using C#

 I will be using Visual Studio 2015 Community Edition and I'm sure you already have it, if not you can download it from here.

 We are going to create a Console Application using .NET 4.5 or higher (this is important) naming the project RedisConnectionTest:



Having this empty project, we need a Nuget package that is our connector to Redis(StackExchange.Redis). Using the Visual Studio tools, we add this NuGet package:



Once this package is installed, we are going to create a new class named RedisConnectorHelper.cs. In order to have an easy way to manage our connection to Redis, we type the following: 





The connection to Redis is handled by the ConnectionMultiplexer class. To connect to the Redis instance we use the static method ConnectionMultiplexer.Connect, that takes a string parameter with the connection string.

ConnectionMultiplexer was designed for code sharing by the whole application, is not necessary to create a new instance every time you need a simple operation. Creating a new instance every time we need a cache, the performance may be affected. In addition, if we are using Redis cache from Azure, there is a limit connection so if we create a new connection each time we need the cache, this limit can be exceeded.

An easy way to share the Multiplexer instance is using a static property. This class was designed to be used like this, singleton and thread-safe.

Setting and Getting data from the cache




With the SaveBigData method we save 10,000 items with random values between 0 and 10,000. The important here is how we use our helper connection class. 

We use the method GetDatabase from the Multiplexer to access the cache. This instance has too many methods: can read, save, delete, increase data, concatenate and finally, all the commands that Redis has. We use StringSet to save a string (or any primitive type) using the "Device_Status: NUMBER" as a key.



Comments

Popular posts from this blog

Email Sending through O365 using OAuth Protocol

IoT Technology

What is reverse proxy?