如何在Redis List中使用where条件查找记录?
How to find out record using where condition in Redis List?
我正在为 Redis 缓存和 Redis 列表使用 cloud structure redis 库。
// Settings should holds in static variable
public static class RedisServer
{
public static readonly RedisSettings Default = new RedisSettings("127.0.0.1");
}
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
Redis 根本不是那样工作的。查询列表时,您不能执行 where ...
的等效操作。您可以推送、弹出和查询范围等 (here's the full list of "list" commands)。 是一个"what is the index of [content] in [list]",但为此你需要知道对象的整个有效载荷(以完全相同的形式),不只是个别作品。您可以在redis中手动建立索引,但是:这是一个相对高级的话题。
就像 Marc Gravell 在他自己的回答中所说的那样,Redis 不支持 查询 但它可以作为原始数据索引进行定制。这就是提供列表、集合、排序集合、哈希等数据结构的原因...
例如,如果您想要检索具有特定姓名和年龄的人,您需要以某种方式对数据建立索引,以便您以后可以获取与整个条件匹配的整个实体。
将您的 JSON 字符串存储在散列中,其中键是唯一标识符,值是 JSON 文本。或者,您可以使用简单的 set
命令将键值对存储在全局 Redis 键空间中。
如果你想获得具有 Name
和 Age
的人,你可以将人存储在另一个散列中,其中键的格式为 [=15] =] 并且该值将是此人的唯一标识符:Tom:12
=> 1
(其中 1
是唯一标识符`.
一旦你已经创建了第二个散列来支持 name 和 age 标准,它只是执行 hget persons:name-and-age Tom:12
来获取 Tom's唯一标识符,稍后执行另一个 hget
以获取 JSON:hget persons 1
(其中 1
是从 name 和 age 的哈希值中获得的唯一标识符 搜索条件。
总之
hset persons 1 "JSON here"
hset persons:bycriteria:name-and-age Tom:12 1
hset persons 2 "JSON here"
hset persons:bycriteria:name-and-age Matias:31 1
现在我想得到 12 岁的汤姆:
// pseudo-code
// "Tom" and "12" may come from C# variables that were set by who knows
// what... For example, a WebAPI request that got these search criteria
// arguments...
tomId = hget persons:bycriteria:name-and-age Tom:12
tom = hget persons tomId
这只是一种可能的方法,可以在 Redis 上索引数据,然后可以通过 Redis 中的一些预定义条件获取数据。
我建议你在 Redis 官方网站上阅读这篇文章:http://redis.io/topics/indexes
我正在为 Redis 缓存和 Redis 列表使用 cloud structure redis 库。
// Settings should holds in static variable
public static class RedisServer
{
public static readonly RedisSettings Default = new RedisSettings("127.0.0.1");
}
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
whoseName=Tom and Age=12
Redis 根本不是那样工作的。查询列表时,您不能执行 where ...
的等效操作。您可以推送、弹出和查询范围等 (here's the full list of "list" commands)。 是一个"what is the index of [content] in [list]",但为此你需要知道对象的整个有效载荷(以完全相同的形式),不只是个别作品。您可以在redis中手动建立索引,但是:这是一个相对高级的话题。
就像 Marc Gravell 在他自己的回答中所说的那样,Redis 不支持 查询 但它可以作为原始数据索引进行定制。这就是提供列表、集合、排序集合、哈希等数据结构的原因...
例如,如果您想要检索具有特定姓名和年龄的人,您需要以某种方式对数据建立索引,以便您以后可以获取与整个条件匹配的整个实体。
将您的 JSON 字符串存储在散列中,其中键是唯一标识符,值是 JSON 文本。或者,您可以使用简单的
set
命令将键值对存储在全局 Redis 键空间中。如果你想获得具有
Name
和Age
的人,你可以将人存储在另一个散列中,其中键的格式为 [=15] =] 并且该值将是此人的唯一标识符:Tom:12
=>1
(其中1
是唯一标识符`.一旦你已经创建了第二个散列来支持 name 和 age 标准,它只是执行
hget persons:name-and-age Tom:12
来获取 Tom's唯一标识符,稍后执行另一个hget
以获取 JSON:hget persons 1
(其中1
是从 name 和 age 的哈希值中获得的唯一标识符 搜索条件。
hset persons 1 "JSON here"
hset persons:bycriteria:name-and-age Tom:12 1
hset persons 2 "JSON here"
hset persons:bycriteria:name-and-age Matias:31 1
现在我想得到 12 岁的汤姆:
// pseudo-code
// "Tom" and "12" may come from C# variables that were set by who knows
// what... For example, a WebAPI request that got these search criteria
// arguments...
tomId = hget persons:bycriteria:name-and-age Tom:12
tom = hget persons tomId
这只是一种可能的方法,可以在 Redis 上索引数据,然后可以通过 Redis 中的一些预定义条件获取数据。
我建议你在 Redis 官方网站上阅读这篇文章:http://redis.io/topics/indexes