Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
86f96139
Kaydet (Commit)
86f96139
authored
Agu 06, 2010
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Improve the whatsnew article on the lru/lfu cache decorators.
üst
7e3b948c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
13 deletions
+24
-13
3.2.rst
Doc/whatsnew/3.2.rst
+24
-13
No files found.
Doc/whatsnew/3.2.rst
Dosyayı görüntüle @
86f96139
...
@@ -71,8 +71,8 @@ New, Improved, and Deprecated Modules
...
@@ -71,8 +71,8 @@ New, Improved, and Deprecated Modules
save repeated queries to an external resource whenever the results are
save repeated queries to an external resource whenever the results are
expected to be the same.
expected to be the same.
For example, adding a
n LFU
decorator to a database query function can save
For example, adding a
caching
decorator to a database query function can save
database accesses for
the most
popular searches::
database accesses for popular searches::
@functools.lfu_cache(maxsize=50)
@functools.lfu_cache(maxsize=50)
def get_phone_number(name):
def get_phone_number(name):
...
@@ -80,21 +80,32 @@ New, Improved, and Deprecated Modules
...
@@ -80,21 +80,32 @@ 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]
The
LFU (least-frequently-used) cache gives best results when the distribution
The
caches support two strategies for limiting their size to *maxsize*. The
of popular queries tends to remain the same over time. In contrast, the LRU
LFU (least-frequently-used) cache works bests when popular queries remain the
(least-recently-used) cache gives best results when the distribution changes
same over time. In contrast, the LRU (least-recently-used) cache works best
over time (for example, the most popular news articles change each day a
s
query popularity changes over time (for example, the most popular new
s
newer articles are added).
articles change each day as
newer articles are added).
The two caching decorators can be composed (nested) to handle hybrid cases
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
For example, music searches can reflect both long-term patterns (popular
classics) and short-term trends (new releases)::
classics) and short-term trends (new releases)::
@functools.lfu_cache(maxsize=500)
@functools.lfu_cache(maxsize=500)
@functools.lru_cache(maxsize=100)
@functools.lru_cache(maxsize=100)
def find_music(song):
def find_lyrics(song):
...
query = 'http://www.example.com/songlist/%s' % urllib.quote(song)
page = urllib.urlopen(query).read()
return parse_lyrics(page)
To help with choosing an effective cache size, the wrapped function
is instrumented with two attributes 'hits' and 'misses'::
>>> for song in user_requests:
... find_lyrics(song)
>>> print find_lyrics.hits
4805
>>> print find_lyrics.misses
980
(Contributed by Raymond Hettinger)
(Contributed by Raymond Hettinger)
...
...
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