How to use ServiceStack Redis API?

1.9k views Asked by At

I am new to service stack redis api. So i am getting little confused while using the service stack redis api. I want to know IRedisTypedClient"<"T">"?
1) What stands for "<"T">"?
2) What are the parameters we can pass in the "<"T">"?

1

There are 1 answers

4
mythz On BEST ANSWER

The IRedisTypeClient interface provides a typed version of the Redis Client API where all its API's accept a typed POCOs (i.e. Plain Old CSharp Object) for its value body which is in contrast to IRedisClient which just accepts raw strings. Behind the scenes the Typed API's just serialize the POCO's to a JSON string but it's typed API provides a nicer API to work with when dealing with rich complex types.

The API to create a IRedisTypeClient<T> is to use the IRedisClient.As<T> API, e.g:

public class Todo
{
    public long Id { get; set; }
    public string Content { get; set; }
    public int Order { get; set; }
    public bool Done { get; set; }
}

IRedisClient redis = redisManager.GetClient();
var redisTodos = redis.As<Todo>(); 

As seen above you can create a typed API from any user-defined POCO, which now provides API's that lets you work directly native Todo types, e.g:

var todo = new Todo
{
    Id = redisTodos.GetNextSequence(),
    Content = "Learn Redis",
    Order = 1,
};

redisTodos.Store(todo);

Todo savedTodo = redisTodos.GetById(todo.Id);
savedTodo.Done = true;
redisTodos.Store(savedTodo);

"Updated Todo:".Print();
redisTodos.GetAll().ToList().PrintDump();

There's a stand-alone version of this example as well as a Live Demo of Backbones TODO app with a Redis backend which makes use of the RedisClient Typed API.