From 738c65d54f9c8c69082dca505a0d321b64fb7eb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Sun, 6 Mar 2016 16:05:59 +0100 Subject: [PATCH] add 'keyarg' argument to cache-decorator --- gallery_dl/cache.py | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/gallery_dl/cache.py b/gallery_dl/cache.py index e2d90bc1..128d142c 100644 --- a/gallery_dl/cache.py +++ b/gallery_dl/cache.py @@ -20,27 +20,31 @@ def init_database(): global _db path_default = os.path.join(tempfile.gettempdir(), ".gallery-dl.cache") path = config.get(("cache", "file"), path_default) - # timeout = config.get(("cache", "timeout"), 30) - _db = sqlite3.connect(path, timeout=30) + _db = sqlite3.connect(path, timeout=30, check_same_thread=False) _db.execute("CREATE TABLE IF NOT EXISTS data (" "key TEXT PRIMARY KEY," "value TEXT," "expires INTEGER" ")") -def cache(max_age=3600): +def cache(maxage=3600, keyarg=None): """decorator - cache function return values in memory and database""" def wrap(func): - key = "{}.{}".format(func.__module__, func.__name__) + gkey = "{}.{}".format(func.__module__, func.__name__) def wrapped(*args): timestamp = time.time() + if keyarg is not None: + key = "{}-{}".format(gkey, args[keyarg]) + else: + key = gkey + try: - result = lookup_cache(timestamp) + result = lookup_cache(key, timestamp) except CacheInvalidError: try: result = func(*args) - expires = int(timestamp+max_age) + expires = int(timestamp+maxage) _cache[key] = (result, expires) _db.execute("INSERT OR REPLACE INTO data VALUES (?,?,?)", (key, pickle.dumps(result), expires)) @@ -48,18 +52,18 @@ def cache(max_age=3600): _db.commit() return result - def lookup_cache(timestamp): + def lookup_cache(key, timestamp): try: result, expires = _cache[key] if timestamp < expires: return result except KeyError: pass - result, expires = lookup_database(timestamp) + result, expires = lookup_database(key, timestamp) _cache[key] = (result, expires) return result - def lookup_database(timestamp): + def lookup_database(key, timestamp): try: cursor = _db.cursor() cursor.execute("BEGIN EXCLUSIVE") @@ -69,8 +73,8 @@ def cache(max_age=3600): if timestamp < expires: _db.commit() return pickle.loads(result), expires - except Exception as e: - print(e) + except TypeError: + pass raise CacheInvalidError() return wrapped @@ -81,16 +85,3 @@ def cache(max_age=3600): _db = None _cache = {} - -# @cache(50) -# def f(): - # time.sleep(15) - # print("called f()") - # return 1 - -# init_database() - -# for i in range(10): - # print(f()) - # time.sleep(1) -# print(f())