]> git.bitcoin.ninja Git - youtube-dl/commitdiff
[utils] Implement cache for OnDemandPagedList
authorYen Chi Hsuan <yan12125@gmail.com>
Tue, 23 Feb 2016 04:17:02 +0000 (12:17 +0800)
committerYen Chi Hsuan <yan12125@gmail.com>
Tue, 23 Feb 2016 05:11:20 +0000 (13:11 +0800)
youtube_dl/utils.py

index 6978a10e4cfa9b89874f5ec1da15c30a3e6d7e99..8ec70f93cd51ee6c05d43dedd8a1962f57a7f40e 100644 (file)
@@ -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