Redis Clients :: ASP.NET Core
In the “Redis Clients” post; we explored what it takes to use Redis and how it can be helpful in our applications. We also utilized Redis datatypes and abstractions and how it can be used for page / visitor counters required in web applications. In the “Dotnet Core” post we saw that given the open source version of Dotnet now works in Linux; we can deploy the Dotnet Core applications into the Docker Containers. We even made the simple ASP.NET Core application and connect it to Redis and experienced that we can use Redis as the Distributed Cache backend using the Microsoft.Extensions.Caching.Redis.Core Nuget package that uses the StackExchange.Redis Redis Client library.
They also opened source the MVC framework and we can setup the ASP.NET Core MVC project using the Dotnet Core CLI; dotnet new mvc. We can have Middlewares; that handle the requests and responses. You can learn more about Middlewares at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware and there is a StartTimeHeader middleware at https://docs.microsoft.com/en-us/aspnet/core/performance/caching/distributed that uses the Distributed Caching feature of ASP.NET Core and Redis as the backend. On the same lines, for the Visitor Counter using Redis that we did in “Redis Clients”; we can have a RedisVisitorMiddleware that does its counting and we can have this cross cutting concern handled separately in its own class that can be glued into the MVC application in the Startup.cs
If we are using Micro Services Architecture and the final application will get deployed on Containers; the Redis Server will be running on a remote node; unfortunately we cant use the host name for Redis Cache Server and it will throw Platform Not Supported exception. We will have to resolve the host name and give its IP for the Redis Configuration in Startup.cs; we can have the static property for RedisConnection in Startup.cs; something like this
The hit counter code can be moved to relevant Controller / Action that can use Startup.RedisConnection static property to access Redis. We can use the same property to setup the Redis Distributed Caching provider
We can use Distributed Caching for Page Caching. If there is any Page that takes considerable time to “generate” and content of that page is not dynamic or change rarely; we can use Redis to store the generated page and reuse it from there. ASP.NET Core MVC has concept of Views and Partial Views; we can use Distributed Caching to cache them; for the testing; lets setup a Partial View in ~Views/Shared/ and for proof of concept we can use Thread.Sleep emulating time that it takes to generate.
Tag Helpers in ASP.NET Core MVC enables server side code to participate in creating and rending HTML elements in Razor files. You can learn about them at https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro and there is a DistributedCacheTagHelper (code @ https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.TagHelpers/DistributedCacheTagHelper.cs) that we can use for Partial View Caching.
Now to deploy our application into the Dockers; if we are doing it on a single host and having two containers; we will have the following v2 Compose file that we can use with Docker Compose tool
The Dockerfile for our MVC application will be something like this:
- Before building the Docker image using the above Dockerfile; we need to have the published application in the "output" folder; by running; dotnet restore (if required) and dotnet publish -c Release -o output