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:
@@ -181,6 +181,18 @@ def main():
|
|||||||
if test:
|
if test:
|
||||||
print("Example :", test[0])
|
print("Example :", test[0])
|
||||||
print()
|
print()
|
||||||
|
elif args.clear_cache:
|
||||||
|
from . import cache
|
||||||
|
log = logging.getLogger("cache")
|
||||||
|
cnt = cache.clear()
|
||||||
|
|
||||||
|
if cnt is None:
|
||||||
|
log.error("Database file not available")
|
||||||
|
else:
|
||||||
|
log.info(
|
||||||
|
"Deleted %d %s from '%s'",
|
||||||
|
cnt, "entry" if cnt == 1 else "entries", cache._path(),
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
if not args.urls and not args.inputfile:
|
if not args.urls and not args.inputfile:
|
||||||
parser.error(
|
parser.error(
|
||||||
|
|||||||
@@ -166,19 +166,39 @@ def cache(maxage=3600, keyarg=None):
|
|||||||
return wrap
|
return wrap
|
||||||
|
|
||||||
|
|
||||||
try:
|
def clear():
|
||||||
path = config.get(("cache", "file"), "")
|
"""Delete all database entries"""
|
||||||
if path is None:
|
db = DatabaseCacheDecorator.db
|
||||||
raise RuntimeError()
|
|
||||||
elif not path:
|
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 tempfile
|
||||||
import os.path
|
import os.path
|
||||||
path = os.path.join(tempfile.gettempdir(), ".gallery-dl.cache")
|
return os.path.join(tempfile.gettempdir(), ".gallery-dl.cache")
|
||||||
else:
|
|
||||||
path = util.expand_path(path)
|
|
||||||
|
|
||||||
|
return util.expand_path(path)
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
DatabaseCacheDecorator.db = sqlite3.connect(
|
DatabaseCacheDecorator.db = sqlite3.connect(
|
||||||
path, timeout=30, check_same_thread=False)
|
_path(), timeout=30, check_same_thread=False)
|
||||||
|
except (TypeError, sqlite3.OperationalError):
|
||||||
except (RuntimeError, sqlite3.OperationalError):
|
|
||||||
cache = memcache # noqa: F811
|
cache = memcache # noqa: F811
|
||||||
|
|||||||
@@ -93,6 +93,11 @@ def build_parser():
|
|||||||
metavar="URL", action=ConfigAction, dest="proxy",
|
metavar="URL", action=ConfigAction, dest="proxy",
|
||||||
help="Use the specified proxy",
|
help="Use the specified proxy",
|
||||||
)
|
)
|
||||||
|
general.add_argument(
|
||||||
|
"--clear-cache",
|
||||||
|
dest="clear_cache", action="store_true",
|
||||||
|
help="Delete all cached login sessions, cookies, etc.",
|
||||||
|
)
|
||||||
|
|
||||||
output = parser.add_argument_group("Output Options")
|
output = parser.add_argument_group("Output Options")
|
||||||
output.add_argument(
|
output.add_argument(
|
||||||
|
|||||||
Reference in New Issue
Block a user