Redis Clients

Published on Friday, March 10, 2017

Redis Series

For the Redis clients; imagine we have an e-commerce platform having a Python based component that does some analysis to show which product or campaign / deal to show on the main page; these results are posted / updated into the Redis Server from where the Asp.Net Core application picks them.

For the Python client; we need pip; a PyPa recommeded tool for installing Python packages; on Ubuntu this can be done using the following command

sudo apt-get update && sudo apt-get install python-dev python-pip

Once pip is available; give this command

sudo pip install redis

Now to connect to Redis Server; we will have a code like this

  • You can see that we are simply adding the product ids and offer ids into the cache; the web interface will retrieve the data from the database and render it accordingly. If we want to; the web application can cache the rendered HTML as well and reuse it to save database trips for performance improvement

We can install Python on Windows development machine and use Visual Studio Code; there is a nice Python extension available at https://marketplace.visualstudio.com/items?itemName=donjayamanne.python that provides linting, intellisense and what not

redisclient-python

For the .NET Core; we can use https://www.nuget.org/packages/StackExchange.Redis Nuget package that’s .NETStandard compatible. This is very famous Redis client library from StackOverFlow guys and its code is available at https://www.nuget.org/packages/StackExchange.Redis

If our application is ASP.NET Core; we can instead use https://www.nuget.org/packages/Microsoft.Extensions.Caching.Redis.Core package; which is Distributed cache implementation of Microsoft.Extensons.Caching.Distributed.IDistributedCache using Redis; its an interface for Distributed cache mechanism basked into the ASP.NET Core to improve the performance and scalability of the applications. This package uses Strong Name version of StackExchange.Redis and to add it into the ASP.NET Core application; use dotnet add package Microsoft.Extensions.Caching.Redis.Core command

For our simple proof of concept; given the command dotnet new web in your project folder

It creates a very minimalist Hello World web application; to use the Static Files; give dotnet add package Microsoft.AspNetCore.StaticFiles command. For using Session; give dotnet add package Microsoft.AspNetCore.Session; and finally give dotnet add package Microsoft.Extensions.Caching.Redis.Core command. Restore the packages using dotnet restore and change the Startup.cs to this

  • redisclient-dotnetAs per StackExchange.Redis recommendation; we can reuse the ConnectionMultiplexer instance; therefore its defined as the static variables
  • Its initialized in the static constructor with ConfigurationOptions through which we defined the Redis Server and its password information
  • In the ConfigureService(IServiceCollection); the Redis Caching Extensions is added; and Redis Servir and password information is again specified while adding it
  • The Session service is also added in the ConfigureService according to its requirements
  • In the Configure(IApplicationBuilder, IHostingEnvironment, ILoggerFactory) method that gets called by the .NET Core runtimes for HTTP request pipeline; we are attaching the StaticFiles and Session extensions as per ASP.NET Core’s app.Use* conventions

We are using Redis in the code above in our ASP.NET Core application for two purposes; unique visitor counter and page hit counter. Page Hit Counter is the simple INCR Redis command. For Unique visitor we are using Cookie and a Sets support of Redis; SADD for adding the visitor and SCARD to determine the length of the set. StackExchange.Redis APIs are StringIncrement, SetAdd and SetLength respectively. Using Sets; we dont have to worry about duplicates as Redis automatically takes care of it and we can continue to add into the set with same id and it will not allow duplicates.