Skip to content

Instantly share code, notes, and snippets.

@jvanasco
Created June 18, 2016 01:29
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 jvanasco/0e420571ac66a0e80de988ed23c9ae40 to your computer and use it in GitHub Desktop.
Save jvanasco/0e420571ac66a0e80de988ed23c9ae40 to your computer and use it in GitHub Desktop.
cache with temporary values.
@task
def get_ExpensiveCalculator_foo():
expensive_foo = app_shared.ExpensiveCalculator().expensive_foo()
app_pyramid.CheapCalculator().set_foo(expensive_foo)
usable_value = app_shared.CheapCalculator().get_foo()
CACHE_REGIONS = {'cache1': dogpile.cache.make_region(),
'expensive1:' dogpile.cache.make_region(),
}
class CheapCalculator():
keys = {'get_foo': 'foo',
}
def _get_foo(self):
# message celery to compute a new one, then return our default value
delayed_action = get_ExpensiveCalculator_foo.apply_async()
return DEFAULT_VALUE
def get_foo(self):
key = self.keys['get_foo']
return CACHE_REGIONS['cache1'].get_or_create(key, self._get_foo)
def set_foo(self, foo):
key = self.keys['get_foo']
return CACHE_REGIONS['cache1'].set(key, foo)
class ExpensiveCalculator(object):
def _get_foo(self):
foo = really_expensive()
return foo
def get_foo(self):
key = "get_foo"
return CACHE_REGIONS['expensive1'].get_or_create(key, self._get_foo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment