Browse Source

[utils] Implement cache for OnDemandPagedList

totalwebcasting
Yen Chi Hsuan 9 years ago
parent
commit
b95dc034ca
1 changed files with 11 additions and 2 deletions
  1. +11
    -2
      youtube_dl/utils.py

+ 11
- 2
youtube_dl/utils.py View File

@ -1570,9 +1570,12 @@ class PagedList(object):
class OnDemandPagedList(PagedList): class OnDemandPagedList(PagedList):
def __init__(self, pagefunc, pagesize):
def __init__(self, pagefunc, pagesize, use_cache=False):
self._pagefunc = pagefunc self._pagefunc = pagefunc
self._pagesize = pagesize self._pagesize = pagesize
self._use_cache = use_cache
if use_cache:
self._cache = {}
def getslice(self, start=0, end=None): def getslice(self, start=0, end=None):
res = [] res = []
@ -1582,7 +1585,13 @@ class OnDemandPagedList(PagedList):
if start >= nextfirstid: if start >= nextfirstid:
continue continue
page_results = list(self._pagefunc(pagenum))
page_results = None
if self._use_cache:
page_results = self._cache.get(pagenum)
if page_results is None:
page_results = list(self._pagefunc(pagenum))
if self._use_cache:
self._cache[pagenum] = page_results
startv = ( startv = (
start % self._pagesize start % self._pagesize


Loading…
Cancel
Save