2
\$\begingroup\$

I built an auction site with a dynamic caching layer after I seeing how expensive a dbaas is. The site is now done, but like any site that grows, my caching solution grew too. The cache currently caches every page on the site so I made sure everything gets invalidated appropriately, but the amount of stuff I'm invalidating has grown. More specifically when I'm creating a new auction. I know I'm using Azure so I can scale things from the portal to handle load, but I wanted to make sure my design will work. When someone creates an auction listing, I have to invalidate a bunch of stuff like in code below. Does anyone see a problem with what I'm doing or should I be good?

I'm using redis-delete-wildcard for the delwild keyword

var Post = require('../models/post');
let emails;
var startTime;
const redis = require('redis');
var redisConnection = require("../connections/server");
require('redis-delete-wildcard')(redis); //pass in redis so prototype can be extended
const checkAuth = require('../middleware/check-auth');

module.exports = function (context, req) {

  checkAuth(context, req);


  const tempAuctionKey = `temp-auction-data-${req.body.creator}`;

  redisConnection.client.get(tempAuctionKey, function (err, result) {
    let blacklistGroup;
    let email;
    if (err) {
      console.log(err);
    }



    let output = JSON.parse(result);
    email = output.documents.email;
    emails = output.emails;

    if (err) {

      context.res = {

        status: 500,

        body: {
          message: "FAILED TO GET TEMP AUCTION DATA FROM CACHE!",
          error: err
        },

        headers: {
          "Access-Control-Allow-Credentials": "true",
          "Access-Control-Allow-Origin": "*",
          "Access-Control-Allow-Methods": "POST, OPTIONS",
          "Access-Control-Allow-Headers": "Content-Type, Set-Cookie",
          "Access-Control-Max-Age": "86400",
          "Vary": "Accept-Encoding, Origin",
          "Content-Type": "application/json"
        }

      };

      context.done();


    }

    var fiveDaysAfterAuction = new Date(req.body.auctionEndDateTime * 1000);
    startTime = new Date(req.body.startTime);

    startTime.setSeconds(0);

    fiveDaysAfterAuction.setHours(120);

    console.log("req.body.auctionImages");
    console.log(req.body.images);
    let images = req.body.images;

    console.log("req.body.auctionImagesTimeStamp");
    console.log(req.body.auctionImagesTimeStamp);


    const post = new Post({
      title: req.body.title,
      creator: req.body.creator
      //and more...
    });


    post.save().then(result => {

      const totalListingsKey = `dashboardTotal-${req.body.creator}`;
      const sellerDashboardKey = `dashboard-${req.body.creator}-*`;
      const bioKey = `my-bio-${req.body.sellerName}-*`;
      const mainListingsCountKey = `main-listing-count`;
      const buyNowTotalKey = `buy-total`;


      //clears seller bio page which has active auctions listed
      redisConnection.client.delwild(bioKey, function (error, numberDeletedKeys) {
        console.log("Deleted Seller Bio Page for Seller");
        console.log(numberDeletedKeys);
      });


      if (req.body.auctionType === "publicAuction") {
        //clears all the auction pages
        redisConnection.client.delwild('main-listings-no-filter-page=*', function (error, numberDeletedKeys) {
          console.log("Deletes all auction pages");
          console.log(numberDeletedKeys);
        });
        //clears the buy it now page if new auction is a buy it now listing
        if (req.body.buyItNow) {
          const buyNowKey = `buy-now-page-*`;
          redisConnection.client.delwild(buyNowKey, function (error, numberDeletedKeys) {
            console.log("deleted buynowkeys");
            console.log(numberDeletedKeys);
          });

          //clears buy it now page total count for pagination
          redisConnection.client.del(buyNowTotalKey, function (err, response) {
            if (response == 1) {
              console.log("Deleted Buy Now total count")
            } else {
              console.log("Cannot delete buy now total")
            }
          })

        }

        //clears main page total count for pagination
        redisConnection.client.del(mainListingsCountKey, function (err, response) {
          if (response == 1) {
            console.log("Deleted Listings Count Successfully!")
          } else {
            console.log("Cannot delete")
          }
        })

      }
      //clears seller dashboard listings
      redisConnection.client.delwild(sellerDashboardKey, function (error, numberDeletedKeys) {
        console.log("numberDeletedKeys");
        console.log(numberDeletedKeys);
      });


      //clears seller dashboard page count for pagination
      redisConnection.client.del(totalListingsKey, function (err, response) {
        if (response == 1) {
          console.log("Deleted SellerDashboard Listings Count Successfully!")
        } else {
          console.log("Cannot delete")
        }
      })


      redisConnection.client.expire(tempAuctionKey, 5);


      context.res = {


        status: 201,

        body: {
          message: "Auction listing created successfully!",
          postId: result._id
        },

        headers: {
          "Access-Control-Allow-Credentials": "true",
          "Access-Control-Allow-Origin": "*",
          "Access-Control-Allow-Methods": "POST, OPTIONS",
          "Access-Control-Allow-Headers": "Content-Type, Set-Cookie",
          "Access-Control-Max-Age": "86400",
          "Vary": "Accept-Encoding, Origin",
          "Content-Type": "application/json"
        }

      };

      context.done();


    }).catch((err) => {


      context.res = {

        status: 500,

        body: {
          message: "Auction Creation Failed",
          error: err
        },

        headers: {
          "Access-Control-Allow-Credentials": "true",
          "Access-Control-Allow-Origin": "*",
          "Access-Control-Allow-Methods": "POST, OPTIONS",
          "Access-Control-Allow-Headers": "Content-Type, Set-Cookie",
          "Access-Control-Max-Age": "86400",
          "Vary": "Accept-Encoding, Origin",
          "Content-Type": "application/json"
        }

      };

      context.done();


    });
  });
};
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.