title: Redis won't retrieve data from cache
body: <p>I'm following a tutorial and I created a cache.js file that takes the mongoose query and JSON.stringifies it into the key for the values being returned by that query. The goal is to cache that and then append <code>.cache()</code> inside app.js where the <code>mongoose.find()</code> is</p>
  
  <p>Currenty if the cache is empty, I have it do a GET from the DB and then store it in the cache. I have a </p>
  
  <pre><code>console.log("CACHE VALUE #2");
  console.log(cacheValue1);
  </code></pre>
  
  <p>that ensures that the data is getting stored and outputs the data successfully. This line works. But with this line,</p>
  
  <pre><code>console.log("CACHE VALUE #1");
  console.log(cacheValue);
  </code></pre>
  
  <p>the <code>cacheValue</code> is null.</p>
  
  <p>Why is that?</p>
  
  <p>It stores at the bottom the value and the key never changes so I don't understand why it won't return the data instead of null.</p>
  
  <p>So <code>Cache Value #1</code> is always null and <code>Cache Value #2</code> has the correct data.</p>
  
  <p>CONSOLE OUTPUT:</p>
  
  <pre><code>GRABBING FROM DB
  CLIENT CONNECTION STATUS: true
  Setting CACHE to True
  ABOUT TO RUN A QUERY
  {"$and":[{"auctionType":{"$eq":"publicAuction"}},{"auctionEndDateTime":{"$gte":1582903244869}},{"blacklistGroup":{"$ne":"5e52cca7180a7605ac94648f"}},{"startTime":{"$lte":1582903244869}}],"collection":"listings"}
  CACHE VALUE #1
  null
  CACHE VALUE #2
  (THIS IS WHERE ALL MY DATA SHOWS UP)
  </code></pre>
  
  <pre><code>const mongoose = require('mongoose');
  const redis = require('redis');
  const util = require('util');
  var env = require("dotenv").config({ path: './.env' });
  
  const client = redis.createClient(6380, process.env.REDISCACHEHOSTNAME + '.redis.cache.windows.net', {
    auth_pass: process.env.REDISCACHEKEY,
    tls: { servername: process.env.REDISCACHEHOSTNAME + '.redis.cache.windows.net' }
  });
  
  
  client.get = util.promisify(client.get);
  
  
  const exec = mongoose.Query.prototype.exec;
  
  mongoose.Query.prototype.cache = function () {
    this.useCache = true;
    console.log("Setting CACHE to True")
    return this;
  }
  
  mongoose.Query
    .prototype.exec = async function () {
      if (!this.useCache) {
        console.log("GRABBING FROM DB")
        console.log("CLIENT CONNECTION STATUS: " + client.connected);
  
        return exec.apply(this, arguments);
      }
  
      console.log("ABOUT TO RUN A QUERY")
      const key = JSON.stringify(Object.assign({}, this.getQuery(), {
        collection: this.mongooseCollection.name
      }));
  
  
      //See if we have a value for 'key' in redis
      console.log(key);
      const cacheValue = await client.get(key);
      console.log("CACHE VALUE #1");
      console.log(cacheValue);
      //If we do, return that
      if (cacheValue) {
        console.log("cacheValue IS TRUE");
        const doc = JSON.parse(cacheValue);
        return Array.isArray(doc)
          ? doc.map(d =&gt; new this.model(d))
          : new this.model(doc);
      }
  
      //Otherwise, issue the query and store the result in redis
      const result = await exec.apply(this, arguments);
  
      let redisData = JSON.stringify(result);
      //stores the mongoose query result in redis
  
  
  
      await client.set(key, JSON.stringify(redisData)), function (err) {
        console.error(err);
  
      }
      const cacheValue1 = await client.get(key);
      console.log("CACHE VALUE #2");
      console.log(cacheValue1);
  
  
  
  
      return result;
    }
  
  
  </code></pre>