diff options
author | Thomas Kemmer <tkemmer@computer.org> | 2019-05-20 22:46:58 +0200 |
---|---|---|
committer | Thomas Kemmer <tkemmer@computer.org> | 2019-05-23 21:06:51 +0200 |
commit | 15a64a05efb3aca96cfd0439e80d0ba39eec5a0e (patch) | |
tree | 1a4fb85afa31708410c7543604ea0dde3dd16270 | |
parent | 748d10de2808bc35b61162930c7adb150cc203e4 (diff) | |
download | cachetools-15a64a05efb3aca96cfd0439e80d0ba39eec5a0e.tar.gz |
Fix #135: Document how to use shared caches with @cachedmethod.
-rw-r--r-- | docs/index.rst | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/docs/index.rst b/docs/index.rst index e6379f3..956070a 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -325,6 +325,44 @@ often called with the same arguments: PEP #1: ... + When using a shared cache for multiple methods, be aware that + different cache keys must be created for each method even when + function arguments are the same, just as with the `@cached` + decorator: + + .. testcode:: + + class CachedReferences(object): + + def __init__(self, cachesize): + self.cache = LRUCache(maxsize=cachesize) + + @cachedmethod(lambda self: self.cache, key=partial(hashkey, 'pep')) + def get_pep(self, num): + """Retrieve text of a Python Enhancement Proposal""" + url = 'http://www.python.org/dev/peps/pep-%04d/' % num + with urllib.request.urlopen(url) as s: + return s.read() + + @cachedmethod(lambda self: self.cache, key=partial(hashkey, 'rfc')) + def get_rfc(self, num): + """Retrieve text of an IETF Request for Comments""" + url = 'https://tools.ietf.org/rfc/rfc%d.txt' % num + with urllib.request.urlopen(url) as s: + return s.read() + + docs = CachedReferences(cachesize=100) + print("PEP #1: %s" % docs.get_pep(1)) + print("RFC #1: %s" % docs.get_rfc(1)) + + .. testoutput:: + :hide: + :options: +ELLIPSIS + + PEP #1: ... + RFC #1: ... + + :mod:`cachetools.keys` --- Key functions for memoizing decorators ============================================================================ |