Skip to content

Commit

Permalink
Compress before caching, store more in each memcache key (#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Jun 14, 2016
1 parent 347c39b commit 77d8ccb
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions caravel/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
import hashlib
import logging
import uuid
import zlib

from collections import OrderedDict, defaultdict
from datetime import datetime, timedelta

import pandas as pd
import numpy as np
from flask import request
Expand Down Expand Up @@ -240,12 +243,20 @@ def get_json(self):
"""Handles caching around the json payload retrieval"""
cache_key = self.cache_key
payload = None

if self.form_data.get('force') != 'true':
payload = cache.get(cache_key)

if payload:
is_cached = True
try:
payload = json.loads(zlib.decompress(payload))
except Exception as e:
logging.error("Error reading cache")
payload = None
logging.info("Serving from cache")
else:

if not payload:
is_cached = False
cache_timeout = self.cache_timeout
payload = {
Expand All @@ -262,7 +273,10 @@ def get_json(self):
logging.info("Caching for the next {} seconds".format(
cache_timeout))
try:
cache.set(cache_key, payload, timeout=cache_timeout)
cache.set(
cache_key,
zlib.compress(self.json_dumps(payload)),
timeout=cache_timeout)
except Exception as e:
# cache.set call can fail if the backend is down or if
# the key is too large or whatever other reasons
Expand Down

0 comments on commit 77d8ccb

Please sign in to comment.