Search code examples
c#redisservicestacktyped

How to use servicestack typed client?


Basically we have a redis instance and we would like to Save and Get all items from a Redis List.

We can Save it but when we tried to get the list

var redis = redisclient.As<MyModel>();
string key = "myredislistkey";
List<MyModel> mylist = redis.GetAllItemsFromList(key);

I know this is wrong but why? And how to properly use Typed client to Getallitems using a redis key(or so called listid)?

Official usage is

List<T> GetAllItemsFromList(IRedisList<T> fromList);

But if I already have fromList, why would I trying to get it?


Solution

  • You can get a reference to a Typed List (i.e. IRedisList<T>) with:

    var redisModels = redisClient.As<MyModel>().List["myredislistkey"];
    

    IRedisList<T> is just an adapter that implements the .NET IList<T> interface over a remote redis LIST, i.e. it doesn't contain any elements itself, you would use it to interface with this list, e.g:

    Add Items to it with:

    redisModels.Add(new MyModel { ... });
    redisModels.AddRange(new[] { new MyModel { ... }, new MyModel { ... } });
    

    And get all elements with:

    var allModels = redisModels.GetAll();