0

I'm trying to figure out how to clear a bunch of keys in redis from the cache in the most efficient manner possible.

In this example I'm getting a string array of userIds. Each user has a watchlist of auctionIds in their user document inside mongodb

"watchlist": [
    "5eb81dbaecb87a4060c3d5a7",
    "5eb81e24ecb87a4060c3d5a8",
    "5eb8302ebae779466c97c1d9"
  ],

It's an auction site so if a user places a bid, then I need the watchlists that have the listing inside that user's [string] watchlist to clear the cache for that key const watchlistKey = `watchlist-${req.query.userIdBidder}`;

but in production that could be 100s of ids. Who knows. I've been using this library for other pattern delete examples: https://www.npmjs.com/package/redis-delete-wildcard?activeTab=readme but I don't think I can apply it to this use case. What will be the most cost effective way to clear the cache using string array as input data?

  User.find({
        watchlist: { $in: [req.query.listingId] },
      }).select("_id").then((res) => {

        console.log(res);

        const watchlistKey = `watchlist-${req.query.userIdBidder}`;


        client.delwild('main-listings-no-filter-page=*', function (error, numberDeletedKeys) {
          console.log("Page Didn't Match During Bid. All Listings Pages have been removed from cache.");
          console.log(numberDeletedKeys);
        });
      })

Console Output

 [ { _id: 5eb8196eecb87a4060c3d5a5 },
  { _id: 5eb82d0c71b0a230b853bd6f } ]
user6680
  • 79
  • 6
  • 34
  • 78
  • Did you check RediSearch? – Guy Korland May 10 '20 at 19:59
  • I haven't. I just looked at, but I'm finding the documentation a little confusing. Can you provide an example of RedisSearch and my use case? – user6680 May 10 '20 at 21:05
  • Take a look at https://stackoverflow.com/questions/61366419/how-to-atomically-delete-millions-of-keys-matching-a-pattern-using-pure-redis/61366420#61366420 – LeoMurillo May 11 '20 at 04:59

0 Answers0