implement '--clear-cache'

Effectively clears all cached values from the cache database by
executing "DELETE FROM data" without any further user input.
This commit is contained in:
Mike Fährmann
2019-04-25 21:30:16 +02:00
parent 0318c610dc
commit bc26fc2439
3 changed files with 48 additions and 11 deletions

View File

@@ -166,19 +166,39 @@ def cache(maxage=3600, keyarg=None):
return wrap
try:
path = config.get(("cache", "file"), "")
if path is None:
raise RuntimeError()
elif not path:
def clear():
"""Delete all database entries"""
db = DatabaseCacheDecorator.db
if db:
rowcount = 0
cursor = db.cursor()
try:
cursor.execute("DELETE FROM data")
except sqlite3.OperationalError:
pass # database is not initialized, can't be modified, etc.
else:
rowcount = cursor.rowcount
db.commit()
cursor.execute("VACUUM")
return rowcount
return None
def _path():
path = config.get(("cache", "file"), -1)
if path == -1:
import tempfile
import os.path
path = os.path.join(tempfile.gettempdir(), ".gallery-dl.cache")
else:
path = util.expand_path(path)
return os.path.join(tempfile.gettempdir(), ".gallery-dl.cache")
return util.expand_path(path)
try:
DatabaseCacheDecorator.db = sqlite3.connect(
path, timeout=30, check_same_thread=False)
except (RuntimeError, sqlite3.OperationalError):
_path(), timeout=30, check_same_thread=False)
except (TypeError, sqlite3.OperationalError):
cache = memcache # noqa: F811