Last active
August 29, 2015 14:01
-
-
Save jonaslindmark/9b21782976ba944177fb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
cache = {} | |
def cache_result(result, key): | |
cache[key] = result | |
return result | |
def cached(f): | |
def wrapped(*args): | |
if key(args) in cache: | |
return defer.succeed(cache[key(args)]) | |
d = f(*args) | |
d.addCallback(cache_result, key(args)) | |
return d | |
return wrapped | |
@cached | |
def some_awesome_function(hehe): | |
return 123 | |
class MyCachedClass(object): | |
self __init__(self): | |
self._cache = {} | |
def _cache_it(result, key): | |
self._cache[key] = result | |
return result | |
def cached_deferred_function(hej, ho): | |
key = "%s-%s" % (hej, ho) | |
if key in self._cache: | |
return defer.succeed(self._cached[key]) | |
return some_awesome_deferred_function(hej,ho)\ | |
.addCallback(self._cache_it, key) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment