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.
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.
So yes, SCAN is blocking, but it's usually not a big deal, and is only blocking when Redis is actually processing the command.
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.
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.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With