I need to use NodeJS to delete all records in Redis that match a prefix / wildcard. I'm using this package: https://github.com/NodeRedis/node_redis
I have seen several solutions available to delete Redis keys that feature a prefix and a wildcard eg
KEYS "web.*"
However, all of these solutions rely on using the KEYS
command which is not suitable for production due to it locking up the server. I believe the correct solution involves the SCAN
command (and perhaps others), but I don't fully understand how it works.
I'm looking for a simple JS function that allows me to pass in a string containing the prefix and wildcard eg "web.*" and all "keys" matching keys will be deleted.
Many thanks!
Redis is single threaded, so it's going to block regardless. Scan returns the result in an iterative fashion, so while it still blocks, it wont block for as long given it's returning a subset of the overall result.
https://redis.io/commands/scan
There's a scan solution here that may or may not work for you depending on whether you need it to be atomic or not. How to atomically delete keys matching a pattern using Redis
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.