Cache¶
The on-disk cache of online answers. See Caching.
provesid.cache
¶
Advanced cache management for PROVESID package.
This module provides persistent, unlimited caching with size monitoring, import/export capabilities, and automatic cache management.
Entries live under
provesid.utils.user_cache_path --- a real
per-user cache directory, not the system temp directory --- so a cache written
today is still there tomorrow. Set PROVESID_CACHE_DIR to put it somewhere
else.
Three version numbers guard against serving an entry whose shape no longer
matches what the caller expects; see
CACHE_KEY_VERSION.
Attributes¶
CACHE_KEY_VERSION
module-attribute
¶
Key component bumped when something changes globally about what is stored or how keys are derived, making every existing entry unreachable at once. Two finer-grained versions sit beneath it, and the right one to bump is the narrowest that covers the change:
- a client's
CACHE_SCHEMA_VERSION, returned from its__cache_key__, retires the entries of that one client (seeprovesid.PubChemView.CACHE_SCHEMA_VERSION); @cached(version=N)retires the entries of one function.
A schema change is not something the cache can detect by itself: pickle
stores an instance's __dict__, so an entry written before a dataclass
gained a field restores as an object missing that attribute, and every
caller reading it raises on a machine where only the package version
changed. Bumping a version is what makes those entries unreachable instead.
CACHE_SERVICES
module-attribute
¶
The services with their own cache directory. This list is data: the
module-level cache functions all take service= rather than each service
owning a hand-written pair of functions.
Classes¶
CacheManager
¶
Advanced cache manager with persistent storage, size monitoring, and import/export.
Features:
- Unlimited cache by default
- Persistent storage across sessions, in a real cache directory rather than the system temp directory
- Size monitoring with warnings at 5GB
- Import/export functionality
- Cache statistics and management
Examples:
>>> import tempfile
>>> with tempfile.TemporaryDirectory() as tmp:
... manager = CacheManager(cache_dir=tmp)
... manager.set('demo', (2244,), {}, {'cid': 2244})
... manager.get('demo', (2244,), {})
(True, {'cid': 2244})
Source code in src/provesid/cache.py
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 | |
Methods:¶
__init__(cache_dir=None, service_name=None, max_size_gb=5.0, enable_warnings=True)
¶
Initialize the cache manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
Optional[str]
|
Directory to store cache files. When None, a per-user
cache directory from
|
None
|
service_name
|
Optional[str]
|
Name of the service for service-specific caching
(one of |
None
|
max_size_gb
|
float
|
Size threshold in GB for warnings (default: 5.0) |
5.0
|
enable_warnings
|
bool
|
Whether to enable size warnings (default: True) |
True
|
Source code in src/provesid/cache.py
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 | |
get(func_name, args, kwargs, version=1)
¶
Get a cached result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func_name
|
str
|
Fully qualified name of the cached function. |
required |
args
|
tuple
|
Positional arguments of the call. |
required |
kwargs
|
dict
|
Keyword arguments of the call. |
required |
version
|
int
|
Shape version of the function's return value; must match the one used when the entry was written, or it is a miss. |
1
|
Returns:
| Type | Description |
|---|---|
tuple
|
(found: bool, value: Any) - Tuple indicating if value was found and the value.
|
Examples:
>>> import tempfile
>>> manager = CacheManager(cache_dir=tempfile.mkdtemp())
>>> manager.get('demo', (2244,), {})
(False, None)
>>> manager.set('demo', (2244,), {}, None)
>>> manager.get('demo', (2244,), {})
(True, None)
>>> manager.get('demo', (2244,), {}, version=2) # another shape
(False, None)
Source code in src/provesid/cache.py
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 | |
set(func_name, args, kwargs, value, version=1)
¶
Store a result under the key for this call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func_name
|
str
|
Fully qualified name of the cached function. |
required |
args
|
tuple
|
Positional arguments of the call. |
required |
kwargs
|
dict
|
Keyword arguments of the call. |
required |
value
|
Any
|
The value to store. It must pickle. |
required |
version
|
int
|
Shape version of the function's return value. |
1
|
Note
A value that cannot be written to disk stays in memory for this process, with a warning.
Examples:
>>> import tempfile
>>> manager = CacheManager(cache_dir=tempfile.mkdtemp())
>>> manager.set('demo', ('aspirin',), {'exact': True}, [2244])
>>> manager.get('demo', ('aspirin',), {'exact': True})
(True, [2244])
>>> manager.get('demo', ('aspirin',), {'exact': False})
(False, None)
Source code in src/provesid/cache.py
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 | |
clear()
¶
Delete every entry of this cache, in memory and on disk.
Only this manager's directory is touched; other services' caches are kept.
Examples:
>>> import tempfile
>>> manager = CacheManager(cache_dir=tempfile.mkdtemp())
>>> manager.set('demo', (1,), {}, 'one')
>>> manager.clear()
>>> manager.get('demo', (1,), {})
(False, None)
Source code in src/provesid/cache.py
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 | |
get_cache_size()
¶
Get cache size information.
Returns:
| Type | Description |
|---|---|
Dict[str, Union[int, float]]
|
Dictionary with size information in bytes, MB, and GB: |
Examples:
>>> import tempfile
>>> manager = CacheManager(cache_dir=tempfile.mkdtemp())
>>> manager.set('demo', (1,), {}, 'one')
>>> size = manager.get_cache_size()
>>> size['files'], size['bytes'] > 0
(1, True)
Source code in src/provesid/cache.py
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 | |
get_cache_info()
¶
Get comprehensive cache information.
Returns:
| Type | Description |
|---|---|
dict
|
|
Examples:
>>> import tempfile
>>> manager = CacheManager(cache_dir=tempfile.mkdtemp())
>>> manager.set('demo', (1,), {}, 'one')
>>> info = manager.get_cache_info()
>>> info['memory_entries'], info['disk_entries'], info['warning_threshold_gb']
(1, 1, 5.0)
Source code in src/provesid/cache.py
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 | |
export_cache(export_path, format='pickle')
¶
Export cache data to a file.
Every entry on disk is exported, whichever process wrote it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
export_path
|
str
|
Path to export file |
required |
format
|
str
|
Export format ('pickle', 'json'). JSON writes values that
are not JSON types as their |
'pickle'
|
Returns:
| Type | Description |
|---|---|
bool
|
True if export successful, False otherwise (with a warning) |
Examples:
>>> import os, tempfile
>>> manager = CacheManager(cache_dir=tempfile.mkdtemp())
>>> manager.set('demo', (1,), {}, 'one')
>>> backup = os.path.join(tempfile.mkdtemp(), 'backup.pkl')
>>> manager.export_cache(backup)
True
Source code in src/provesid/cache.py
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 | |
import_cache(import_path, merge=True)
¶
Import cache data from a file.
Imported entries are written to disk and found by the next
get; the keys are kept as
exported, so an export imports into any cache whose functions and
versions match.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
import_path
|
str
|
Path to import file. A name ending |
required |
merge
|
bool
|
If True, merge with existing cache. If False, replace existing cache. |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if import successful, False otherwise (with a warning) |
Examples:
>>> import os, tempfile
>>> source = CacheManager(cache_dir=tempfile.mkdtemp())
>>> source.set('demo', (1,), {}, 'one')
>>> backup = os.path.join(tempfile.mkdtemp(), 'backup.pkl')
>>> source.export_cache(backup)
True
>>> target = CacheManager(cache_dir=tempfile.mkdtemp())
>>> target.import_cache(backup)
True
>>> target.get('demo', (1,), {})
(True, 'one')
Source code in src/provesid/cache.py
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 | |
Functions:¶
stable_key_part(obj)
¶
Reduce a function argument to a JSON-serialisable, process-stable form.
A cache key must come out identical for the same logical call in every
process and for every client instance, otherwise a persistent entry written
by one run is unreachable from the next. Python's default str() of an
object embeds its memory address --- <PubChemView object at 0x7f...> ---
so objects are reduced here to either the value they declare through
__cache_key__() or their fully qualified class name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Any positional or keyword argument of a cached call. For a bound
method this includes |
required |
Returns:
| Type | Description |
|---|---|
Any
|
A structure built only from strings, numbers, booleans, |
Note
Class-name reduction is right for a client instance, whose identity is
its configuration, but it would make two value objects of the same
class share a key. Any value-like object passed to a cached function
must therefore declare __cache_key__. No current caller passes one;
cached functions take identifiers, strings and sequences.
Examples:
>>> stable_key_part({'cid': 2244, 'props': ('MolecularWeight',)})
{'cid': 2244, 'props': ['MolecularWeight']}
Source code in src/provesid/cache.py
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 | |
is_failure_result(result)
¶
Report whether a return value is an in-band failure report.
Several PROVESID clients signal an error by returning a dict with
success: False and an error message rather than by raising. Storing
one of those would turn a transient network failure into a permanent
"no data" answer, so cached never writes a value
this function accepts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Any
|
The value returned by a cached function. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when |
Examples:
>>> is_failure_result({'success': False, 'error': 'Server busy'})
True
>>> is_failure_result({'success': True, 'CID': 2244})
False
Source code in src/provesid/cache.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
is_empty_result(result)
¶
Report whether a lookup returned nothing.
Absence is never cached. A service answers a genuinely empty lookup and a
momentary refusal the same way often enough that the two cannot be told
apart with confidence, and a wrongly cached "no data" is permanent whereas
re-fetching a genuinely empty result costs one cheap request. Pass this to
cached as skip_if for any method that
reports absence with an empty list, dict or DataFrame rather than by
raising.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Any
|
The value returned by a cached function. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when |
Examples:
>>> is_empty_result([])
True
>>> is_empty_result(None)
True
>>> is_empty_result(['3.47'])
False
Source code in src/provesid/cache.py
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 | |
get_service_cache(service=None)
¶
Return the cache manager for one service, building it on first use.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service
|
Optional[str]
|
One of |
None
|
Returns:
| Type | Description |
|---|---|
CacheManager
|
The shared |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> get_service_cache('pubchem') is get_service_cache('pubchem')
True
Source code in src/provesid/cache.py
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 | |
cached(func=None, *, service=None, skip_if=None, version=1)
¶
Decorator for unlimited caching with persistent storage.
This replaces the standard @lru_cache decorator with unlimited caching,
persistent storage, and size monitoring. Keys are process-stable, so an
entry written by one run is found by the next (see
stable_key_part).
Failed lookups are never written. An exception propagates uncached, and a
return value that is_failure_result
recognises as an in-band failure report is returned to the caller but not
stored --- a transient HTTP 429, 503 or timeout must not become a permanent
"no data" answer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
The function to cache, when used as a bare |
None
|
service
|
Optional[str]
|
Optional service name for service-specific caching; one of
|
None
|
skip_if
|
Optional[Callable[[Any], bool]]
|
Optional predicate applied to the return value; when it returns
True the value is handed back but not stored. Use it for functions
that report a failed lookup with something other than a
|
None
|
version
|
int
|
Shape version of this function's return value, part of every
key it writes. Bump it when the function starts returning a
different shape --- a new dataclass field, a dict key renamed,
a list of strings becoming a list of objects --- so that entries of
the old shape become unreachable instead of being unpickled into
something the caller cannot read. Bump the client's
|
1
|
Returns:
| Type | Description |
|---|---|
Callable
|
The decorated function, carrying |
Raises:
| Type | Description |
|---|---|
ValueError
|
At decoration time, if |
Note
use_cache=False --- either as a constructor flag on the client or as
a keyword on the call --- means "do not read the cache"; a successful
result is still written so later calls benefit.
Examples:
>>> calls = []
>>> @cached(service='pubchem', version=2)
... def fetch(cid):
... calls.append(cid)
... return {'success': True, 'cid': cid}
>>> fetch.cache_clear()
>>> fetch(2244)['cid'], fetch(2244)['cid'], calls
(2244, 2244, [2244])
Source code in src/provesid/cache.py
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 | |
clear_cache(service=None, all_services=False)
¶
Delete every cached entry for one service, or for the global cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service
|
Optional[str]
|
One of |
None
|
all_services
|
bool
|
When True, clear every service cache as well as the
global one, and ignore |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> clear_cache(service='pubchem')
Source code in src/provesid/cache.py
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 | |
get_cache_info(service=None)
¶
Report directory, entry counts and size for one cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service
|
Optional[str]
|
One of |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict with |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> sorted(get_cache_info(service='opsin'))[:2]
['cache_directory', 'disk_entries']
Source code in src/provesid/cache.py
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 | |
get_all_cache_info()
¶
Report get_cache_info for the global
cache and every service.
Returns:
| Type | Description |
|---|---|
Dict[str, Dict[str, Any]]
|
Dict keyed by |
Examples:
>>> 'pubchem' in get_all_cache_info()
True
Source code in src/provesid/cache.py
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 | |
export_cache(export_path, format='pickle', service=None)
¶
Write one cache's entries to a file so they can be shared or archived.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
export_path
|
str
|
Destination file. |
required |
format
|
str
|
|
'pickle'
|
service
|
Optional[str]
|
One of |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True on success, False if the export failed (a warning says why). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> export_cache('cache.pkl', service='pubchem')
True
Source code in src/provesid/cache.py
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 | |
import_cache(import_path, merge=True, service=None)
¶
Load entries exported by export_cache into
one cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
import_path
|
str
|
File written by |
required |
merge
|
bool
|
When True (default), keep existing entries; when False, clear the cache first. |
True
|
service
|
Optional[str]
|
One of |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True on success, False if the import failed (a warning says why). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Note
Entries carry the key they were written under, so an export made before a version bump imports cleanly and is simply never read.
Examples:
>>> import_cache('cache.pkl', service='pubchem')
True
Source code in src/provesid/cache.py
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 | |
get_cache_size(service=None)
¶
Measure one cache on disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service
|
Optional[str]
|
One of |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Union[int, float]]
|
Dict with |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> sorted(get_cache_size())
['bytes', 'files', 'gb', 'mb']
Source code in src/provesid/cache.py
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 | |
set_cache_warning_threshold(size_gb, service=None)
¶
Set the size at which a cache starts warning, in GB.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size_gb
|
float
|
New threshold. |
required |
service
|
Optional[str]
|
One of |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> set_cache_warning_threshold(10.0, service='pubchem')
Source code in src/provesid/cache.py
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 | |
enable_cache_warnings(enabled=True, service=None)
¶
Turn size warnings on or off for a cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enabled
|
bool
|
True to warn past the threshold, False to stay quiet. |
True
|
service
|
Optional[str]
|
One of |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> enable_cache_warnings(False, service='pubchem')
Source code in src/provesid/cache.py
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 | |