Originally I was trying to add the documents returned from User.find({}) into the redis cache as a sorted set so that I can do pagination. like so: client.zadd("directory", documents_id, docsString, (err, reply) => {
I'm starting to wonder if I need to use hmset
since I'm dealing with multiple items in JSON object, but I'm not sure how to query if that's the case. I'm looking for the best solution that will later allow me to paginate the data and also have a keyword that returns matching values in fields (if possible to any extent with redis)
app.get("/api/users", (req, res, next) => {
var searchKey = new RegExp(req.query.usernameSearch, "i");
console.log("CURRENT PAGE IS " + req.query.currentPage);
client.zrange("directory", 0, -1, (err, reply) => {
console.log("reply");
console.log(reply);
console.log(err);
if (err) {
res.status(401).json({
message: "Directory Cache Error!"
});
return;
} else if (reply) {
console.log("GRABBING FROM CACHE");
let packageReply = JSON.parse(reply);
console.log("packageReply");
console.log(packageReply);
let totatArtists = packageReply.length;
res.status(200).json({
message: "Grabbed from cache",
posts: packageReply,
maxPosts: totatArtists
});
} else {
console.log("GRABBING FROM DB");
User.countDocuments({
$and: [{ username: searchKey }, { role: "Seller" }]
}).then(docs => {
let totalPosts = docs;
let postPerPage = 20;
User.find({
$and: [{ username: searchKey }, { role: "Seller" }]
})
.select("_id username")
.skip(postPerPage * (currentPage - 1))
.limit(postPerPage)
.then(documents => {
let docsString = JSON.stringify(documents);
client.zadd(
"directory",
documents_id,
docsString,
(err, reply) => {
client.expire("directory", 3600);
res.status(200).json({
message: "Users retrieved successfully!",
posts: documents
});
}
);
});
});
}
});
sample output
[{ _id: 5e34482ce04d7c0ca4725f92, username: 'bob' },
{ _id: 5e344842e04d7c0ca4725f93, username: 'joe' },
{ _id: 5e383e5dace65e4774e646e1, username: 'bill' },
{ _id: 5e383e63ace65e4774e646e2, username: 'sue' }]