Getting started with Redis in ASP.NET under Windows

March 25, 2012 · Posted in Development 

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:

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

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

sc delete Redis
 

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:

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

 

You can find a lot of examples on ServiceStack github page

Comments