@@ -66,6 +66,38 @@ Some smaller changes made to the core Python language are:
6666New, Improved, and Deprecated Modules
6767=====================================
6868
69+ * The functools module now includes two new decorators for caching function
70+ calls, :func: `functools.lru_cache ` and :func: `functools.lfu_cache `. These can
71+ save repeated queries to an external resource whenever the results are
72+ expected to be the same.
73+
74+ For example, adding an LFU decorator to a database query function can save
75+ database accesses for the most popular searches::
76+
77+ @functools.lfu_cache(maxsize=50)
78+ def get_phone_number(name):
79+ c = conn.cursor()
80+ c.execute('SELECT phonenumber FROM phonelist WHERE name=?', (name,))
81+ return c.fetchone()[0]
82+
83+ The LFU (least-frequently-used) cache gives best results when the distribution
84+ of popular queries tends to remain the same over time. In contrast, the LRU
85+ (least-recently-used) cache gives best results when the distribution changes
86+ over time (for example, the most popular news articles change each day as
87+ newer articles are added).
88+
89+ The two caching decorators can be composed (nested) to handle hybrid cases
90+ that have both long-term access patterns and some short-term access trends.
91+ For example, music searches can reflect both long-term patterns (popular
92+ classics) and short-term trends (new releases)::
93+
94+ @functools.lfu_cache(maxsize=500)
95+ @functools.lru_cache(maxsize=100)
96+ def find_music(song):
97+ ...
98+
99+ (Contributed by Raymond Hettinger)
100+
69101* The previously deprecated :func: `contextlib.nested ` function has been
70102 removed in favor of a plain :keyword: `with ` statement which can
71103 accept multiple context managers. The latter technique is faster
0 commit comments