From 9eb2e7890c8362b9135419a009d98b1fcf3a6e5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mice=20P=C3=A1pai?= Date: Fri, 5 May 2017 10:51:26 +0200 Subject: [PATCH] Fix serious cache key lookup performance problem Checking if a request is cached or not took ~6sec / item vs <0.01sec now, because __contains__ wasn't definied for the _ShelfCacheBackend class and it iterated over each cache key on every check --- pylast/__init__.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pylast/__init__.py b/pylast/__init__.py index d6ee580..c8f54f7 100644 --- a/pylast/__init__.py +++ b/pylast/__init__.py @@ -909,6 +909,10 @@ class _ShelfCacheBackend(object): """Used as a backend for caching cacheable requests.""" def __init__(self, file_path=None): self.shelf = shelve.open(file_path) + self.cache_keys = set(self.shelf.keys()) + + def __contains__(self, key): + return key in self.cache_keys def __iter__(self): return iter(self.shelf.keys()) @@ -917,6 +921,7 @@ class _ShelfCacheBackend(object): return self.shelf[key] def set_xml(self, key, xml_string): + self.cache_keys.add(key) self.shelf[key] = xml_string