1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
# -*- coding: utf-8 -*-
# Copyright 2016-2020 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
"""Decorators to keep function results in an in-memory and database cache"""
import sqlite3
import pickle
import time
import os
import functools
from . import config, util
class CacheDecorator():
"""Simplified in-memory cache"""
def __init__(self, func, keyarg):
self.func = func
self.cache = {}
self.keyarg = keyarg
def __get__(self, instance, cls):
return functools.partial(self.__call__, instance)
def __call__(self, *args, **kwargs):
key = "" if self.keyarg is None else args[self.keyarg]
try:
value = self.cache[key]
except KeyError:
value = self.cache[key] = self.func(*args, **kwargs)
return value
def update(self, key, value):
self.cache[key] = value
def invalidate(self, key=""):
try:
del self.cache[key]
except KeyError:
pass
class MemoryCacheDecorator(CacheDecorator):
"""In-memory cache"""
def __init__(self, func, keyarg, maxage):
CacheDecorator.__init__(self, func, keyarg)
self.maxage = maxage
def __call__(self, *args, **kwargs):
key = "" if self.keyarg is None else args[self.keyarg]
timestamp = int(time.time())
try:
value, expires = self.cache[key]
except KeyError:
expires = 0
if expires < timestamp:
value = self.func(*args, **kwargs)
expires = timestamp + self.maxage
self.cache[key] = value, expires
return value
def update(self, key, value):
self.cache[key] = value, int(time.time()) + self.maxage
class DatabaseCacheDecorator():
"""Database cache"""
db = None
_init = True
def __init__(self, func, keyarg, maxage):
self.key = "%s.%s" % (func.__module__, func.__name__)
self.func = func
self.cache = {}
self.keyarg = keyarg
self.maxage = maxage
def __get__(self, obj, objtype):
return functools.partial(self.__call__, obj)
def __call__(self, *args, **kwargs):
key = "" if self.keyarg is None else args[self.keyarg]
timestamp = int(time.time())
# in-memory cache lookup
try:
value, expires = self.cache[key]
if expires > timestamp:
return value
except KeyError:
pass
# database lookup
fullkey = "%s-%s" % (self.key, key)
with self.database() as db:
cursor = db.cursor()
try:
cursor.execute("BEGIN EXCLUSIVE")
except sqlite3.OperationalError:
pass # Silently swallow exception - workaround for Python 3.6
cursor.execute(
"SELECT value, expires FROM data WHERE key=? LIMIT 1",
(fullkey,),
)
result = cursor.fetchone()
if result and result[1] > timestamp:
value, expires = result
value = pickle.loads(value)
else:
value = self.func(*args, **kwargs)
expires = timestamp + self.maxage
cursor.execute(
"INSERT OR REPLACE INTO data VALUES (?,?,?)",
(fullkey, pickle.dumps(value), expires),
)
self.cache[key] = value, expires
return value
def update(self, key, value):
expires = int(time.time()) + self.maxage
self.cache[key] = value, expires
with self.database() as db:
db.execute(
"INSERT OR REPLACE INTO data VALUES (?,?,?)",
("%s-%s" % (self.key, key), pickle.dumps(value), expires),
)
def invalidate(self, key):
try:
del self.cache[key]
except KeyError:
pass
with self.database() as db:
db.execute(
"DELETE FROM data WHERE key=?",
("%s-%s" % (self.key, key),),
)
def database(self):
if self._init:
self.db.execute(
"CREATE TABLE IF NOT EXISTS data "
"(key TEXT PRIMARY KEY, value TEXT, expires INTEGER)"
)
DatabaseCacheDecorator._init = False
return self.db
def memcache(maxage=None, keyarg=None):
if maxage:
def wrap(func):
return MemoryCacheDecorator(func, keyarg, maxage)
else:
def wrap(func):
return CacheDecorator(func, keyarg)
return wrap
def cache(maxage=3600, keyarg=None):
def wrap(func):
return DatabaseCacheDecorator(func, keyarg, maxage)
return wrap
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:
return util.expand_path(path)
if os.name == "nt":
import tempfile
return os.path.join(tempfile.gettempdir(), ".gallery-dl.cache")
cachedir = util.expand_path(os.path.join(
os.environ.get("XDG_CACHE_HOME", "~/.cache"), "gallery-dl"))
os.makedirs(cachedir, exist_ok=True)
return os.path.join(cachedir, "cache.sqlite3")
try:
dbfile = _path()
if os.name != "nt":
# restrict access permissions for new db files
os.close(os.open(dbfile, os.O_CREAT | os.O_RDONLY, 0o600))
DatabaseCacheDecorator.db = sqlite3.connect(
dbfile, timeout=30, check_same_thread=False)
except (OSError, TypeError, sqlite3.OperationalError):
cache = memcache # noqa: F811
|