Skip to content

Instantly share code, notes, and snippets.

@pamelafox-coursera
Created September 14, 2012 21:21
Show Gist options
  • Save pamelafox-coursera/3724931 to your computer and use it in GitHub Desktop.
Save pamelafox-coursera/3724931 to your computer and use it in GitHub Desktop.
Memcache utility functions
# So we can easily bust cache between huge versions
def get_cache_key(key):
return 'v01022012' + key
def put_in_cache(key, val, time=None):
from google.appengine.api import memcache
try:
key = get_cache_key(key)
if time:
memcache.set(key, val, time)
else:
memcache.set(key, val)
except ValueError:
logging.error('Memcache %s value too large for cache!' % key)
def get_from_cache(key_or_keys):
from google.appengine.api import memcache
if isinstance(key_or_keys, list):
keys = []
for key in key_or_keys:
keys.append(get_cache_key(key))
return memcache.get_multi(keys)
if isinstance(key_or_keys, basestring):
key = get_cache_key(key_or_keys)
return memcache.get(key)
def delete_from_cache(key_or_keys):
from google.appengine.api import memcache
if isinstance(key_or_keys, list):
keys = []
for key in key_or_keys:
keys.append(get_cache_key(key))
memcache.delete_multi(keys)
if isinstance(key_or_keys, basestring):
key = get_cache_key(key_or_keys)
memcache.delete(key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment