classification
Title: Enhanced cache access API for functools.lru_cache
Type: enhancement Stage: resolved
Components: Versions: Python 3.2
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: ncoghlan Nosy List: georg.brandl, ncoghlan, rhettinger
Priority: high Keywords: patch

Created on 2010-11-30 04:12 by ncoghlan, last changed 2010-11-30 06:35 by rhettinger. This issue is now closed.

Files
File name Uploaded Description Edit
functools_lru_cache_introspection.diff ncoghlan, 2010-11-30 04:12 Code and test changes for f.cache.* introspection API
functools_lru_cache_info_method.diff ncoghlan, 2010-11-30 04:55 Simpler alternative using a cache_info() method
Messages (5)
msg122879 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2010-11-30 04:12
As per private email to Raymond, I would like to move the lru_cache access attributes and methods into a CacheInfo class, exposed as a "cache" attribute with several methods and read-only properties.

Read-only properties: hits, misses, maxsize
Methods: clear(), __len__()

As an implementation detail, cache_misses and cache_hits become nonlocal variables rather than attributes of the wrapper function.

Priority set to high, since the current API will be locked in as soon the first beta goes out.
msg122881 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2010-11-30 04:55
Raymond suggested a simple cache_info() method that returns a named tuple as a possible alternative.

I like that idea, as it makes displaying debugging information (the intended use case for these attributes) absolutely trivial:

>>> import functools
>>> @functools.lru_cache()
... def f(x):
...   return x
... 
>>> f.cache_info()
lru_cache_info(maxsize=100, currsize=0, hits=0, misses=0)

(Alternative patch attached)
msg122882 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-11-30 05:17
Okay, go ahead with the second patch.
With the following changes:

_CacheInfo = namedtuple("CacheInfo", "maxsize size hits misses")

Change the variable names:  
         cache_hits --> hits
         cache_misses --> misses

Add a "with lock:" to the cache_info() function.

Update the docs.

Thanks.
msg122886 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2010-11-30 06:20
Committed in r86878.

I put a XXX note in the relevant part of the 3.2 What's New rather than updating it directly.
msg122888 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-11-30 06:35
Thx
History
Date User Action Args
2010-11-30 06:35:27rhettingersetmessages: + msg122888
2010-11-30 06:21:52ncoghlansetstatus: open -> closed
type: enhancement
stage: resolved
2010-11-30 06:20:58ncoghlansetmessages: + msg122886
2010-11-30 05:17:49rhettingersetassignee: rhettinger -> ncoghlan
resolution: accepted
messages: + msg122882
2010-11-30 04:55:32ncoghlansetfiles: + functools_lru_cache_info_method.diff

messages: + msg122881
2010-11-30 04:12:04ncoghlancreate