user6680
user6680

Reputation: 139

Clear keys with array - REDIS

I'm trying to figure out the most efficient way to clear multiple keys using array of userIds for my auction site.

When a user places a bid on a specific auction then their userId gets added to array of bidders on that specific auction. The key is used for the active bids page. If a user places a bid, then the other bidders in the biddingGroup need to have their keys cache cleared. The only way I can think of doing it is to

  for (let i = 0; i < biddingGroup.length; i++) {
            redisConnection.client.del("active-bid-" + biddingGroup[i], function (err, response) {
              if (response == 1) {
                console.log("Deleted active bid pages from cache")
              } else {
                console.log("Cannot delete")
              }
            })
          }

but I feel this is inefficient. Is there a better way? I appreciate any help!

Upvotes: 0

Views: 47

Answers (1)

Itamar Haber
Itamar Haber

Reputation: 49942

This loopless approach should be more efficient if we're talking about 10s or 100s of keys:

redisConnection.client.del(...biddingGroup.map(e => `active-bid-${e}`), function (err, response) {
// code
}

For 1000s or more, using wrapping it in a loop that handles about 100..1000 keys at a time makes sense imo.

Upvotes: 1

Related Questions