add 'keyarg' argument to cache-decorator

pull/13/head
Mike Fährmann 9 years ago
parent 81096f7790
commit 738c65d54f
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -20,27 +20,31 @@ def init_database():
global _db global _db
path_default = os.path.join(tempfile.gettempdir(), ".gallery-dl.cache") path_default = os.path.join(tempfile.gettempdir(), ".gallery-dl.cache")
path = config.get(("cache", "file"), path_default) path = config.get(("cache", "file"), path_default)
# timeout = config.get(("cache", "timeout"), 30) _db = sqlite3.connect(path, timeout=30, check_same_thread=False)
_db = sqlite3.connect(path, timeout=30)
_db.execute("CREATE TABLE IF NOT EXISTS data (" _db.execute("CREATE TABLE IF NOT EXISTS data ("
"key TEXT PRIMARY KEY," "key TEXT PRIMARY KEY,"
"value TEXT," "value TEXT,"
"expires INTEGER" "expires INTEGER"
")") ")")
def cache(max_age=3600): def cache(maxage=3600, keyarg=None):
"""decorator - cache function return values in memory and database""" """decorator - cache function return values in memory and database"""
def wrap(func): def wrap(func):
key = "{}.{}".format(func.__module__, func.__name__) gkey = "{}.{}".format(func.__module__, func.__name__)
def wrapped(*args): def wrapped(*args):
timestamp = time.time() timestamp = time.time()
if keyarg is not None:
key = "{}-{}".format(gkey, args[keyarg])
else:
key = gkey
try: try:
result = lookup_cache(timestamp) result = lookup_cache(key, timestamp)
except CacheInvalidError: except CacheInvalidError:
try: try:
result = func(*args) result = func(*args)
expires = int(timestamp+max_age) expires = int(timestamp+maxage)
_cache[key] = (result, expires) _cache[key] = (result, expires)
_db.execute("INSERT OR REPLACE INTO data VALUES (?,?,?)", _db.execute("INSERT OR REPLACE INTO data VALUES (?,?,?)",
(key, pickle.dumps(result), expires)) (key, pickle.dumps(result), expires))
@ -48,18 +52,18 @@ def cache(max_age=3600):
_db.commit() _db.commit()
return result return result
def lookup_cache(timestamp): def lookup_cache(key, timestamp):
try: try:
result, expires = _cache[key] result, expires = _cache[key]
if timestamp < expires: if timestamp < expires:
return result return result
except KeyError: except KeyError:
pass pass
result, expires = lookup_database(timestamp) result, expires = lookup_database(key, timestamp)
_cache[key] = (result, expires) _cache[key] = (result, expires)
return result return result
def lookup_database(timestamp): def lookup_database(key, timestamp):
try: try:
cursor = _db.cursor() cursor = _db.cursor()
cursor.execute("BEGIN EXCLUSIVE") cursor.execute("BEGIN EXCLUSIVE")
@ -69,8 +73,8 @@ def cache(max_age=3600):
if timestamp < expires: if timestamp < expires:
_db.commit() _db.commit()
return pickle.loads(result), expires return pickle.loads(result), expires
except Exception as e: except TypeError:
print(e) pass
raise CacheInvalidError() raise CacheInvalidError()
return wrapped return wrapped
@ -81,16 +85,3 @@ def cache(max_age=3600):
_db = None _db = None
_cache = {} _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())

Loading…
Cancel
Save