Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all key and values from all redis databases?

I'm using StackExchange.Redis to store and retrieve items in redis cache using multiple databases.

However, I cannot figure out how to retrieve key/values across ALL databases. The following code retrieves keys from the default database 0, and I cannot find how to change it to retrieve keys from each database

    public IEnumerable<KeyValuePair<string, object>> GetAll()
    {
        var result = new List<KeyValuePair<string, object>>();
        var endpoints = _dataCache.Multiplexer.GetEndPoints();
        var server = _dataCache.Multiplexer.GetServer(endpoints.First());

        var keys = server.Keys();
        foreach (var key in keys)
        {
            Console.WriteLine(key.ToString());
        }
        return result;
    }

Any suggestions?

like image 974
Michael Avatar asked Nov 20 '16 14:11

Michael


People also ask

Which command is used to obtain all the keys in Redis database?

The Redis KEYS command returns all the keys in the database that match a pattern (or all the keys in the key space). Similar commands for fetching all the fields stored in a hash is HGETALL and for all fetching the members of a SMEMBERS.

Can Redis have multiple databases?

Redis comes with support for multiple databases, which is very similar to the concept in SQL databases. In SQL databases, such as MySQL, PostgreSQL, and Oracle, you can define a name for your databases. However, Redis databases are represented by numbers.

What is Keyspace in Redis?

Redis keyspace notifications allow you to subscribe to PubSub channels. Through the channel, clients receive published events when a Redis command or data alteration occurs. These notifications are useful when an application must respond to changes that occur to the value stored in a particular key or keys.


2 Answers

it seems I found the answers - need to pass the database name to the actual .Keys, as below

var keys = server.Keys(_dataCache.Database);
like image 52
Michael Avatar answered Oct 17 '22 04:10

Michael


You need to write: var keys = server.Keys(int dbName); I added in the example how to get the values by key

public static ConnectionMultiplexer Connection
{
   get
   {
      return ConnectionMultiplexer.Connect("serverName: port");
   }
}

        public Dictionary<string, string> GetDataFromDataBase(srting sPattern = "*")
        {
         int dbName = 2; //or 0, 1, 2
         Dictionary<string, string> dicKeyValue = new Dictionary<string, string>();
         var keys = Connection.GetServer("serverName: port").Keys(dbName, pattern: sPattern);
         string[] keysArr = keys.Select(key =>(string)key).ToArray();

         foreach(var key in keysArr)
         {
           dicKeyValue.Add(key, GetFromDB(dbName , key));
         }

         return dicKeyValue;
        }

    public string GetFromDB(int dbName, string key)
    {
      var db = Connection.GetDatabase(dbName);
      return db.StringGet(key);
    }
like image 24
DLab Avatar answered Oct 17 '22 03:10

DLab