Using Redis Caching in your ASP.NET Core web app


Join the mailing list to stay up to date with published articles


Video Summary

Welcome back! In our previous videos, we covered the installation of Ubuntu on Windows 11 and explored how to install Redis Server on that Ubuntu instance. Today, we’ll delve into a quick Blazor application that demonstrates how to read and write values from our Redis Server. Let’s dive in!

Step 1: Running the Blazor Application When we run the app, we can navigate to the “Get Cache” page, where we currently have no items in the cache. Then, we can move to the “Set Cache” page, enter a key-value pair, and set an expiration time of one hour. Returning to the “Get Cache” page, we can now see the value we set. Additionally, we have a “Remove Cache” option to delete the cached item.

Step 2: Understanding the Cache Service In the code, we have a cache service that uses the default ASP.NET Core in-memory cache. Although this is suitable for development purposes, it is not recommended for production as it lacks visibility and scalability. The cache service includes functions to get, remove, and set cache values based on the provided key.

Step 3: Checking Redis Service Status To ensure the Redis service is running, we can access our Ubuntu instance and execute the command “sudo service redis-server status.” If the service is stopped, we can start it using the command “sudo service redis-server start.” Additionally, we can use Redis Insight, which we explored in a previous video, to manage our Redis instance.

Step 4: Adding Redis Cache Configuration To utilize Redis cache, we need to add the necessary packages. In Visual Studio, we can right-click on dependencies, select “Manage NuGet Packages,” and search for “Microsoft.Extensions.Caching.StackExchangeRedis.” After installing the package, we can add the Redis connection string to the app settings file, mentioning that it should be stored in user secrets. We can then configure the connection string in the program.cs file.

Step 5: Configuring Redis Cache In the program.cs file, we add the Redis cache service to the dependency injection container. By supplying the connection string, we enable the Redis cache functionality. Now, when we run the application, we can see the cache values reflected in Redis Insight.

Conclusion: Congratulations! You’ve successfully integrated Redis Server with your Blazor application on Ubuntu Linux using the Windows Subsystem for Linux (WSL) on Windows 11. By leveraging Redis cache, you gain enhanced visibility and control over your cached data during the development process. If you haven’t watched the previous videos on installing Ubuntu and Redis cache, don’t forget to check the links provided. Remember to like the video and subscribe to our channel for more exciting content. Thank you for watching!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top