From 885d0084883d67d0c9a58669baebb25f097b47dc Mon Sep 17 00:00:00 2001 From: Tilman Sauerbeck Date: Sun, 11 Jun 2017 13:59:21 +0200 Subject: [PATCH] Tweak use of in memory cache. 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 --- resources/lib/KodiHelper.py | 21 ++------------------- resources/lib/Navigation.py | 11 ++++++++--- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/resources/lib/KodiHelper.py b/resources/lib/KodiHelper.py index 5b45281..0e5b661 100644 --- a/resources/lib/KodiHelper.py +++ b/resources/lib/KodiHelper.py @@ -282,22 +282,6 @@ class KodiHelper: """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 @@ -312,9 +296,8 @@ class KodiHelper: 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 diff --git a/resources/lib/Navigation.py b/resources/lib/Navigation.py index 1643706..2d690af 100644 --- a/resources/lib/Navigation.py +++ b/resources/lib/Navigation.py @@ -541,9 +541,14 @@ class Navigation: """ 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() -- 2.30.2