diff options
author | Yen Chi Hsuan <yan12125@gmail.com> | 2016-02-23 12:17:02 +0800 |
---|---|---|
committer | Yen Chi Hsuan <yan12125@gmail.com> | 2016-02-23 13:11:20 +0800 |
commit | b95dc034ca96fbb19995c54a8c75b5d46116a1e1 (patch) | |
tree | a237ca3d478df7401b5a9ad8e8c3b090f0cbae9c /youtube_dl/utils.py | |
parent | 86a7dbe66e9aed051daa8c19a1ba8b860959a30a (diff) |
[utils] Implement cache for OnDemandPagedList
Diffstat (limited to 'youtube_dl/utils.py')
-rw-r--r-- | youtube_dl/utils.py | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index 6978a10e4..8ec70f93c 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -1570,9 +1570,12 @@ class PagedList(object): class OnDemandPagedList(PagedList): - def __init__(self, pagefunc, pagesize): + def __init__(self, pagefunc, pagesize, use_cache=False): self._pagefunc = pagefunc self._pagesize = pagesize + self._use_cache = use_cache + if use_cache: + self._cache = {} def getslice(self, start=0, end=None): res = [] @@ -1582,7 +1585,13 @@ class OnDemandPagedList(PagedList): if start >= nextfirstid: 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 = ( start % self._pagesize |