Fehmy
Fehmy

Reputation: 65

Data coming from Redis cache is not paginated

Before I implemented Redis cache in my backend api, pagination was working fine on the backend and on the frontend, after caching the data, it does not work any longer. Here is my middleware that runs before I get all data

const checkCache = (req, res, next) => {
  client.get("posts", (err, data) => {
    if (err) {
      console.log(err);
      sendFailureResponse(res, 500, err);
    }
    if (data != null) {
      sendSuccessResponse(res, 200, JSON.parse(data));
    } else {
      //proceed to next middleware function
      next();
    }
  });
};

The controller

const page = parseInt(req.query.page);
    const size = parseInt(req.query.size);
    const query = {};
    if (page < 0 || page === 0) {
      sendFailureResponse(res, 400, "invalid page number")
    }
    query.skip = size * (page - 1);
    query.limit = size;

    const posts = await Post.find({}, {}, query).populate("user", ["name"]);
    client.setex("posts", 3600, JSON.stringify(posts))
    sendSuccessResponse(res, 200, posts);
  } catch (err) {
    sendFailureResponse(res, 500, err.message);
  }

How can I return the paginated result from the cache?

Upvotes: 1

Views: 758

Answers (1)

Leibale Eidelman
Leibale Eidelman

Reputation: 3184

You can simply add the page number to the key ('page' + req.query.page), and then you'll have cache per page

BTW, you have a bug in the checkCache middleware: if there is an error, you call both sendFailureResponse and next functions, and you'll get "ERR_STREAM_WRITE_AFTER_END" error

Upvotes: 2

Related Questions