In overview function we called await Tour.find().cache() so find should be called first then cache but the first acutally cache is executed first then find method ? Is this because await is executing sync and async function it first exeution sync then async (yes without escaping to next line of code ) or it is something else
Here is handler function (Overview.js) exports.getOverview = catchAsync(async (req, res, next) => {
Can you explain why this is not executing the way I think? const tours = await Tour.find().cache(); res.status(200).render("overview", { title: "All Tours", tours, }); });
Here is function defined in exec.js const { exec } = mongoose.Query.prototype;
mongoose.Query.prototype.cache = function () {
this.useCache = true;
return this;
};
mongoose.Query.prototype.exec = async function () {
if (!this.useCache) return exec.apply(this, arguments);
const key = JSON.stringify(
Object.assign({}, this.getQuery(), {
collection: this.mongooseCollection.name,
})
);
const cacheValue = await client.get(key);
if (cacheValue) {
const doc = JSON.parse(cacheValue);
return Array.isArray(doc) ? doc.map((d) => this.model(d)) : this.model(doc);
}
// if we do, return that
// Otherwise, issue the query and store the result in redis
const result = await exec.apply(this, arguments);
client.set(key, JSON.stringify(result), "Ex", 10);
return result;
};