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
c364a36f
Commit
c364a36f
authored
Aug 02, 2010
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Demonstrate the caching decorators in whatsnew.
parent
5b856e5c
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
0 deletions
+32
-0
Doc/whatsnew/3.2.rst
Doc/whatsnew/3.2.rst
+32
-0
No files found.
Doc/whatsnew/3.2.rst
View file @
c364a36f
...
...
@@ -66,6 +66,38 @@ Some smaller changes made to the core Python language are:
New, Improved, and Deprecated Modules
=====================================
* The functools module now includes two new decorators for caching function
calls, :func:`functools.lru_cache` and :func:`functools.lfu_cache`. These can
save repeated queries to an external resource whenever the results are
expected to be the same.
For example, adding an LFU decorator to a database query function can save
database accesses for the most popular searches::
@functools.lfu_cache(maxsize=50)
def get_phone_number(name):
c = conn.cursor()
c.execute('SELECT phonenumber FROM phonelist WHERE name=?', (name,))
return c.fetchone()[0]
The LFU (least-frequently-used) cache gives best results when the distribution
of popular queries tends to remain the same over time. In contrast, the LRU
(least-recently-used) cache gives best results when the distribution changes
over time (for example, the most popular news articles change each day as
newer articles are added).
The two caching decorators can be composed (nested) to handle hybrid cases
that have both long-term access patterns and some short-term access trends.
For example, music searches can reflect both long-term patterns (popular
classics) and short-term trends (new releases)::
@functools.lfu_cache(maxsize=500)
@functools.lru_cache(maxsize=100)
def find_music(song):
...
(Contributed by Raymond Hettinger)
* The previously deprecated :func:`contextlib.nested` function has been
removed in favor of a plain :keyword:`with` statement which can
accept multiple context managers. The latter technique is faster
...
...
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