Spaces:
Sleeping
Sleeping
File size: 22,725 Bytes
ea0280d |
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 |
"""Fanout cache automatically shards keys and values."""
import contextlib as cl
import functools
import itertools as it
import operator
import os.path as op
import sqlite3
import tempfile
import time
from .core import DEFAULT_SETTINGS, ENOVAL, Cache, Disk, Timeout
from .persistent import Deque, Index
class FanoutCache:
"""Cache that shards keys and values."""
def __init__(
self, directory=None, shards=8, timeout=0.010, disk=Disk, **settings
):
"""Initialize cache instance.
:param str directory: cache directory
:param int shards: number of shards to distribute writes
:param float timeout: SQLite connection timeout
:param disk: `Disk` instance for serialization
:param settings: any of `DEFAULT_SETTINGS`
"""
if directory is None:
directory = tempfile.mkdtemp(prefix='diskcache-')
directory = str(directory)
directory = op.expanduser(directory)
directory = op.expandvars(directory)
default_size_limit = DEFAULT_SETTINGS['size_limit']
size_limit = settings.pop('size_limit', default_size_limit) / shards
self._count = shards
self._directory = directory
self._disk = disk
self._shards = tuple(
Cache(
directory=op.join(directory, '%03d' % num),
timeout=timeout,
disk=disk,
size_limit=size_limit,
**settings,
)
for num in range(shards)
)
self._hash = self._shards[0].disk.hash
self._caches = {}
self._deques = {}
self._indexes = {}
@property
def directory(self):
"""Cache directory."""
return self._directory
def __getattr__(self, name):
safe_names = {'timeout', 'disk'}
valid_name = name in DEFAULT_SETTINGS or name in safe_names
assert valid_name, 'cannot access {} in cache shard'.format(name)
return getattr(self._shards[0], name)
@cl.contextmanager
def transact(self, retry=True):
"""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.
Blocks until transactions are held on all cache shards by retrying as
necessary.
>>> cache = FanoutCache()
>>> 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
:return: context manager for use in `with` statement
"""
assert retry, 'retry must be True in FanoutCache'
with cl.ExitStack() as stack:
for shard in self._shards:
shard_transaction = shard.transact(retry=True)
stack.enter_context(shard_transaction)
yield
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.
If database timeout occurs then fails silently unless `retry` is set to
`True` (default `False`).
:param key: key for item
:param value: value for item
:param float expire: seconds until the key expires
(default None, no expiry)
:param bool read: read value as raw 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
"""
index = self._hash(key) % self._count
shard = self._shards[index]
try:
return shard.set(key, value, expire, read, tag, retry)
except Timeout:
return False
def __setitem__(self, key, value):
"""Set `key` and `value` item in cache.
Calls :func:`FanoutCache.set` internally with `retry` set to `True`.
:param key: key for item
:param value: value for item
"""
index = self._hash(key) % self._count
shard = self._shards[index]
shard[key] = value
def touch(self, key, expire=None, retry=False):
"""Touch `key` in cache and update `expire` time.
If database timeout occurs then fails silently unless `retry` is set to
`True` (default `False`).
:param key: key for item
:param float expire: seconds until the key expires
(default None, no expiry)
:param bool retry: retry if database timeout occurs (default False)
:return: True if key was touched
"""
index = self._hash(key) % self._count
shard = self._shards[index]
try:
return shard.touch(key, expire, retry)
except Timeout:
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.
This operation is atomic. Only one concurrent add operation for given
key from separate threads or processes will succeed.
When `read` is `True`, `value` should be a file-like object opened
for reading in binary mode.
If database timeout occurs then fails silently unless `retry` is set to
`True` (default `False`).
:param key: key for item
:param value: value for item
:param float expire: seconds until the key 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 added
"""
index = self._hash(key) % self._count
shard = self._shards[index]
try:
return shard.add(key, value, expire, read, tag, retry)
except Timeout:
return False
def incr(self, key, delta=1, default=0, retry=False):
"""Increment value by delta for item with key.
If key is missing and default is None then raise KeyError. Else if key
is missing and default is not None then use default for value.
Operation is atomic. All concurrent increment operations will be
counted individually.
Assumes value may be stored in a SQLite column. Most builds that target
machines with 64-bit pointer widths will support 64-bit signed
integers.
If database timeout occurs then fails silently unless `retry` is set to
`True` (default `False`).
:param key: key for item
:param int delta: amount to increment (default 1)
:param int default: value if key is missing (default 0)
:param bool retry: retry if database timeout occurs (default False)
:return: new value for item on success else None
:raises KeyError: if key is not found and default is None
"""
index = self._hash(key) % self._count
shard = self._shards[index]
try:
return shard.incr(key, delta, default, retry)
except Timeout:
return None
def decr(self, key, delta=1, default=0, retry=False):
"""Decrement value by delta for item with key.
If key is missing and default is None then raise KeyError. Else if key
is missing and default is not None then use default for value.
Operation is atomic. All concurrent decrement operations will be
counted individually.
Unlike Memcached, negative values are supported. Value may be
decremented below zero.
Assumes value may be stored in a SQLite column. Most builds that target
machines with 64-bit pointer widths will support 64-bit signed
integers.
If database timeout occurs then fails silently unless `retry` is set to
`True` (default `False`).
:param key: key for item
:param int delta: amount to decrement (default 1)
:param int default: value if key is missing (default 0)
:param bool retry: retry if database timeout occurs (default False)
:return: new value for item on success else None
:raises KeyError: if key is not found and default is None
"""
index = self._hash(key) % self._count
shard = self._shards[index]
try:
return shard.decr(key, delta, default, retry)
except Timeout:
return None
def get(
self,
key,
default=None,
read=False,
expire_time=False,
tag=False,
retry=False,
):
"""Retrieve value from cache. If `key` is missing, return `default`.
If database timeout occurs then returns `default` unless `retry` is set
to `True` (default `False`).
:param key: key for item
:param default: return value if key is missing (default None)
:param bool read: if True, return file handle to value
(default False)
:param float expire_time: if True, return expire_time in tuple
(default False)
:param tag: if True, return tag in tuple (default False)
:param bool retry: retry if database timeout occurs (default False)
:return: value for item if key is found else default
"""
index = self._hash(key) % self._count
shard = self._shards[index]
try:
return shard.get(key, default, read, expire_time, tag, retry)
except (Timeout, sqlite3.OperationalError):
return default
def __getitem__(self, key):
"""Return corresponding value for `key` from cache.
Calls :func:`FanoutCache.get` internally with `retry` set to `True`.
:param key: key for item
:return: value for item
:raises KeyError: if key is not found
"""
index = self._hash(key) % self._count
shard = self._shards[index]
return shard[key]
def read(self, key):
"""Return file handle corresponding to `key` from cache.
:param key: key for item
:return: file open for reading in binary mode
:raises KeyError: if key is not found
"""
handle = self.get(key, default=ENOVAL, read=True, retry=True)
if handle is ENOVAL:
raise KeyError(key)
return handle
def __contains__(self, key):
"""Return `True` if `key` matching item is found in cache.
:param key: key for item
:return: True if key is found
"""
index = self._hash(key) % self._count
shard = self._shards[index]
return key in shard
def pop(
self, key, default=None, expire_time=False, tag=False, retry=False
): # noqa: E501
"""Remove corresponding item for `key` from cache and return value.
If `key` is missing, return `default`.
Operation is atomic. Concurrent operations will be serialized.
If database timeout occurs then fails silently unless `retry` is set to
`True` (default `False`).
:param key: key for item
:param default: return value if key is missing (default None)
:param float expire_time: if True, return expire_time in tuple
(default False)
:param tag: if True, return tag in tuple (default False)
:param bool retry: retry if database timeout occurs (default False)
:return: value for item if key is found else default
"""
index = self._hash(key) % self._count
shard = self._shards[index]
try:
return shard.pop(key, default, expire_time, tag, retry)
except Timeout:
return default
def delete(self, key, retry=False):
"""Delete corresponding item for `key` from cache.
Missing keys are ignored.
If database timeout occurs then fails silently unless `retry` is set to
`True` (default `False`).
:param key: key for item
:param bool retry: retry if database timeout occurs (default False)
:return: True if item was deleted
"""
index = self._hash(key) % self._count
shard = self._shards[index]
try:
return shard.delete(key, retry)
except Timeout:
return False
def __delitem__(self, key):
"""Delete corresponding item for `key` from cache.
Calls :func:`FanoutCache.delete` internally with `retry` set to `True`.
:param key: key for item
:raises KeyError: if key is not found
"""
index = self._hash(key) % self._count
shard = self._shards[index]
del shard[key]
def check(self, fix=False, retry=False):
"""Check database and file system consistency.
Intended for use in testing and post-mortem error analysis.
While checking the cache table for consistency, a writer lock is held
on the database. The lock blocks other cache clients from writing to
the database. For caches with many file references, the lock may be
held for a long time. For example, local benchmarking shows that a
cache with 1,000 file references takes ~60ms to check.
If database timeout occurs then fails silently unless `retry` is set to
`True` (default `False`).
:param bool fix: correct inconsistencies
:param bool retry: retry if database timeout occurs (default False)
:return: list of warnings
:raises Timeout: if database timeout occurs
"""
warnings = (shard.check(fix, retry) for shard in self._shards)
return functools.reduce(operator.iadd, warnings, [])
def expire(self, retry=False):
"""Remove expired items from cache.
If database timeout occurs then fails silently unless `retry` is set to
`True` (default `False`).
:param bool retry: retry if database timeout occurs (default False)
:return: count of items removed
"""
return self._remove('expire', args=(time.time(),), retry=retry)
def create_tag_index(self):
"""Create tag index on cache database.
Better to initialize cache with `tag_index=True` than use this.
:raises Timeout: if database timeout occurs
"""
for shard in self._shards:
shard.create_tag_index()
def drop_tag_index(self):
"""Drop tag index on cache database.
:raises Timeout: if database timeout occurs
"""
for shard in self._shards:
shard.drop_tag_index()
def evict(self, tag, retry=False):
"""Remove items with matching `tag` from cache.
If database timeout occurs then fails silently unless `retry` is set to
`True` (default `False`).
:param str tag: tag identifying items
:param bool retry: retry if database timeout occurs (default False)
:return: count of items removed
"""
return self._remove('evict', args=(tag,), retry=retry)
def cull(self, retry=False):
"""Cull items from cache until volume is less than size limit.
If database timeout occurs then fails silently unless `retry` is set to
`True` (default `False`).
:param bool retry: retry if database timeout occurs (default False)
:return: count of items removed
"""
return self._remove('cull', retry=retry)
def clear(self, retry=False):
"""Remove all items from cache.
If database timeout occurs then fails silently unless `retry` is set to
`True` (default `False`).
:param bool retry: retry if database timeout occurs (default False)
:return: count of items removed
"""
return self._remove('clear', retry=retry)
def _remove(self, name, args=(), retry=False):
total = 0
for shard in self._shards:
method = getattr(shard, name)
while True:
try:
count = method(*args, retry=retry)
total += count
except Timeout as timeout:
total += timeout.args[0]
else:
break
return total
def stats(self, enable=True, reset=False):
"""Return cache statistics hits and misses.
:param bool enable: enable collecting statistics (default True)
:param bool reset: reset hits and misses to 0 (default False)
:return: (hits, misses)
"""
results = [shard.stats(enable, reset) for shard in self._shards]
total_hits = sum(hits for hits, _ in results)
total_misses = sum(misses for _, misses in results)
return total_hits, total_misses
def volume(self):
"""Return estimated total size of cache on disk.
:return: size in bytes
"""
return sum(shard.volume() for shard in self._shards)
def close(self):
"""Close database connection."""
for shard in self._shards:
shard.close()
self._caches.clear()
self._deques.clear()
self._indexes.clear()
def __enter__(self):
return self
def __exit__(self, *exception):
self.close()
def __getstate__(self):
return (self._directory, self._count, self.timeout, type(self.disk))
def __setstate__(self, state):
self.__init__(*state)
def __iter__(self):
"""Iterate keys in cache including expired items."""
iterators = (iter(shard) for shard in self._shards)
return it.chain.from_iterable(iterators)
def __reversed__(self):
"""Reverse iterate keys in cache including expired items."""
iterators = (reversed(shard) for shard in reversed(self._shards))
return it.chain.from_iterable(iterators)
def __len__(self):
"""Count of items in cache including expired items."""
return sum(len(shard) for shard in self._shards)
def reset(self, key, value=ENOVAL):
"""Reset `key` and `value` item from Settings table.
If `value` is not given, it is reloaded from the Settings
table. Otherwise, the Settings table is updated.
Settings attributes on cache objects are lazy-loaded and
read-only. Use `reset` to update the value.
Settings with the ``sqlite_`` prefix correspond to SQLite
pragmas. Updating the value will execute the corresponding PRAGMA
statement.
:param str key: Settings key for item
:param value: value for item (optional)
:return: updated value for item
"""
for shard in self._shards:
while True:
try:
result = shard.reset(key, value)
except Timeout:
pass
else:
break
return result
def cache(self, name, timeout=60, disk=None, **settings):
"""Return Cache with given `name` in subdirectory.
If disk is none (default), uses the fanout cache disk.
>>> fanout_cache = FanoutCache()
>>> cache = fanout_cache.cache('test')
>>> cache.set('abc', 123)
True
>>> cache.get('abc')
123
>>> len(cache)
1
>>> cache.delete('abc')
True
:param str name: subdirectory name for Cache
:param float timeout: SQLite connection timeout
:param disk: Disk type or subclass for serialization
:param settings: any of DEFAULT_SETTINGS
:return: Cache with given name
"""
_caches = self._caches
try:
return _caches[name]
except KeyError:
parts = name.split('/')
directory = op.join(self._directory, 'cache', *parts)
temp = Cache(
directory=directory,
timeout=timeout,
disk=self._disk if disk is None else Disk,
**settings,
)
_caches[name] = temp
return temp
def deque(self, name, maxlen=None):
"""Return Deque with given `name` in subdirectory.
>>> cache = FanoutCache()
>>> deque = cache.deque('test')
>>> deque.extend('abc')
>>> deque.popleft()
'a'
>>> deque.pop()
'c'
>>> len(deque)
1
:param str name: subdirectory name for Deque
:param maxlen: max length (default None, no max)
:return: Deque with given name
"""
_deques = self._deques
try:
return _deques[name]
except KeyError:
parts = name.split('/')
directory = op.join(self._directory, 'deque', *parts)
cache = Cache(
directory=directory,
disk=self._disk,
eviction_policy='none',
)
deque = Deque.fromcache(cache, maxlen=maxlen)
_deques[name] = deque
return deque
def index(self, name):
"""Return Index with given `name` in subdirectory.
>>> cache = FanoutCache()
>>> index = cache.index('test')
>>> index['abc'] = 123
>>> index['def'] = 456
>>> index['ghi'] = 789
>>> index.popitem()
('ghi', 789)
>>> del index['abc']
>>> len(index)
1
>>> index['def']
456
:param str name: subdirectory name for Index
:return: Index with given name
"""
_indexes = self._indexes
try:
return _indexes[name]
except KeyError:
parts = name.split('/')
directory = op.join(self._directory, 'index', *parts)
cache = Cache(
directory=directory,
disk=self._disk,
eviction_policy='none',
)
index = Index.fromcache(cache)
_indexes[name] = index
return index
FanoutCache.memoize = Cache.memoize # type: ignore
|