Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis client GetAll keys from a certain class

I have a redis with many keys (around 100), I want to select only Keys from type of ClassA (just an example).

Right now I am doing GetAllKeys, and then going in a foreach loop on all items and selecting just the relevant keys.

Is it possible to select just the relevant keys and get them all with one function?

When I am doing GetAll I get only 2 items while I should get around 45.

like image 969
m0fo Avatar asked Nov 18 '12 09:11

m0fo


People also ask

How do I get Redis all keys?

To list the keys in the Redis data store, use the KEYS command followed by a specific pattern. Redis will search the keys for all the keys matching the specified pattern. In our example, we can use an asterisk (*) to match all the keys in the data store to get all the keys.

Is Redis scan blocked?

So yes, SCAN is blocking, but it's usually not a big deal, and is only blocking when Redis is actually processing the command.

How can I check Redis data?

A Redis server has 16 databases by default. You can check the actual number by running redis-cli config get databases. In interactive mode, the database number is displayed in the prompt within square braces. For example, 127.0. 0.1:6379[13] shows that the 13th database is in use.

How do I get the oldest key in Redis?

Redis doesn't save the age of the keys. If you set a fixed TTL on all keys, you can sample a few keys and by viewing their TTL you can know their age (since all objects have the same TTL). This way you can statistically estimate the oldest key.


1 Answers

Following code will help you to find specific key,you have to specify * wildcard character after string, will only fetch specific keys from Redis Server.

 using (RedisClient redisClient = new RedisClient("localhost"))
 {
      string searchString = "ClassA*";

      var getSpecificKeys = redisClient.SearchKeys(searchString);

      foreach (var getKey in getSpecificKeys)
      {
           // operation
      }
  }
like image 143
Urja Avatar answered Sep 24 '22 05:09

Urja