Posts

....
Technical Blog for .NET Developers ©

Saturday, April 27, 2024

Redis Service Stack

ServiceStack's C# Redis Client is a simple, high-performance and feature-rich C# Client for Redis with native support and high-level abstractions for serializing POCOs and Complex Types supporting both native Sync and Async APIs

In this post we are installing Redis Server on WSL for Windows 11 OS, and using it from .NET Core Web Application

As a first step we are installing WSL from PowerShell with Admin privileges elevated

     
    wsl --install    
    




Now we have installed Wsl we need to update apt-get

 
  	sudo apt-get update
	sudo apt-get upgrade
  


Then we can install Redis server database

 
  sudo apt-get install redis-server
  




We have already installed Redis Server, now we are using it from our web application demo

 
    using ServiceStack.Redis;
  


 
        private IRedisClient GetRedisClient()
        {
            string conString = "redis://localhost:6379?ConnectTimeout=5000&IdleTimeOutSecs=180";
            RedisManagerPool manager = new(conString);
            return manager.GetClient();
        }  
    


 
            var client = GetRedisClient();
            var bookId = int.Parse(Request.Form["bookId"]);

            if (!client.GetAllItemsFromList("cart").Contains(bookId.ToString()))
            {
                var book = _context.Books.Find(bookId);
                book.InStock--;
                _context.SaveChanges();

                client.AddItemToList("cart", bookId.ToString());
            } 
    





<METHOD SOFTWARE 2024>