简体   繁体   English

StackExchange.Redis - 如何获取密钥的类型?

[英]StackExchange.Redis - How to get the type of the key?

I am trying to go through the list of dbX in Redis via StackExchange.Redis and look for the particular type of the keys.我正在尝试通过 StackExchange.Redis 通过 Redis 中的 dbX 列表 go 并查找特定type的键。 There are string , list , set , zset , hash types of keys.stringlistsetzsethash类型的键。 Lets say I would like to find the list types of keys in the db1 , I tried the following:假设我想在db1中找到键的list类型,我尝试了以下方法:

RedisCacheClient _client;
...
IRedisDatabase database = _client.GetDb(1);
List<InfoDetail> categorizedInfo = await database.GetInfoCategorizedAsync();
IEnumerable<InfoDetail> infos = categorizedInfo.Where(i => i.Category == "Keyspace");

This helps me with getting the basic info about the keys keys=9,expires=0,avg_ttl=0 but not the types.这有助于我获取有关键keys=9,expires=0,avg_ttl=0但不是类型的基本信息。

I can find all the keys:我可以找到所有的钥匙:

IEnumerable<string> keys = await database.SearchKeysAsync("*");

But that gives me only the names, not the types of the keys.但这只给了我名称,而不是键的类型。 So, how do I find the type of the key?.那么,如何找到密钥的type

Is this possible to do?这可能吗?

Thanks.谢谢。

I think I have found the solution.我想我已经找到了解决方案。 the IRedisDatabase type has the Database propery of type IDatabase , and IDatabase type has KeyType() method that I could use (it should have been GetKeyType() , I think, which would've made it much easier to find). IRedisDatabase类型具有IDatabase类型的Database属性,并且IDatabase类型具有我可以使用的KeyType()方法(我认为它应该是GetKeyType() ,这样会更容易找到)。 With that, I was able to list the keys with types:这样,我就可以列出具有类型的键:

RedisCacheClient _client;
...
IRedisDatabase redisDatabase = _client.GetDb(1);
IEnumerable<string> keys = await redisDatabase.SearchKeysAsync("*");
IDatabase database = redisDatabase.Database;
Dictionary<string, RedisType> keysWithTypes = keys.ToDictionary(k => k, k => database.KeyType(k));

the result looks something like this:结果看起来像这样:

{[myHash, Hash]}
{[myZSet, SortedSet]}
{[myString, String]}
{[mySet, Set]}
{[mylist, List]}

Redis has TYPE command which gives you the type of the key. Redis 有TYPE命令,可以为您提供密钥的类型。 I suppose any client would support this.我想任何客户都会支持这一点。

Returns the string representation of the type of the value stored at key.返回存储在 key 的值的类型的字符串表示形式。 The different types that can be returned are: string, list, set, zset, hash and stream.可以返回的不同类型有:string、list、set、zset、hash 和 stream。

https://redis.io/commands/type https://redis.io/commands/type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM