Tweak use of in memory cache.
authorTilman Sauerbeck <tilman@code-monkey.de>
Sun, 11 Jun 2017 11:59:21 +0000 (13:59 +0200)
committers.golasch <s.golasch@telekom.de>
Sat, 5 Aug 2017 18:09:16 +0000 (20:09 +0200)
Previously we used to load the cache once to check if the requested
item was in there, and then another time to actually load it.

Combine both functions into one for less overhead.

Signed-off-by: s.golasch <s.golasch@telekom.de>
resources/lib/KodiHelper.py
resources/lib/Navigation.py

index 5b4528116fcb7d5a56f43775e8da757c32e92a08..0e5b6617de3696a9ba009cee73df3dcf1b84184c 100644 (file)
@@ -282,22 +282,6 @@ class KodiHelper:
         """Invalidates the memory cache"""
         xbmcgui.Window(xbmcgui.getCurrentWindowId()).setProperty('memcache', pickle.dumps({}))
 
         """Invalidates the memory cache"""
         xbmcgui.Window(xbmcgui.getCurrentWindowId()).setProperty('memcache', pickle.dumps({}))
 
-    def has_cached_item (self, cache_id):
-        """Checks if the requested item is in memory cache
-
-        Parameters
-        ----------
-        cache_id : :obj:`str`
-            ID of the cache entry
-
-        Returns
-        -------
-        bool
-            Item is cached
-        """
-        cached_items = pickle.loads(xbmcgui.Window(xbmcgui.getCurrentWindowId()).getProperty('memcache'))
-        return cache_id in cached_items.keys()
-
     def get_cached_item (self, cache_id):
         """Returns an item from the in memory cache
 
     def get_cached_item (self, cache_id):
         """Returns an item from the in memory cache
 
@@ -312,9 +296,8 @@ class KodiHelper:
             Contents of the requested cache item or none
         """
         cached_items = pickle.loads(xbmcgui.Window(xbmcgui.getCurrentWindowId()).getProperty('memcache'))
             Contents of the requested cache item or none
         """
         cached_items = pickle.loads(xbmcgui.Window(xbmcgui.getCurrentWindowId()).getProperty('memcache'))
-        if self.has_cached_item(cache_id) != True:
-            return None
-        return cached_items[cache_id]
+
+        return cached_items.get(cache_id)
 
     def add_cached_item (self, cache_id, contents):
         """Adds an item to the in memory cache
 
     def add_cached_item (self, cache_id, contents):
         """Adds an item to the in memory cache
index 1643706551e3992606cf7d6b74ff98c349892939..2d690af942d4459039cee4bf6fc3a000c0d4f186 100644 (file)
@@ -541,9 +541,14 @@ class Navigation:
         """
         url_values = urllib.urlencode(params)
         # check for cached items
         """
         url_values = urllib.urlencode(params)
         # check for cached items
-        if self.kodi_helper.has_cached_item(cache_id=url_values) and params.get('cache', False) == True:
-            self.log(msg='Fetching item from cache: (cache_id=' + url_values + ')')
-            return self.kodi_helper.get_cached_item(cache_id=url_values)
+        if params.get('cache', False) == True:
+            cached_value = self.kodi_helper.get_cached_item(cache_id=url_values)
+
+            # Cache lookup successful?
+            if cached_value != None:
+                self.log(msg='Fetched item from cache: (cache_id=' + url_values + ')')
+                return cached_value
+
         url = self.get_netflix_service_url()
         full_url = url + '?' + url_values
         data = urllib2.urlopen(full_url).read()
         url = self.get_netflix_service_url()
         full_url = url + '?' + url_values
         data = urllib2.urlopen(full_url).read()