Easier Redis development with scriptcs and ServiceStack.Redis powered C# REPL - StrathWeb

Strath

December 9th, 2013

Easier Redis development with scriptcs and ServiceStack.Redis powered C# REPL

Work with Redis like a pro

In my current project I am working a lot with Redis on the back end. What I have found extremely useful is the ability to work with Redis directly from its REPL (redis-cli), to instantly check or reset the objects stored there.

However, working in the C# environment, the basic redis-cli is definitely not enough to facilitate productive development.

Enter scriptcs.

ServiceStack.Redis

Probably the most common and most powerful way of integrating Redis into your C# backend is to use the wonderful ServiceStack.Redis library.

It exposes both the typed client – for dead-easy use in your code, and a native client, if you wish a more granular, low-level ability to communicate with Redis. Under the hood, it uses the fastest JSON serializer out there, ServiceStack.Text, to send and read your objects from Redis.

SsRedis script pack for your Redis server

Since scriptcs already offers a powerful C# REPL, it was a no-brainer that combining with ServiceStack.Redis could result in dramatic benefits in terms of facilitating development and testing.

With scriptcs, you can simply fire up C# REPL, include the relevant ServiceStack libs, perhaps reference your own DLLs (such as the ones containing the models/DTOs you store in Redis) and interact with the Redis server straight away, in any way you wish.

The obvious advantage over the raw redis-cli is the ability to work with your custom types directly.

While scriptcs allows you to use pretty much any .NET assemblies you want to, the notion of script packs makes things slightly easier – so I quickly created a script pack for Redis, as a tiny wrapper around ServiceStack.Redis. It’s called ScriptCs.SsRedis and is available here on Github and on Nuget:

Here is a short example:

BatpZ3ACQAE8rnz

So you can simply Require the service pack, and point it to the Redis host(s) (default being obviously localhost:6379). No using statements are necessary, since script pack takes care of everything. Then you can use any of the methods that ServiceStack’s IRedisClient exposes – both strongly typed ones and the low level ones too.

What you have at your disposal now, is an interactive tool allowing you to tinker and inspect the data in Redis at any point you wish – and in my recent work, this has been an absolutely lifesaver in many situations. You could, for example, import your own assemblies with scriptcs #r and use them to transfer data to and from Redis.

Additionally, the script pack imports the following namespaces into your context:

  • – ServiceStack.Redis
  • – ServiceStack.Redis.Generic
  • – ServiceStack.Redis.Messaging
  • – ServiceStack.Text
  • – ServiceStack.Text.Json

So yo ucan use any types defined there straight away.

Hope you will find this useful too!

Be Sociable, Share!