Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
5e20bab4
Commit
5e20bab4
authored
Nov 30, 2010
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Neaten-up a bit.
parent
7921b9f2
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
12 additions
and
15 deletions
+12
-15
Doc/library/functools.rst
Doc/library/functools.rst
+3
-3
Doc/whatsnew/3.2.rst
Doc/whatsnew/3.2.rst
+3
-5
Lib/functools.py
Lib/functools.py
+6
-7
No files found.
Doc/library/functools.rst
View file @
5e20bab4
...
@@ -72,10 +72,10 @@ The :mod:`functools` module defines the following functions:
...
@@ -72,10 +72,10 @@ The :mod:`functools` module defines the following functions:
A `LRU (least recently used) cache
A `LRU (least recently used) cache
<http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used>`_
<http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used>`_
is indicated when the pattern of calls changes over time, such as
works best when more recent calls are the best predictors of upcoming calls
when more recent calls are the best predictors of upcoming calls
(for example, the most popular articles on a news server tend to
(for example, the most popular articles on a news server tend to
change every day).
change each day). The cache's size limit assurs that caching does not
grow without bound on long-running processes such as web servers.
.. versionadded:: 3.2
.. versionadded:: 3.2
...
...
Doc/whatsnew/3.2.rst
View file @
5e20bab4
...
@@ -332,15 +332,13 @@ New, Improved, and Deprecated Modules
...
@@ -332,15 +332,13 @@ New, Improved, and Deprecated Modules
c.execute('SELECT phonenumber FROM phonelist WHERE name=?', (name,))
c.execute('SELECT phonenumber FROM phonelist WHERE name=?', (name,))
return c.fetchone()[0]
return c.fetchone()[0]
XXX: update for Issue 10586 changes to cache statistics API
To help with choosing an effective cache size, the wrapped function is
To help with choosing an effective cache size, the wrapped function is
instrumented with
two attributes *cache_hits* and *cache_misses*
:
instrumented with
info function
:
>>> for name in user_requests:
>>> for name in user_requests:
... get_phone_number(name)
... get_phone_number(name)
>>>
print(get_phone_number.cache_hits, get_phone_number.cache_misses
)
>>>
get_phone_number.cache_info(
)
4805 980
CacheInfo(maxsize=300, size=300, hits=4805, misses=980)
If the phonelist table gets updated, the outdated contents of the cache can be
If the phonelist table gets updated, the outdated contents of the cache can be
cleared with:
cleared with:
...
...
Lib/functools.py
View file @
5e20bab4
...
@@ -121,10 +121,9 @@ def lru_cache(maxsize=100):
...
@@ -121,10 +121,9 @@ def lru_cache(maxsize=100):
Arguments to the cached function must be hashable.
Arguments to the cached function must be hashable.
Significant statistics (maxsize, size, hits, misses) are
View the cache statistics named tuple (maxsize, size, hits, misses) with
available through the f.cache_info() named tuple.
f.cache_info(). Clear the cache and statistics with f.cache_clear().
Clear the cache and statistics using f.cache_clear().
And access the underlying function with f.__wrapped__.
The underlying function is stored in f.__wrapped__.
See: http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used
See: http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used
...
@@ -141,7 +140,7 @@ def lru_cache(maxsize=100):
...
@@ -141,7 +140,7 @@ def lru_cache(maxsize=100):
cache_popitem
=
cache
.
popitem
cache_popitem
=
cache
.
popitem
cache_renew
=
cache
.
move_to_end
cache_renew
=
cache
.
move_to_end
hits
=
misses
=
0
hits
=
misses
=
0
kwd_mark
=
object
()
# separate positional and keyword args
kwd_mark
=
object
()
# separate
s
positional and keyword args
lock
=
Lock
()
lock
=
Lock
()
@
wraps
(
user_function
)
@
wraps
(
user_function
)
...
@@ -165,7 +164,7 @@ def lru_cache(maxsize=100):
...
@@ -165,7 +164,7 @@ def lru_cache(maxsize=100):
return
result
return
result
def
cache_info
():
def
cache_info
():
"""Report
significant
cache statistics"""
"""Report cache statistics"""
with
lock
:
with
lock
:
return
_CacheInfo
(
maxsize
,
len
(
cache
),
hits
,
misses
)
return
_CacheInfo
(
maxsize
,
len
(
cache
),
hits
,
misses
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment