'Handles caching around the json payload retrieval'

def get_payload(self, force=False):

    cache_key = self.cache_key
    payload = None
    force = (force if force else (self.form_data.get(u'force') == u'true'))
    if ((not force) and cache):
        payload = cache.get(cache_key)
    if payload:
        stats_logger.incr(u'loaded_from_source')
        is_cached = True
        try:
            cached_data = zlib.decompress(payload)
            if PY3:
                cached_data = cached_data.decode(u'utf-8')
            payload = json.loads(cached_data)
        except Exception as e:
            logging.error((u'Error    reading    cache:    ' + utils.error_msg_from_exception(e)))
            payload = None
        logging.info(u'Serving    from    cache')
    if (not payload):
        stats_logger.incr(u'loaded_from_cache')
        data = None
        is_cached = False
        cache_timeout = self.cache_timeout
        stacktrace = None
        try:
            df = self.get_df()
            if (not self.error_message):
                data = self.get_data(df)
        except Exception as e:
            logging.exception(e)
            if (not self.error_message):
                self.error_message = str(e)
            self.status = utils.QueryStatus.FAILED
            data = None
            stacktrace = traceback.format_exc()
        payload = {u'cache_key': cache_key, u'cache_timeout': cache_timeout, u'data': data, u'error': self.error_message, u'form_data': self.form_data, u'query': self.query, u'status': self.status, u'stacktrace': stacktrace}
        payload[u'cached_dttm'] = datetime.utcnow().isoformat().split(u'.')[0]
        logging.info(u'Caching    for    the    next    {}    seconds'.format(cache_timeout))
        data = self.json_dumps(payload)
        if PY3:
            data = bytes(data, u'utf-8')
        if (cache and (self.status != utils.QueryStatus.FAILED)):
            try:
                cache.set(cache_key, zlib.compress(data), timeout=cache_timeout)
            except Exception as e:
                logging.warning(u'Could    not    cache    key    {}'.format(cache_key))
                logging.exception(e)
                cache.delete(cache_key)
    payload[u'is_cached'] = is_cached
    return payload
