Redis Tutorial with ServiceStack.Redis



I thought it's about time for another quickstart tutorial, this time on using Redis in C# making use of the ServiceStack API on our beloved .Net.

What you'll need

Before we start I should say running the Redis server on Windows is not recommended for production for testing and learning it's fine. But for Live environments I believe only Linux has been tested properly.
Step 1 - UnZip Redis

Put Redis anywhere you want really, I've just got it running as a Console application, so it's not a Windows Service as you're probably thinking but it's good enough to play with.


Run the redis-server.exe and you should see the server window popup in a console window.

So in the picture above you can see Redis is now up and running and is listening on port 6379.

Step 2 - Playing with the Redis Command Line Interface

In the same folder as the service run redis-cli.exe which will popup another console application. In this you can experiment with the raw redis commands against your server. I'd recommend the interactive Try Redis tutorial to get you started.

To start off just type: set car.make "Ford" which will add a simple key which is car.make and will give it a value of "Ford" . To get it out of Redis simply type GET car.make and it will return "Ford". Easy isn't it.

Step 3 - ServiceStack.Redis Example

Firstly create a new Console application in Visual Studio. Next unzip the ServiceStack.Redis-v2.09.zip into a lib folder. Then in Visual Studio add references to:

  • ServiceStack.Common
  • ServiceStack.Interfaces
  • ServiceStack.Redis
  • ServiceStack.Text

Step 4 - Write Some Code

So in great tradition we're going to create a Car class and store several instances of it in Redis Expire an old Astra and then get back all the Ford cars.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using ServiceStack.Redis;
using ServiceStack.Text;

namespace RedisTutorial.CLI
{
    class Program
    {
        static void Main(string[] args)
        {
            var redisClient = new RedisClient("localhost");

            using (var cars = redisClient.GetTypedClient())
            {
                if(cars.GetAll().Count>0)
                    cars.DeleteAll();

                var dansFord = new Car
                               {
                                   Id = cars.GetNextSequence(),
                                   Title = "Dan's Ford",
                                   Make = new Make {Name = "Ford"},
                                   Model = new Model {Name = "Fiesta"}
                               };
                var beccisFord = new Car
                {
                    Id = cars.GetNextSequence(),
                    Title = "Becci's Ford",
                    Make = new Make { Name = "Ford" },
                    Model = new Model { Name = "Focus" }
                };
                var vauxhallAstra = new Car
                                   {
                                       Id = cars.GetNextSequence(),
                                       Title = "Dans Vauxhall Astra",
                                       Make = new Make {Name = "Vauxhall"},
                                       Model = new Model {Name = "Asta"}
                                   };
                var vauxhallNova = new Car
                {
                    Id = cars.GetNextSequence(),
                    Title = "Dans Vauxhall Nova",
                    Make = new Make { Name = "Vauxhall" },
                    Model = new Model { Name = "Nova" }
                };
                                
                var carsToStore = new List { dansFord, beccisFord, vauxhallAstra,vauxhallNova };
                cars.StoreAll(carsToStore);
                
                Console.WriteLine("Redis Has-> " + cars.GetAll().Count + " cars");
                Console.WriteLine(cars.GetAll().Dump());

                cars.ExpireAt(vauxhallAstra.Id, DateTime.Now.AddSeconds(5)); //Expire Vauxhall Astra in 5 seconds
                
                Thread.Sleep(6000); //Wait 6 seconds to prove we can expire our old Astra

                Console.WriteLine("Redis Has-> " + cars.GetAll().Count + " cars");
                Console.WriteLine(cars.GetAll().Dump());
                
                //Get Cars out of Redis
                var carsFromRedis = cars.GetAll().Where(car => car.Make.Name == "Ford");

                foreach (var car in carsFromRedis)
                {
                    Console.WriteLine("Redis Has a ->" + car.Title);
                }

            }                      
            Console.ReadLine();
        }
    }

    public class Car
    {
        public long Id { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public Make Make { get; set; }
        public Model Model { get; set; }
    }

    public class Make
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class Model
    {
        public int Id { get; set; }
        public Make Make { get; set; }
        public string Name { get; set; }
    }
}

It's that simple.

 

 


blog comments powered by Disqus

Search

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2014