Skip to content

Commit 051ff52

Browse files
Zheaolirhettinger
authored andcommitted
bpo-38565: add new cache_parameters method for lru_cache (GH-16916)
1 parent 98480ce commit 051ff52

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

Doc/library/functools.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ The :mod:`functools` module defines the following functions:
108108
cached separately. For example, ``f(3)`` and ``f(3.0)`` will be treated
109109
as distinct calls with distinct results.
110110

111+
The wrapped function is instrumented with a :func:`cache_parameters`
112+
function that returns a new :class:`dict` showing the values for *maxsize*
113+
and *typed*. This is for information purposes only. Mutating the values
114+
has no effect.
115+
111116
To help measure the effectiveness of the cache and tune the *maxsize*
112117
parameter, the wrapped function is instrumented with a :func:`cache_info`
113118
function that returns a :term:`named tuple` showing *hits*, *misses*,
@@ -178,6 +183,9 @@ The :mod:`functools` module defines the following functions:
178183
.. versionchanged:: 3.8
179184
Added the *user_function* option.
180185

186+
.. versionadded:: 3.9
187+
Added the function :func:`cache_parameters`
188+
181189
.. decorator:: total_ordering
182190

183191
Given a class defining one or more rich comparison ordering methods, this

Lib/functools.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,15 @@ def lru_cache(maxsize=128, typed=False):
499499
# The user_function was passed in directly via the maxsize argument
500500
user_function, maxsize = maxsize, 128
501501
wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo)
502+
wrapper.cache_parameters = lambda : {'maxsize': maxsize, 'typed': typed}
502503
return update_wrapper(wrapper, user_function)
503504
elif maxsize is not None:
504505
raise TypeError(
505506
'Expected first argument to be an integer, a callable, or None')
506507

507508
def decorating_function(user_function):
508509
wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo)
510+
wrapper.cache_parameters = lambda : {'maxsize': maxsize, 'typed': typed}
509511
return update_wrapper(wrapper, user_function)
510512

511513
return decorating_function

Lib/test/test_functools.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,6 +1655,17 @@ def orig(x, y):
16551655
f_copy = copy.deepcopy(f)
16561656
self.assertIs(f_copy, f)
16571657

1658+
def test_lru_cache_parameters(self):
1659+
@self.module.lru_cache(maxsize=2)
1660+
def f():
1661+
return 1
1662+
self.assertEqual(f.cache_parameters(), {'maxsize': 2, "typed": False})
1663+
1664+
@self.module.lru_cache(maxsize=1000, typed=True)
1665+
def f():
1666+
return 1
1667+
self.assertEqual(f.cache_parameters(), {'maxsize': 1000, "typed": True})
1668+
16581669

16591670
@py_functools.lru_cache()
16601671
def py_cached_func(x, y):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add new cache_parameters() method for functools.lru_cache() to better support pickling.

0 commit comments

Comments
 (0)