Getting started with Redis in ASP.NET under Windows

Redis is one of the fastest and most feature-rich in-memory key value data stores.

Install Redis server under Windows

Redis was primarily developed on *NIX operating systems. If you’re looking to install on a *NIX platform the best place to download Redis is still from the redis project website.

Start from Redis project download page. The Redis project does not directly support win32/win64. Unofficial versions of Redis for Windows are available for downloading:

Seems like the Windows Redis port by Dušan Majkić is the most popular. You can download it from here.

 

Install Redis server as a service under Windows

If you run Redis on Windows machine, the best option is to run each Redis instance as Windows service.

You can install the Windows Redis port by Dušan Majkić as follows:

– Download the Windows Redis port by Dušan Majkić.

– Extract it to C:\Program Files\Redis or any other directory.

– Download the Windows Redis service program by Konstantin Cherenkov and put it into the same directory. So you have the file

C:\Program Files\Redis\RedisService.exe

– Run the following to install the service:

[codesyntax lang=”bash”]

sc create Redis start= auto DisplayName= Redis binpath= "\"C:\Program Files\Redis\RedisService.exe\" \"C:\Program Files\Redis\redis.conf\""

[/codesyntax]

where “Redis” can be any name of your service.

– Run the service. Go to your Windows Management / Services and run the service and/or make it run Automatically after the system starts.

– To uninstall service run

[codesyntax lang=”bash”]

sc delete Redis [/codesyntax] 

where “Redis” is the name of the installed service.

 

The instructions are taken from How to Run Redis as a Service under Windows and Run Redis as Service on Windows.

 

C# .NET Client Library

ServiceStack client library:

ServiceStack’s C# Redis Client is an Open Source C# Redis client based on Miguel de Icaza previous efforts with redis-sharp.

Download ServiceStack from  github page.

Also you can download the Redis Client in any one of the following ways:

  • Packaged by default in ServiceStack.dll
  • Available to download separately as a stand-alone ServiceStack.Redis.dll
  • As Source Code via Git: git clone git://github.com/ServiceStack/ServiceStack.Redis.git
  • For those interested in having a GUI admin tool to visualize your Redis data should check out the Redis Admin UI

 

Overview of  ServiceStack

ServiceStack has several interfaces:

  • ICacheClient – If you are using Redis solely as a cache, you should bind to the ServiceStack’s common interface as there already are In-Memory an Memcached implementations available in ServiceStack, allowing you to easily switch providers in-future.
  • IRedisNativeClient – For those wanting a low-level raw byte access (where you can control your own serialization/deserialization) that map 1:1 with Redis operations of the same name.

For most cases if you require access to Redis-specific functionality you would want to bind to the interface below:

  • IRedisClient – Provides a friendlier, more descriptive API that lets you store values as strings (UTF8 encoding).
  • IRedisTypedClient – created with IRedisClient.GetTypedClient<T>() – it returns a ‘strongly-typed client’ that provides a typed-interface for all redis value operations that works against any C#/.NET POCO type.

Classes:

  • The RedisNativeClient exposes raw byte[] apis and does no marshalling and passes all values directly to redis.
  • The RedisClient assumes string values and simply converts strings to UTF8 bytes before sending to Redis
  • The RedisTypedClient provides a generic interface allowing you to add POCO values. The POCO types are serialized using .NETs fastest JSON Serializer which is then converted to UTF8 bytes and sent to Redis.

Read the tutorial about using ServiceStack here.

 

Examples

Caching using Redis and ServiceStack client library:

[codesyntax lang=”csharp”]
using ServiceStack.Redis;

//
RedisClient redisClient = new RedisClient(“localhost”);

// store some value in cache for one hour

RedisClient.Set(“mykey_1” + name, 15, TimeSpan.FromSeconds(3600));

// get typed value from cache

int valueFromCache = RedisClient.Get<int>(“mykey_1”); // must be = 15

[/codesyntax]

 

You can find a lot of examples on ServiceStack github page

4 thoughts on “Getting started with Redis in ASP.NET under Windows”

  • Admirador de Damas says:

    Any good getting started with it? step by step in 10 minutes?

    Updates in 2015 ? lessons learned and patterns-practices?

    IMHO, better samples for minimize learning curve are real applications with full source code and good patterns.

    Full source code sample REAL application? not Demo, only real applications?
    Like for small to medium applications development, but open source, if you can sharing or maybe another real project or production-ready application using.

    main influences, are full of innovative ideas that can free our minds to explore new techniques, patterns and paradigms.

    You want to use technologies that allow for rapid development, constant iteration, maximal efficiency, speed, robustness and more. You want to be lean and you want to be agile. You want to use technologies that will help you succeed in the short and long term. And those technologies are not always easy to pick out.

  • kiquenet kiquenet says:

    Suggestions: full source code useful, complete and sample using patterns & practices about it

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>