1

I am using the StackExchange.Redis library to interface with an Azure Redis server to extract some hash values for access via a web api. Here is how I am doing it:

        IDatabase cache = lazyConnection.Value.GetDatabase();
        var server = lazyConnection.Value.GetServer(lazyConnection.Value.GetEndPoints().First());
        var starterKeys = server.Keys(pattern: "Monitor:*");
        var count = 0;
        foreach (var starterKey in starterKeys)
        {
            var hashEntries = cache.HashGetAll(starterKey);
            foreach (var hashEntry in hashEntries)
            {
                //put values into my model here...
            }
            count++;
        }

There are exactly 22 keys that match the pattern "Monitor:*" (found by debugging and watching the count value at the end). However, this snippet of code, retrieving those 22 keys and hashes takes 55 seconds. The hash values aren't very large either. There are about 19 key-value pairs in each hash (things like name, company, email, phone, etc. Nothing too big).

Why would this take so long? I thought one of the reasons to use Redis was to be fast, and reduce load from the database by storing aggregated data. I could set this up in my database and query it (even with joins to get the aggregated data), and my process would by considerably faster than 55 seconds.

Am I doing something wrong here? Or am I not understanding the purpose or correct usage of Redis?

And, most importantly, is there anything I can do to retrieve these keys and hash values faster?

LeoMurillo
  • 6,048
  • 1
  • 19
  • 34
Matt Spinks
  • 6,380
  • 3
  • 28
  • 47

1 Answers1

0

if you need all KEY*, keeping them in a value will increase the speed. You can keep the object under AllKeys to display all Keys.

SetAllObject

 cache.StringSetAsync("AllKeys", JsonConvert.SerializeObject(obj));

GetAllObject

JsonConvert.DeserializeObject<List<TEntity>>(await cache.StringGetAsync("AllKeys")).ToList();
Emrah AKIN
  • 137
  • 1
  • 6