Skip to content

Commit

Permalink
Fix #138: Add documentation regarding caching of exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
tkem committed Jan 21, 2025
1 parent d5c6892 commit efc3633
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,43 @@ often called with the same arguments:
[..., (('fib', 42), 267914296), ..., (('luc', 42), 599074578)]


Function invocations are _not_ cached if any exception are raised.
To cache some (or all) calls raising exceptions, additional
function wrappers may be introduced which wrap exceptions as
regular function results for caching purposes:

.. testcode::

@cached(cache=LRUCache(maxsize=10), info=True)
def _get_pep_wrapped(num):
url = "http://www.python.org/dev/peps/pep-%04d/" % num
try:
with urllib.request.urlopen(url) as s:
return s.read()
except urllib.error.HTTPError as e:
# note that only HTTPError instances are cached
return e

def get_pep(num):
"Retrieve text of a Python Enhancement Proposal"

res = _get_pep_wrapped(num)
if isinstance(res, Exception):
raise res
else:
return res

try:
get_pep(100_000_000)
except Exception as e:
print(e, "-", _get_pep_wrapped.cache_info())

try:
get_pep(100_000_000)
except Exception as e:
print(e, "-", _get_pep_wrapped.cache_info())


.. decorator:: cachedmethod(cache, key=cachetools.keys.methodkey, lock=None)

Decorator to wrap a class or instance method with a memoizing
Expand Down

0 comments on commit efc3633

Please sign in to comment.