summaryrefslogtreecommitdiffstats
path: root/Doc/whatsnew
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2010-08-02 01:43:41 (GMT)
committerRaymond Hettinger <python@rcn.com>2010-08-02 01:43:41 (GMT)
commitaed05eb6b84456f8a97382f65a2d38779249b4a2 (patch)
treeda368e8309e17101351ad83dbedd93fcd19aadea /Doc/whatsnew
parentc8dc62d6028e51d06b8643c482bd2c7ec0a03d47 (diff)
downloadcpython-aed05eb6b84456f8a97382f65a2d38779249b4a2.zip
cpython-aed05eb6b84456f8a97382f65a2d38779249b4a2.tar.gz
cpython-aed05eb6b84456f8a97382f65a2d38779249b4a2.tar.bz2
Demonstrate the caching decorators in whatsnew.
Diffstat (limited to 'Doc/whatsnew')
-rw-r--r--Doc/whatsnew/3.2.rst32
1 files changed, 32 insertions, 0 deletions
diff --git a/Doc/whatsnew/3.2.rst b/Doc/whatsnew/3.2.rst
index d1ab51b..924d7f5 100644
--- a/Doc/whatsnew/3.2.rst
+++ b/Doc/whatsnew/3.2.rst
@@ -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