How to find out record using where condition in Redis List?

Category: azure cache


ashuthinks32 on Fri, 27 May 2016 11:16:42

I'm using cloud structure redis library for redis cache and redis list.

// Settings should holds in static variable
public static class RedisServer
    public static readonly RedisSettings Default = new RedisSettings("");

public class Person
    public string Name { get; set; }
    public int Age { get; set; }

var mylist = new RedisList<Person>(RedisServer.Default, "test-list-key");
await mylist.LeftPush(new[] { new Person { Name = "Tom" , Age = 12 }, new Person { Name = "Mary" , Age = 23} });

I want to find out record from mylist whose Name=Tom and Age=12



HSUlriksen on Sun, 29 May 2016 11:23:00


this is not possible.

A Redis list is basically a linked list. There is no way to lookup entries that way without iterating through the whole list, which is slow and not what you want. On a list you can set or retrieve items by index, trim the list. It also supports the push and pop functions, familiar from other array implementations such as JavaScript.

To further understand this I recommend having a look at the native Redis commands for list and reading the introduction to Redid data types.

If you have a Key for your persons, you can store them in a HASHSET and then use the key to lookup a person in the set. If you need to access the persons by other means than a key you will need a way to build your own indexes in Redis.

Hope this helps,

ashuthinks32 on Sun, 29 May 2016 12:08:44

great stuff! how can u solved my current issue , i guess there is only way to do it using redis dictionary