[Python-checkins] r83516 - python/branches/py3k/Doc/whatsnew/3.2.rst

raymond.hettinger python-checkins at python.org
Mon Aug 2 03:43:42 CEST 2010


Author: raymond.hettinger
Date: Mon Aug  2 03:43:41 2010
New Revision: 83516

Log:
Demonstrate the caching decorators in whatsnew.

Modified:
   python/branches/py3k/Doc/whatsnew/3.2.rst

Modified: python/branches/py3k/Doc/whatsnew/3.2.rst
==============================================================================
--- python/branches/py3k/Doc/whatsnew/3.2.rst	(original)
+++ python/branches/py3k/Doc/whatsnew/3.2.rst	Mon Aug  2 03:43:41 2010
@@ -66,6 +66,38 @@
 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


More information about the Python-checkins mailing list