forked from grantjenks/python-diskcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.py
2481 lines (1912 loc) · 80.5 KB
/
core.py
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Core disk and file backed cache API.
"""
import codecs
import contextlib as cl
import errno
import functools as ft
import io
import json
import os
import os.path as op
import pickletools
import sqlite3
import struct
import sys
import tempfile
import threading
import time
import warnings
import zlib
############################################################################
# BEGIN Python 2/3 Shims
############################################################################
if sys.hexversion < 0x03000000:
import cPickle as pickle # pylint: disable=import-error
# ISSUE #25 Fix for http://bugs.python.org/issue10211
from cStringIO import StringIO as BytesIO # pylint: disable=import-error
from thread import get_ident # pylint: disable=import-error,no-name-in-module
TextType = unicode # pylint: disable=invalid-name,undefined-variable
BytesType = str
INT_TYPES = int, long # pylint: disable=undefined-variable
range = xrange # pylint: disable=redefined-builtin,invalid-name,undefined-variable
io_open = io.open # pylint: disable=invalid-name
else:
import pickle
from io import BytesIO # pylint: disable=ungrouped-imports
from threading import get_ident
TextType = str
BytesType = bytes
INT_TYPES = (int,)
io_open = open # pylint: disable=invalid-name
def full_name(func):
"Return full name of `func` by adding the module and function name."
try:
# The __qualname__ attribute is only available in Python 3.3 and later.
# GrantJ 2019-03-29 Remove after support for Python 2 is dropped.
name = func.__qualname__
except AttributeError:
name = func.__name__
return func.__module__ + '.' + name
############################################################################
# END Python 2/3 Shims
############################################################################
try:
WindowsError
except NameError:
class WindowsError(Exception):
"Windows error place-holder on platforms without support."
class Constant(tuple):
"Pretty display of immutable constant."
def __new__(cls, name):
return tuple.__new__(cls, (name,))
def __repr__(self):
return '%s' % self[0]
DBNAME = 'cache.db'
ENOVAL = Constant('ENOVAL')
UNKNOWN = Constant('UNKNOWN')
MODE_NONE = 0
MODE_RAW = 1
MODE_BINARY = 2
MODE_TEXT = 3
MODE_PICKLE = 4
DEFAULT_SETTINGS = {
u'statistics': 0, # False
u'tag_index': 0, # False
u'eviction_policy': u'least-recently-stored',
u'size_limit': 2 ** 30, # 1gb
u'cull_limit': 10,
u'sqlite_auto_vacuum': 1, # FULL
u'sqlite_cache_size': 2 ** 13, # 8,192 pages
u'sqlite_journal_mode': u'wal',
u'sqlite_mmap_size': 2 ** 26, # 64mb
u'sqlite_synchronous': 1, # NORMAL
u'disk_min_file_size': 2 ** 15, # 32kb
u'disk_pickle_protocol': pickle.HIGHEST_PROTOCOL,
}
METADATA = {
u'count': 0,
u'size': 0,
u'hits': 0,
u'misses': 0,
}
EVICTION_POLICY = {
'none': {
'init': None,
'get': None,
'cull': None,
},
'least-recently-stored': {
'init': (
'CREATE INDEX IF NOT EXISTS Cache_store_time ON'
' Cache (store_time)'
),
'get': None,
'cull': 'SELECT {fields} FROM Cache ORDER BY store_time LIMIT ?',
},
'least-recently-used': {
'init': (
'CREATE INDEX IF NOT EXISTS Cache_access_time ON'
' Cache (access_time)'
),
'get': 'access_time = {now}',
'cull': 'SELECT {fields} FROM Cache ORDER BY access_time LIMIT ?',
},
'least-frequently-used': {
'init': (
'CREATE INDEX IF NOT EXISTS Cache_access_count ON'
' Cache (access_count)'
),
'get': 'access_count = access_count + 1',
'cull': 'SELECT {fields} FROM Cache ORDER BY access_count LIMIT ?',
},
}
class Disk(object):
"Cache key and value serialization for SQLite database and files."
def __init__(self, directory, min_file_size=0, pickle_protocol=0):
"""Initialize disk instance.
:param str directory: directory path
:param int min_file_size: minimum size for file use
:param int pickle_protocol: pickle protocol for serialization
"""
self._directory = directory
self.min_file_size = min_file_size
self.pickle_protocol = pickle_protocol
def hash(self, key):
"""Compute portable hash for `key`.
:param key: key to hash
:return: hash value
"""
mask = 0xFFFFFFFF
disk_key, _ = self.put(key)
type_disk_key = type(disk_key)
if type_disk_key is sqlite3.Binary:
return zlib.adler32(disk_key) & mask
elif type_disk_key is TextType:
return zlib.adler32(disk_key.encode('utf-8')) & mask # pylint: disable=no-member
elif type_disk_key in INT_TYPES:
return disk_key % mask
else:
assert type_disk_key is float
return zlib.adler32(struct.pack('!d', disk_key)) & mask
def put(self, key):
"""Convert `key` to fields key and raw for Cache table.
:param key: key to convert
:return: (database key, raw boolean) pair
"""
# pylint: disable=bad-continuation,unidiomatic-typecheck
type_key = type(key)
if type_key is BytesType:
return sqlite3.Binary(key), True
elif ((type_key is TextType)
or (type_key in INT_TYPES
and -9223372036854775808 <= key <= 9223372036854775807)
or (type_key is float)):
return key, True
else:
data = pickle.dumps(key, protocol=self.pickle_protocol)
result = pickletools.optimize(data)
return sqlite3.Binary(result), False
def get(self, key, raw):
"""Convert fields `key` and `raw` from Cache table to key.
:param key: database key to convert
:param bool raw: flag indicating raw database storage
:return: corresponding Python key
"""
# pylint: disable=no-self-use,unidiomatic-typecheck
if raw:
return BytesType(key) if type(key) is sqlite3.Binary else key
else:
return pickle.load(BytesIO(key))
def store(self, value, read, key=UNKNOWN):
"""Convert `value` to fields size, mode, filename, and value for Cache
table.
:param value: value to convert
:param bool read: True when value is file-like object
:param key: key for item (default UNKNOWN)
:return: (size, mode, filename, value) tuple for Cache table
"""
# pylint: disable=unidiomatic-typecheck
type_value = type(value)
min_file_size = self.min_file_size
if ((type_value is TextType and len(value) < min_file_size)
or (type_value in INT_TYPES
and -9223372036854775808 <= value <= 9223372036854775807)
or (type_value is float)):
return 0, MODE_RAW, None, value
elif type_value is BytesType:
if len(value) < min_file_size:
return 0, MODE_RAW, None, sqlite3.Binary(value)
else:
filename, full_path = self.filename(key, value)
with open(full_path, 'wb') as writer:
writer.write(value)
return len(value), MODE_BINARY, filename, None
elif type_value is TextType:
filename, full_path = self.filename(key, value)
with io_open(full_path, 'w', encoding='UTF-8') as writer:
writer.write(value)
size = op.getsize(full_path)
return size, MODE_TEXT, filename, None
elif read:
size = 0
reader = ft.partial(value.read, 2 ** 22)
filename, full_path = self.filename(key, value)
with open(full_path, 'wb') as writer:
for chunk in iter(reader, b''):
size += len(chunk)
writer.write(chunk)
return size, MODE_BINARY, filename, None
else:
result = pickle.dumps(value, protocol=self.pickle_protocol)
if len(result) < min_file_size:
return 0, MODE_PICKLE, None, sqlite3.Binary(result)
else:
filename, full_path = self.filename(key, value)
with open(full_path, 'wb') as writer:
writer.write(result)
return len(result), MODE_PICKLE, filename, None
def fetch(self, mode, filename, value, read):
"""Convert fields `mode`, `filename`, and `value` from Cache table to
value.
:param int mode: value mode raw, binary, text, or pickle
:param str filename: filename of corresponding value
:param value: database value
:param bool read: when True, return an open file handle
:return: corresponding Python value
"""
# pylint: disable=no-self-use,unidiomatic-typecheck
if mode == MODE_RAW:
return BytesType(value) if type(value) is sqlite3.Binary else value
elif mode == MODE_BINARY:
if read:
return open(op.join(self._directory, filename), 'rb')
else:
with open(op.join(self._directory, filename), 'rb') as reader:
return reader.read()
elif mode == MODE_TEXT:
full_path = op.join(self._directory, filename)
with io_open(full_path, 'r', encoding='UTF-8') as reader:
return reader.read()
elif mode == MODE_PICKLE:
if value is None:
with open(op.join(self._directory, filename), 'rb') as reader:
return pickle.load(reader)
else:
return pickle.load(BytesIO(value))
def filename(self, key=UNKNOWN, value=UNKNOWN):
"""Return filename and full-path tuple for file storage.
Filename will be a randomly generated 28 character hexadecimal string
with ".val" suffixed. Two levels of sub-directories will be used to
reduce the size of directories. On older filesystems, lookups in
directories with many files may be slow.
The default implementation ignores the `key` and `value` parameters.
In some scenarios, for example :meth:`Cache.push
<diskcache.Cache.push>`, the `key` or `value` may not be known when the
item is stored in the cache.
:param key: key for item (default UNKNOWN)
:param value: value for item (default UNKNOWN)
"""
# pylint: disable=unused-argument
hex_name = codecs.encode(os.urandom(16), 'hex').decode('utf-8')
sub_dir = op.join(hex_name[:2], hex_name[2:4])
name = hex_name[4:] + '.val'
directory = op.join(self._directory, sub_dir)
try:
os.makedirs(directory)
except OSError as error:
if error.errno != errno.EEXIST:
raise
filename = op.join(sub_dir, name)
full_path = op.join(self._directory, filename)
return filename, full_path
def remove(self, filename):
"""Remove a file given by `filename`.
This method is cross-thread and cross-process safe. If an "error no
entry" occurs, it is suppressed.
:param str filename: relative path to file
"""
full_path = op.join(self._directory, filename)
try:
os.remove(full_path)
except WindowsError:
pass
except OSError as error:
if error.errno != errno.ENOENT:
# ENOENT may occur if two caches attempt to delete the same
# file at the same time.
raise
class JSONDisk(Disk):
"Cache key and value using JSON serialization with zlib compression."
def __init__(self, directory, compress_level=1, **kwargs):
"""Initialize JSON disk instance.
Keys and values are compressed using the zlib library. The
`compress_level` is an integer from 0 to 9 controlling the level of
compression; 1 is fastest and produces the least compression, 9 is
slowest and produces the most compression, and 0 is no compression.
:param str directory: directory path
:param int compress_level: zlib compression level (default 1)
:param kwargs: super class arguments
"""
self.compress_level = compress_level
super(JSONDisk, self).__init__(directory, **kwargs)
def put(self, key):
json_bytes = json.dumps(key).encode('utf-8')
data = zlib.compress(json_bytes, self.compress_level)
return super(JSONDisk, self).put(data)
def get(self, key, raw):
data = super(JSONDisk, self).get(key, raw)
return json.loads(zlib.decompress(data).decode('utf-8'))
def store(self, value, read, key=UNKNOWN):
if not read:
json_bytes = json.dumps(value).encode('utf-8')
value = zlib.compress(json_bytes, self.compress_level)
return super(JSONDisk, self).store(value, read, key=key)
def fetch(self, mode, filename, value, read):
data = super(JSONDisk, self).fetch(mode, filename, value, read)
if not read:
data = json.loads(zlib.decompress(data).decode('utf-8'))
return data
class Timeout(Exception):
"Database timeout expired."
class UnknownFileWarning(UserWarning):
"Warning used by Cache.check for unknown files."
class EmptyDirWarning(UserWarning):
"Warning used by Cache.check for empty directories."
def args_to_key(base, args, kwargs, typed):
"""Create cache key out of function arguments.
:param tuple base: base of key
:param tuple args: function arguments
:param dict kwargs: function keyword arguments
:param bool typed: include types in cache key
:return: cache key tuple
"""
key = base + args
if kwargs:
key += (ENOVAL,)
sorted_items = sorted(kwargs.items())
for item in sorted_items:
key += item
if typed:
key += tuple(type(arg) for arg in args)
if kwargs:
key += tuple(type(value) for _, value in sorted_items)
return key
class Cache(object):
"Disk and file backed cache."
# pylint: disable=bad-continuation
def __init__(self, directory=None, timeout=60, disk=Disk, **settings):
"""Initialize cache instance.
:param str directory: cache directory
:param float timeout: SQLite connection timeout
:param disk: Disk type or subclass for serialization
:param settings: any of DEFAULT_SETTINGS
"""
try:
assert issubclass(disk, Disk)
except (TypeError, AssertionError):
raise ValueError('disk must subclass diskcache.Disk')
if directory is None:
directory = tempfile.mkdtemp(prefix='diskcache-')
directory = op.expanduser(directory)
directory = op.expandvars(directory)
self._directory = directory
self._timeout = 0 # Manually handle retries during initialization.
self._local = threading.local()
self._txn_id = None
if not op.isdir(directory):
try:
os.makedirs(directory, 0o755)
except OSError as error:
if error.errno != errno.EEXIST:
raise EnvironmentError(
error.errno,
'Cache directory "%s" does not exist'
' and could not be created' % self._directory
)
sql = self._sql_retry
# Setup Settings table.
try:
current_settings = dict(sql(
'SELECT key, value FROM Settings'
).fetchall())
except sqlite3.OperationalError:
current_settings = {}
sets = DEFAULT_SETTINGS.copy()
sets.update(current_settings)
sets.update(settings)
for key in METADATA:
sets.pop(key, None)
# Chance to set pragmas before any tables are created.
for key, value in sorted(sets.items()):
if key.startswith('sqlite_'):
self.reset(key, value, update=False)
sql('CREATE TABLE IF NOT EXISTS Settings ('
' key TEXT NOT NULL UNIQUE,'
' value)'
)
# Setup Disk object (must happen after settings initialized).
kwargs = {
key[5:]: value for key, value in sets.items()
if key.startswith('disk_')
}
self._disk = disk(directory, **kwargs)
# Set cached attributes: updates settings and sets pragmas.
for key, value in sets.items():
query = 'INSERT OR REPLACE INTO Settings VALUES (?, ?)'
sql(query, (key, value))
self.reset(key, value)
for key, value in METADATA.items():
query = 'INSERT OR IGNORE INTO Settings VALUES (?, ?)'
sql(query, (key, value))
self.reset(key)
(self._page_size,), = sql('PRAGMA page_size').fetchall()
# Setup Cache table.
sql('CREATE TABLE IF NOT EXISTS Cache ('
' rowid INTEGER PRIMARY KEY,'
' key BLOB,'
' raw INTEGER,'
' store_time REAL,'
' expire_time REAL,'
' access_time REAL,'
' access_count INTEGER DEFAULT 0,'
' tag BLOB,'
' size INTEGER DEFAULT 0,'
' mode INTEGER DEFAULT 0,'
' filename TEXT,'
' value BLOB)'
)
sql('CREATE UNIQUE INDEX IF NOT EXISTS Cache_key_raw ON'
' Cache(key, raw)'
)
sql('CREATE INDEX IF NOT EXISTS Cache_expire_time ON'
' Cache (expire_time)'
)
query = EVICTION_POLICY[self.eviction_policy]['init']
if query is not None:
sql(query)
# Use triggers to keep Metadata updated.
sql('CREATE TRIGGER IF NOT EXISTS Settings_count_insert'
' AFTER INSERT ON Cache FOR EACH ROW BEGIN'
' UPDATE Settings SET value = value + 1'
' WHERE key = "count"; END'
)
sql('CREATE TRIGGER IF NOT EXISTS Settings_count_delete'
' AFTER DELETE ON Cache FOR EACH ROW BEGIN'
' UPDATE Settings SET value = value - 1'
' WHERE key = "count"; END'
)
sql('CREATE TRIGGER IF NOT EXISTS Settings_size_insert'
' AFTER INSERT ON Cache FOR EACH ROW BEGIN'
' UPDATE Settings SET value = value + NEW.size'
' WHERE key = "size"; END'
)
sql('CREATE TRIGGER IF NOT EXISTS Settings_size_update'
' AFTER UPDATE ON Cache FOR EACH ROW BEGIN'
' UPDATE Settings'
' SET value = value + NEW.size - OLD.size'
' WHERE key = "size"; END'
)
sql('CREATE TRIGGER IF NOT EXISTS Settings_size_delete'
' AFTER DELETE ON Cache FOR EACH ROW BEGIN'
' UPDATE Settings SET value = value - OLD.size'
' WHERE key = "size"; END'
)
# Create tag index if requested.
if self.tag_index: # pylint: disable=no-member
self.create_tag_index()
else:
self.drop_tag_index()
# Close and re-open database connection with given timeout.
self.close()
self._timeout = timeout
self._sql # pylint: disable=pointless-statement
@property
def directory(self):
"""Cache directory."""
return self._directory
@property
def timeout(self):
"""SQLite connection timeout value in seconds."""
return self._timeout
@property
def disk(self):
"""Disk used for serialization."""
return self._disk
@property
def _con(self):
# Check process ID to support process forking. If the process
# ID changes, close the connection and update the process ID.
local_pid = getattr(self._local, 'pid', None)
pid = os.getpid()
if local_pid != pid:
self.close()
self._local.pid = pid
con = getattr(self._local, 'con', None)
if con is None:
con = self._local.con = sqlite3.connect(
op.join(self._directory, DBNAME),
timeout=self._timeout,
isolation_level=None,
)
# Some SQLite pragmas work on a per-connection basis so
# query the Settings table and reset the pragmas. The
# Settings table may not exist so catch and ignore the
# OperationalError that may occur.
try:
select = 'SELECT key, value FROM Settings'
settings = con.execute(select).fetchall()
except sqlite3.OperationalError:
pass
else:
for key, value in settings:
if key.startswith('sqlite_'):
self.reset(key, value, update=False)
return con
@property
def _sql(self):
return self._con.execute
@property
def _sql_retry(self):
sql = self._sql
# 2018-11-01 GrantJ - Some SQLite builds/versions handle
# the SQLITE_BUSY return value and connection parameter
# "timeout" differently. For a more reliable duration,
# manually retry the statement for 60 seconds. Only used
# by statements which modify the database and do not use
# a transaction (like those in ``__init__`` or ``reset``).
# See Issue #85 for and tests/issue_85.py for more details.
def _execute_with_retry(statement, *args, **kwargs):
start = time.time()
while True:
try:
return sql(statement, *args, **kwargs)
except sqlite3.OperationalError as exc:
if str(exc) != 'database is locked':
raise
diff = time.time() - start
if diff > 60:
raise
time.sleep(0.001)
return _execute_with_retry
@cl.contextmanager
def transact(self, retry=False):
"""Context manager to perform a transaction by locking the cache.
While the cache is locked, no other write operation is permitted.
Transactions should therefore be as short as possible. Read and write
operations performed in a transaction are atomic. Read operations may
occur concurrent to a transaction.
Transactions may be nested and may not be shared between threads.
Raises :exc:`Timeout` error when database timeout occurs and `retry` is
`False` (default).
>>> cache = Cache()
>>> with cache.transact(): # Atomically increment two keys.
... _ = cache.incr('total', 123.4)
... _ = cache.incr('count', 1)
>>> with cache.transact(): # Atomically calculate average.
... average = cache['total'] / cache['count']
>>> average
123.4
:param bool retry: retry if database timeout occurs (default False)
:return: context manager for use in `with` statement
:raises Timeout: if database timeout occurs
"""
with self._transact(retry=retry):
yield
@cl.contextmanager
def _transact(self, retry=False, filename=None):
sql = self._sql
filenames = []
_disk_remove = self._disk.remove
tid = get_ident()
txn_id = self._txn_id
if tid == txn_id:
begin = False
else:
while True:
try:
sql('BEGIN IMMEDIATE')
begin = True
self._txn_id = tid
break
except sqlite3.OperationalError:
if retry:
continue
if filename is not None:
_disk_remove(filename)
raise Timeout
try:
yield sql, filenames.append
except BaseException:
if begin:
assert self._txn_id == tid
self._txn_id = None
sql('ROLLBACK')
raise
else:
if begin:
assert self._txn_id == tid
self._txn_id = None
sql('COMMIT')
for name in filenames:
if name is not None:
_disk_remove(name)
def set(self, key, value, expire=None, read=False, tag=None, retry=False):
"""Set `key` and `value` item in cache.
When `read` is `True`, `value` should be a file-like object opened
for reading in binary mode.
Raises :exc:`Timeout` error when database timeout occurs and `retry` is
`False` (default).
:param key: key for item
:param value: value for item
:param float expire: seconds until item expires
(default None, no expiry)
:param bool read: read value as bytes from file (default False)
:param str tag: text to associate with key (default None)
:param bool retry: retry if database timeout occurs (default False)
:return: True if item was set
:raises Timeout: if database timeout occurs
"""
now = time.time()
db_key, raw = self._disk.put(key)
expire_time = None if expire is None else now + expire
size, mode, filename, db_value = self._disk.store(value, read, key=key)
columns = (expire_time, tag, size, mode, filename, db_value)
# The order of SELECT, UPDATE, and INSERT is important below.
#
# Typical cache usage pattern is:
#
# value = cache.get(key)
# if value is None:
# value = expensive_calculation()
# cache.set(key, value)
#
# Cache.get does not evict expired keys to avoid writes during lookups.
# Commonly used/expired keys will therefore remain in the cache making
# an UPDATE the preferred path.
#
# The alternative is to assume the key is not present by first trying
# to INSERT and then handling the IntegrityError that occurs from
# violating the UNIQUE constraint. This optimistic approach was
# rejected based on the common cache usage pattern.
#
# INSERT OR REPLACE aka UPSERT is not used because the old filename may
# need cleanup.
with self._transact(retry, filename) as (sql, cleanup):
rows = sql(
'SELECT rowid, filename FROM Cache'
' WHERE key = ? AND raw = ?',
(db_key, raw),
).fetchall()
if rows:
(rowid, old_filename), = rows
cleanup(old_filename)
self._row_update(rowid, now, columns)
else:
self._row_insert(db_key, raw, now, columns)
self._cull(now, sql, cleanup)
return True
def __setitem__(self, key, value):
"""Set corresponding `value` for `key` in cache.
:param key: key for item
:param value: value for item
:return: corresponding value
:raises KeyError: if key is not found
"""
self.set(key, value, retry=True)
def _row_update(self, rowid, now, columns):
sql = self._sql
expire_time, tag, size, mode, filename, value = columns
sql('UPDATE Cache SET'
' store_time = ?,'
' expire_time = ?,'
' access_time = ?,'
' access_count = ?,'
' tag = ?,'
' size = ?,'
' mode = ?,'
' filename = ?,'
' value = ?'
' WHERE rowid = ?', (
now, # store_time
expire_time,
now, # access_time
0, # access_count
tag,
size,
mode,
filename,
value,
rowid,
),
)
def _row_insert(self, key, raw, now, columns):
sql = self._sql
expire_time, tag, size, mode, filename, value = columns
sql('INSERT INTO Cache('
' key, raw, store_time, expire_time, access_time,'
' access_count, tag, size, mode, filename, value'
') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', (
key,
raw,
now, # store_time
expire_time,
now, # access_time
0, # access_count
tag,
size,
mode,
filename,
value,
),
)
def _cull(self, now, sql, cleanup, limit=None):
cull_limit = self.cull_limit if limit is None else limit
if cull_limit == 0:
return
# Evict expired keys.
select_expired_template = (
'SELECT %s FROM Cache'
' WHERE expire_time IS NOT NULL AND expire_time < ?'
' ORDER BY expire_time LIMIT ?'
)
select_expired = select_expired_template % 'filename'
rows = sql(select_expired, (now, cull_limit)).fetchall()
if rows:
delete_expired = (
'DELETE FROM Cache WHERE rowid IN (%s)'
% (select_expired_template % 'rowid')
)
sql(delete_expired, (now, cull_limit))
for filename, in rows:
cleanup(filename)
cull_limit -= len(rows)
if cull_limit == 0:
return
# Evict keys by policy.
select_policy = EVICTION_POLICY[self.eviction_policy]['cull']
if select_policy is None or self.volume() < self.size_limit:
return
select_filename = select_policy.format(fields='filename', now=now)
rows = sql(select_filename, (cull_limit,)).fetchall()
if rows:
delete = (
'DELETE FROM Cache WHERE rowid IN (%s)'
% (select_policy.format(fields='rowid', now=now))
)
sql(delete, (cull_limit,))
for filename, in rows:
cleanup(filename)
def touch(self, key, expire=None, retry=False):
"""Touch `key` in cache and update `expire` time.
Raises :exc:`Timeout` error when database timeout occurs and `retry` is
`False` (default).
:param key: key for item
:param float expire: seconds until item expires
(default None, no expiry)
:param bool retry: retry if database timeout occurs (default False)
:return: True if key was touched
:raises Timeout: if database timeout occurs
"""
now = time.time()
db_key, raw = self._disk.put(key)
expire_time = None if expire is None else now + expire
with self._transact(retry) as (sql, _):
rows = sql(
'SELECT rowid, expire_time FROM Cache'
' WHERE key = ? AND raw = ?',
(db_key, raw),
).fetchall()
if rows:
(rowid, old_expire_time), = rows
if old_expire_time is None or old_expire_time > now:
sql('UPDATE Cache SET expire_time = ? WHERE rowid = ?',
(expire_time, rowid),
)
return True
return False
def add(self, key, value, expire=None, read=False, tag=None, retry=False):
"""Add `key` and `value` item to cache.
Similar to `set`, but only add to cache if key not present.