cached_file¶
Block-based file caching.
This module implements a cache holding remote - potentially huge - files in local folder trees, so that they can be read with a local-filesystem performance: files are split into blocks, fetched on demand, and identified by the hash of their metadata (URL, size, block size), so that blocks of equal content are shared across cache instances.
The cache is cleaned up periodically - removing the least-recently-used blocks exceeding a configurable maximum size - and offers a CacheInterface to be implemented by the remote file readers.
Meta
¶
Bases: Obj
Metadata of a cached file.
Attributes:
| Name | Type | Description |
|---|---|---|
url |
The URL of the file. |
|
size |
The size of the file. |
|
blocksize |
The size of the cache blocks. |
|
cid |
The content id - the hash identifying the cache entry. |
Source code in py_misc_utils/cached_file.py
41 42 43 44 45 46 47 48 49 | |
CachedBlockFile
¶
A block-cached file.
A CachedBlockFile holds the cache folder tree of a file - its META metadata file, its blocks/ folder holding the fetched file blocks and its links/ folder holding the hard links to the fetched data - and implements block-based random-access reads over it.
Reads are served from the fetched blocks - fetched via the reader (see CacheInterface) upon a cache miss - with each block fetched under a per-block lock file, so that concurrent readers do not fetch the same block twice.
Source code in py_misc_utils/cached_file.py
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 | |
default_meta()
classmethod
¶
Returns the default metadata of a cached file.
Source code in py_misc_utils/cached_file.py
88 89 90 91 92 | |
prepare_meta(meta, **kwargs)
classmethod
¶
Builds the complete metadata of a cached file.
The content id (cid) - a hash of the metadata tag - is computed and added to the metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
meta
|
An optional base metadata to start from. |
required | |
**kwargs
|
Metadata overrides. |
{}
|
Returns:
| Type | Description |
|---|---|
|
The complete metadata, including its content id. |
Source code in py_misc_utils/cached_file.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
remove(path)
classmethod
¶
Removes the cache folder tree of a cached file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
The cache folder path. |
required |
Returns:
| Type | Description |
|---|---|
|
True if the folder tree has been removed. |
Source code in py_misc_utils/cached_file.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
create(path, meta)
classmethod
¶
Creates the cache folder tree of a cached file - atomically.
The folder tree is built into a temporary path, and renamed to its final path upon success - so that readers never observe a partially built tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
The cache folder path. |
required | |
meta
|
The metadata of the cached file - saved into the META file. |
required |
Source code in py_misc_utils/cached_file.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
close()
¶
Closes the cached file - releasing its reader, if any.
Source code in py_misc_utils/cached_file.py
254 255 256 257 258 | |
cacheall()
¶
Fetches the whole file content into the cache.
Returns:
| Type | Description |
|---|---|
|
The local link path to the cached content, or None if the file |
|
|
could not be fetched. |
Source code in py_misc_utils/cached_file.py
260 261 262 263 264 265 266 267 268 269 | |
read_block(offset)
¶
Reads a block of data from the file.
On a cache miss the block is fetched via the reader - and the read is retried from the freshly-fetched block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
offset
|
The offset of the block within the file: it must be a multiple of the block size. |
required |
Returns:
| Type | Description |
|---|---|
|
The block data, or None if the block could not be read - e.g. if |
|
|
the offset is beyond the end of the file. |
Source code in py_misc_utils/cached_file.py
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 | |
size()
¶
Returns the size of the file, fetching it from the reader - and caching it into the metadata - if not already known.
Returns:
| Type | Description |
|---|---|
|
The file size, in bytes. |
Source code in py_misc_utils/cached_file.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
locked()
¶
Returns the lock file serializing the access to the cache folder tree of the file.
Source code in py_misc_utils/cached_file.py
316 317 318 319 | |
local_link()
¶
Returns the local link path of the file - the path a cached whole-file content can be accessed from, if fetched.
Source code in py_misc_utils/cached_file.py
321 322 323 324 | |
blocks_dir(path)
classmethod
¶
Returns the folder path holding the blocks of a cached file.
Source code in py_misc_utils/cached_file.py
326 327 328 329 | |
links_dir(path)
classmethod
¶
Returns the folder path holding the links of a cached file.
Source code in py_misc_utils/cached_file.py
331 332 333 334 | |
fblock_path(path, cid, offset)
classmethod
¶
Returns the block file path of a content id and offset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
The cache folder path. |
required | |
cid
|
The content id of the cached file. |
required | |
offset
|
The block offset within the file, or WHOLE_OFFSET for whole-file caching. |
required |
Returns:
| Type | Description |
|---|---|
|
The block file path - blocks/block- |
|
|
offset part dropped for whole-file caching. |
Source code in py_misc_utils/cached_file.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
parse_block_file(fname)
classmethod
¶
Parses a block file name into its (content id, offset) parts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fname
|
The block file name. |
required |
Returns:
| Type | Description |
|---|---|
|
A (cid, offset) tuple, or None if the name does not match the |
|
|
block file name format. |
Source code in py_misc_utils/cached_file.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | |
flink_path(path, cid, url)
classmethod
¶
Returns the link file path of a cached content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
The cache folder path. |
required | |
cid
|
The content id of the cached file. |
required | |
url
|
The URL of the cached file, whose base name names the link. |
required |
Returns:
| Type | Description |
|---|---|
|
The link file path - links/ |
Source code in py_misc_utils/cached_file.py
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | |
purge_blocks(path, max_age=None)
classmethod
¶
Purges the too-old blocks of a cached file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
The cache folder path. |
required | |
max_age
|
The maximum age - in seconds - of the blocks, dropping the older ones. If None, all the blocks are kept. |
None
|
Source code in py_misc_utils/cached_file.py
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 | |
fmeta_path(path)
classmethod
¶
Returns the metadata file path of a cached file.
Source code in py_misc_utils/cached_file.py
429 430 431 432 | |
save_meta(path, meta)
classmethod
¶
Saves the metadata of a cached file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
The cache folder path. |
required | |
meta
|
The metadata to save. |
required |
Source code in py_misc_utils/cached_file.py
434 435 436 437 438 439 440 441 442 443 444 | |
load_meta(path)
classmethod
¶
Loads the metadata of a cached file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
The cache folder path. |
required |
Returns:
| Type | Description |
|---|---|
|
The metadata. |
Source code in py_misc_utils/cached_file.py
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 | |
validate(path)
classmethod
¶
Validates the cache folder tree of a cached file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
The cache folder path. |
required |
Returns:
| Type | Description |
|---|---|
|
True if the folder tree holds a consistent, complete cache entry. |
Source code in py_misc_utils/cached_file.py
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 | |
CachedFile
¶
A file-like object reading a block-cached file.
A CachedFile implements the standard read/seek/tell file interface over a CachedBlockFile, serving the reads from the cached blocks - and holding the current block in memory.
Source code in py_misc_utils/cached_file.py
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 | |
closed
property
¶
Whether the file is closed.
close()
¶
Closes the file, releasing the underlying cached file.
Source code in py_misc_utils/cached_file.py
502 503 504 505 506 | |
seek(pos, whence=os.SEEK_SET)
¶
Moves the file position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pos
|
The position offset - relative to whence. |
required | |
whence
|
The seek mode: os.SEEK_SET (absolute), os.SEEK_CUR (relative to the current position) or os.SEEK_END (relative to the end of the file). |
SEEK_SET
|
Returns:
| Type | Description |
|---|---|
|
The new absolute position. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the whence mode is invalid. |
AssertionError
|
If the new position falls outside the file. |
Source code in py_misc_utils/cached_file.py
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 | |
tell()
¶
Returns the current file position.
Source code in py_misc_utils/cached_file.py
546 547 548 549 | |
read(size=-1)
¶
Reads data from the current file position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
The number of bytes to read - all the remaining ones if -1. |
-1
|
Returns:
| Type | Description |
|---|---|
|
The read data. |
Source code in py_misc_utils/cached_file.py
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 | |
read1(size=-1)
¶
Reads data without crossing the current block boundary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
The number of bytes to read - all the remaining ones of the current block if -1. |
-1
|
Returns:
| Type | Description |
|---|---|
|
The read data. |
Source code in py_misc_utils/cached_file.py
591 592 593 594 595 596 597 598 599 600 601 | |
peek(size=0)
¶
Reads data without moving the file position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
The number of bytes to peek. |
0
|
Returns:
| Type | Description |
|---|---|
|
The peeked data. |
Source code in py_misc_utils/cached_file.py
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 | |
readline(size=-1)
¶
Reads a line from the current file position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
The maximum number of bytes to read - unbounded if -1. |
-1
|
Returns:
| Type | Description |
|---|---|
|
The line data - terminated by the newline, if found. |
Source code in py_misc_utils/cached_file.py
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 | |
flush()
¶
No-op: the file is read-only.
Source code in py_misc_utils/cached_file.py
649 650 | |
readable()
¶
Whether the file is readable.
Source code in py_misc_utils/cached_file.py
652 653 654 655 | |
seekable()
¶
Whether the file is seekable.
Source code in py_misc_utils/cached_file.py
657 658 659 660 | |
writable()
¶
Whether the file is writable: always False.
Source code in py_misc_utils/cached_file.py
662 663 664 665 | |
CacheInterface
¶
The interface of the remote file readers feeding the block cache.
The reader of a cached file must implement the methods of this interface: reading the file size, the blocks of data - or the whole file content for readers not supporting block-based reads - and the bookkeeping of the per-reader resources.
Example
import os
import tempfile
from py_misc_utils import cached_file as chf
# A reader serving an in-memory content through the interface
# contract - like the readers of the remote filesystem backends.
class MemReader:
def __init__(self, data):
self.data = data
def tag(self):
return 'mem'
def support_blocks(self):
return True
def read_block(self, bpath, offset, size):
content = (self.data if offset == chf.CachedBlockFile.WHOLE_OFFSET
else self.data[offset:offset + size])
with open(bpath, 'wb') as fd:
fd.write(content)
return len(content)
iface = chf.CacheInterface(tempfile.mkdtemp())
url = 'mem://host/data.bin'
with iface.open(url, chf.Meta(url=url, tag='mem'), MemReader(b'hello')) as cf:
print(cf.read())
Source code in py_misc_utils/cached_file.py
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 | |
open(url, meta, reader, **kwargs)
¶
Opens a cached file, creating its cache entry - if missing.
If the cache entry exists and validates, its metadata is reused; otherwise the entry is created from the given metadata - completed with the reader-provided size, if missing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
The URL of the file to open. |
required | |
meta
|
The base metadata of the file. |
required | |
reader
|
The CacheInterface reader fetching the file data. |
required | |
**kwargs
|
Metadata overrides, plus 'uncached' - a flag materializing the file content into a temporary folder, cleaned up upon close. |
{}
|
Returns:
| Type | Description |
|---|---|
|
The open CachedFile. |
Source code in py_misc_utils/cached_file.py
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 | |
as_local(url, meta, reader, **kwargs)
¶
Materializes a file into a local path.
The file is fetched - whole - into the cache, and its local link path is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
The URL of the file to materialize. |
required | |
meta
|
The base metadata of the file. |
required | |
reader
|
The CacheInterface reader fetching the file data. |
required | |
**kwargs
|
Metadata overrides. |
{}
|
Returns:
| Type | Description |
|---|---|
|
The local path holding the file content. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the file content could not be fetched. |
Source code in py_misc_utils/cached_file.py
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 | |
cleanup_cache(cache_dir, max_age=None, max_size=None)
¶
Cleans up a file cache folder.
Purges the too-old blocks of the cached files, then drops the whole cache entries - least-recently-used first - until the total cache size fits the maximum size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
The cache folder path. |
required | |
max_age
|
An optional maximum age - in seconds - of the cached blocks, dropping the older ones. Defaults to the period the cache is not accessed for (see get_cache_dir()). |
None
|
|
max_size
|
An optional maximum size of the cache. Defaults to the GFS_CACHE_MAXSIZE environment variable value - 16GB by default. |
None
|
Source code in py_misc_utils/cached_file.py
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 | |
make_tag(**kwargs)
¶
Builds the metadata tag hashed into a cache entry content id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
The metadata values making up the tag. |
{}
|
Returns:
| Type | Description |
|---|---|
|
The serialized tag. |
Source code in py_misc_utils/cached_file.py
869 870 871 872 873 874 875 876 877 878 879 880 | |
get_cache_dir(path)
¶
Returns the cache folder of a base path, triggering its cleanup.
The cache folder is cleaned up if the last cleanup is older than the cleanup period - tracked via a marker file. The folder is not created when missing: it is created lazily by the cache machinery, so the returned path may not exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
The base path holding the cache folder. |
required |
Returns:
| Type | Description |
|---|---|
|
The cache folder path. |
Source code in py_misc_utils/cached_file.py
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 | |