Getting started with Redis for Windows

TL;DR - Download redis installer: Redis-64 (2.8.17)

Introduction

Redis is an open source, BSD licensed, advanced key-value cache and store that you can read more about on redis.io. There are various video introductions to it, including Channel 9's Getting Started and Azure Friday's Introduction to Redis.

Obtaining Redis for Windows

Microsoft OpenTech is distributing Windows ports of Redis. Whereas the team is working on adding support in the Microsoft Web Installer and have had various links to installs out there, it's not easy to find a go-to spot to download the latest stable version. If you are using Chocolatey, you can install redis into a local directory by executing

choco install redis-64

Since Chocolatey is built on top of NuGet, if you'd rather get the package from within Visual Studio, run 

PM> Install-Package Redis-64

from the package manager console. It will download and extract the Redis executables in a Redis-64* sub directory of your packages:

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        2014-11-26     09:37      18388 Redis on Windows.docx
-a---        2014-11-26     09:37      13549 Redis Release Notes.docx
-a---        2014-11-26     09:37    1408246 Redis-64.2.8.17.nupkg
-a---        2014-11-26     09:37     444888 redis-benchmark.exe
-a---        2014-11-26     09:37     302552 redis-check-aof.exe
-a---        2014-11-26     09:37     314328 redis-check-dump.exe
-a---        2014-11-26     09:37     487896 redis-cli.exe
-a---        2014-11-26     09:37    1393624 redis-server.exe
-a---        2014-11-26     09:37      29147 redis.windows.conf
-a---        2014-11-26     09:37      16038 RedisService.docx


Installing Redis as a service

To install redis as a service on your machine, run 

> redis-server --service-install

Similarly, to uninstall, pass the --service-uninstall. There's an accompanying document - RedisService.docx - describing the configuration options available.

To simplify the process for system administrators, I've built an .MSI package that installs the NuGet package mentioned above into ProgramFiles\Redis-64 (by default), adding shortcuts to your start menu.

Getting started

Once you've obtained the Redis server, you can follow the videos linked in the introduction or simply create a new Visual Studio project, adding a client library:

PM> Install-Package StackExchange.Redis

... and start typing away:

using System;
using System.Globalization;
using Newtonsoft.Json;
using StackExchange.Redis;

namespace Console
{
    class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public override string ToString()
        {
            return FirstName + " " + LastName;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            using (var connection = ConnectionMultiplexer.Connect("localhost"))
            {
                var database = connection.GetDatabase();
                database.StringSet("server-started-at", DateTime.UtcNow.ToString(CultureInfo.InvariantCulture));

                var subscriber = connection.GetSubscriber();

                subscriber.Subscribe("names", (channel, value) =>
                {
                    var person = JsonConvert.DeserializeObject("{" + value + "}");

                    System.Console.WriteLine(person.ToString());
                });


                System.Console.ReadKey();
            }
        }
    }
}

(I'm using the Newtonsoft.Json package to do JSON serialization for this example).

Running our app and then starting the redis client app, we can see that the default connectivity works (server is running on the default port, the client library and client app both connect to the same default port):

> ./redis-cli
127.0.0.1:6379> get server-started-at
"11/26/2014 09:17:11"

We can also publish a semi-JSON person (I had issues with typing { and } in my PowerShell :-P) object into our app through its names subscription:

127.0.0.1:6379> publish names "firstName: 'Ronald', lastName: 'McDonald'"
(integer) 1

... instantly seeing 

Ronald McDonald

as output from our client app.

For a listing of all of redis' commands, see http://redis.io/commands.

OK, now you're up and running - go have fun!




Comments

Popular posts from this blog

Auto Mapper and Record Types - will they blend?

Unit testing your Azure functions - part 2: Queues and Blobs

Testing WCF services with user credentials and binary endpoints