![]() ![]() String port = _configuration.GetValue("RedisCache:Port") String host = _configuration.GetValue("RedisCache:Host") Please use the same above logic in the loop and delete multiple keys.ĭelete all keys in Redis the database can be achieved using FlushDatabaseAsync or FlushAllDatabasesAsync methods. Redis-Dependency Injection of the ConnectionMultiplexerīelow is a sample snippet from the above article where we do DI for IConnectionMultiplexer,.To get more details about the same, please visit below article of best practices of Redis using IConnectionMultiplexer _redisDBAsync is an instance of IDatabaseAsync which is created using IConnectionMultiplexer related GetDatabase method. Public async Task DeleteID(int employeeID)Īwait _redisDBAsync.KeyDeleteAsync(employeeID) The below example uses the dependency injection approach for IConnectionMultiplexer and uses the object to perform read, write, delete operations on the cache. Its recommended using Use the SCAN Vs KEYS command as we learned in the Best practices of Redis Cache guidelinesĭelete single key in Redis from the current databaseĭelete single key in Redis from the current database can be achieved using supported sync and async methods as shown in the below example, We will see a few approaches which are supported in. Delete all keys in Redis from all the database.In this post we cover two important concepts i.e scan and pipeline. Final Words:įetching data from redis is very fast only if we use it correctly, understand how it works and what will break it. Let me know in the comments if you want it sooner or if you’ve any doubts. Soon I’ll be uploading the code on github which you can use directly in any of your environments. Finally when stream ends, we’ve to execute the remaining del commands if left in pipeline queue. When the count of found keys reaches 100(will differ for other apps), then only we are executing all the delete commands queued earlier. We are storing the found keys in localKeys array and pushing the command to delete those keys in pipeline. The above code is scanning for all keys having “user:” pattern in them and returning the found keys in a single scan which may not be full set. var Redis = require('ioredis') var redis = new Redis(port, host) // Create a readable stream (object mode) var stream = redis.scanStream() Note: throughout this post, I am using ioredis module in nodejs environment, but the same concepts can be used with any other library. It's different from KEYS because SCAN only returns a small number of elements in each call, so it can be used in production without the downside of blocking the server for a long time. Redis 2.8 added the SCAN command to incrementally iterate through the keys in the database. Note: It is recommended to use timeouts at client level which essentially means that if data from cache is taking too much time, then data needs to be fetched from actual source. When the number of keys in redis (use DBSIZE to know the count) is less, this method can be used but when redis size grows, using keys cmd is extremely dangerous for performance and highly discouraged in production environment since redis is single threaded, keys cmd will block all other operations on redis and all the clients will have to wait for its completion. Keys cmd has a time complexity of O(n) which means it iterates over all the keys one by one and returns those with the matching criteria. In the above code, keys cmd is searching for all the keys which have “key” string in name.
0 Comments
Leave a Reply. |