Skip to content

Instantly share code, notes, and snippets.

@brianrusso
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianrusso/9525907 to your computer and use it in GitHub Desktop.
Save brianrusso/9525907 to your computer and use it in GitHub Desktop.
from dogpile.cache import make_region
# key dictionary
keys = dict()
def my_key_generator(namespace, fn, **kwargs):
fname = fn.__name__
def generate_key(*arg):
# generate a key template,
# basically just concatenate all the args with the functionname
if namespace:
prefix = namespace + "__" + fname
else:
prefix = fname
out = str()
for a in arg[1:]:
out += "_" + str(a)
key_template = prefix + "|" + out[1:]
fruit_type = arg[1] # fruit_type must be first parameter in all cached functions
# store the key in the appropriate set for the fruit type
try:
keys[fruit_type].add(key_template)
except KeyError: # if it's the first one..
keys[fruit_type] = set()
keys[fruit_type].add(key_template)
# return cache key
return key_template
return generate_key
def invalidate_fruit_cache(region, fruit_type):
# Delete all cached data for the specified fruit type
try:
for key in keys[fruit_type]:
region.delete(key)
except KeyError:
pass # Probably wasn't instantiated yet.
finally:
keys[fruit_type] = set()
region = make_region(
function_key_generator=my_key_generator
).configure(
"dogpile.cache.memory"
)
# I couldn't be bothered to shop for a real gift..
# or
# much friendlier than a basket with lotion.
class FruitBasket(object):
fruit = dict()
def __init__(self):
apples = self.fruit['apples'] = list()
apples.append('Braeburn')
apples.append('Granny Smith')
apples.append('Delicious')
apples.append('Fuji')
citrus = self.fruit['citrus'] = list()
citrus.append('Grapefruit')
citrus.append('Limes')
citrus.append('Oranges')
citrus.append('Mandarins')
@region.cache_on_arguments(expiration_time=1000)
def get_fruit(self, fruit_type, json):
if json:
import json
s = self.fruit[fruit_type]
return "json: %s" % str(json.dumps(s))
else:
return "text: %s" % str(self.fruit[fruit_type])
def fuji_to_honeycrisp(self):
apples = self.fruit['apples']
apples.remove('Fuji')
apples.append('Honeycrisp')
basket = FruitBasket()
# Load up the cache
print basket.get_fruit('citrus', False)
print basket.get_fruit('apples', False)
print basket.get_fruit('apples', True)
print "Should have honeycrisp now: "
# But we won't because we've cached everything..
basket.fuji_to_honeycrisp()
print basket.get_fruit('apples', True)
# Check out the cache, remove apples. Citrus stay intact.
print region.backend._cache
print "keys: %s: " % str(keys)
print "Removing any bad apples"
invalidate_fruit_cache(region, "apples")
print region.backend._cache
print "keys: %s: " % str(keys)
# Honeycrisp Heaven
print basket.get_fruit('apples', False)
print basket.get_fruit('apples', True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment