Created
September 14, 2012 21:21
-
-
Save pamelafox-coursera/3724931 to your computer and use it in GitHub Desktop.
Memcache utility functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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