Get all keys from Redis Cache in C#.NET

Get all keys from Redis Cache in CNET

Today in this article, we shall see a simple approach of how to get all keys from Redis cache in the C#.NET application. In our last article, we learned an approach by using StackExchange.Redis provides a good interface to read or write from the Redis server instance.

However, it does not provide full access to the database.

Today we shall see the simple approach of using ConnectionMultiplexer to read the full database and perform read or delete of keys as needed.

Let’s push a few keys into the Redis cache,

>set 101 thecodebuzz101
>set 102 thecodebuzz102

Get all keys from Redis

I have written below a simple method that reads the cache and returns all available keys.

        public List<string> GetAllkeys()
        {
            List<string> listKeys = new List<string>();
            using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost:6379,allowAdmin=true"))
            {
                var keys = redis.GetServer("localhost", 6379).Keys();
                listKeys.AddRange(keys.Select(key => (string)key).ToList());

            }

            return listKeys;
        }

ConnectionMultiplexer Create a new ConnectionMultiplexer instance and obtain an interactive connection to a database inside Redis.

Best practices on using ConnectionMultiplexer

  • ConnectionMultiplexer is thread-safe and support multi threading.
  • Create an instance and try to reuse ConnectionMultiplexer object throughout process.
  • Recommended to use ConnectionMultiplexer using dependency injection

read all keys from Redis

The above example just explains a possible approach of using the ConnectionMultiplexer object for establishing the Redis connection.

However as explained above, please try to use dependency injection to manage the instances.

ConnectionMultiplexer using dependency injection is recommended approach for Redis cache

Please is another example using dependency injection,

public List<string> GetAllKeysinRedisCache()
        {
            List<string> listKeys = new List<string>();
            string host = _configuration.GetValue<string>("RedisCache:Host");
            string port = _configuration.GetValue<string>("RedisCache:Port");
            var iServer = _redisCache.GetServer(host, port);
            var keys = iServer.Keys();
            listKeys.AddRange(keys.Select(key => (string)key).ToList());
            return listKeys;
        }

Please refer to the below article for more details on how to create the above-mentioned _redisCache object using ConnectionMultiplexer

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *