Memoize
memoize
memoize(
cache: CacheProtocol,
name: str | None = None,
*,
typed: bool = False,
expire: float | None = None,
tags: str | Iterable[str] | None = None,
include: Iterable[str | int] = (),
exclude: Iterable[str | int] = ()
) -> MemoizedDecorator
Memoizing cache decorator.
Decorator to wrap callable with memoizing function using cache. Repeated calls with the same arguments will lookup result in cache and avoid function evaluation.
If name is set to None (default), the callable name will be determined automatically.
When expire is set to zero, function results will not be set in the cache. Cache lookups still occur, however.
If typed is set to True, function arguments of different types will be cached separately. For example, f(3) and f(3.0) will be treated as distinct calls with distinct results.
The original underlying function is accessible through the __wrapped__
attribute. This is useful for introspection, for bypassing the cache,
or for rewrapping the function with a different cache.
An additional cache_key method can be used to generate the
cache key used for the given arguments.
Remember to call memoize when decorating a callable. If you forget, then a TypeError will occur. Note the lack of parenthenses after memoize below:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
CacheProtocol
|
cache to store callable arguments and return values |
required |
|
str | None
|
name given for callable (default None, automatic) |
None
|
|
bool
|
cache different types separately (default False) |
False
|
|
float | None
|
seconds until arguments expire (default None, no expiry) |
None
|
|
str | Iterable[str] | None
|
text to associate with arguments (default None) |
None
|
|
Iterable[str | int]
|
positional or keyword args to include (default ()) |
()
|
|
Iterable[str | int]
|
positional or keyword args to exclude (default ()) |
()
|
Returns:
| Type | Description |
|---|---|
MemoizedDecorator
|
memoized callable decorator |
Source code in src/typed_diskcache/utils/memo.py
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 | |
memoize_stampede
memoize_stampede(
cache: CacheProtocol,
name: str | None = None,
*,
typed: bool = False,
expire: float | None = None,
tags: str | Iterable[str] | None = None,
include: Iterable[str | int] = (),
exclude: Iterable[str | int] = (),
beta: float = 1
) -> MemoizedStampedeDecorator
Memoizing cache decorator with cache stampede protection.
Cache stampedes are a type of system overload that can occur when parallel computing systems using memoization come under heavy load. This behaviour is sometimes also called dog-piling, cache miss storm, cache choking, or the thundering herd problem.
The memoization decorator implements cache stampede protection through early recomputation. Early recomputation of function results will occur probabilistically before expiration in a background thread of execution. Early probabilistic recomputation is based on research by Vattani, A.; Chierichetti, F.; Lowenstein, K. (2015), Optimal Probabilistic Cache Stampede Prevention, VLDB, pp. 886-897, ISSN 2150-8097
If name is set to None (default), the callable name will be determined automatically.
If typed is set to True, function arguments of different types will be cached separately. For example, f(3) and f(3.0) will be treated as distinct calls with distinct results.
The original underlying function is accessible through the __wrapped__
attribute. This is useful for introspection, for bypassing the cache, or
for rewrapping the function with a different cache.
An additional cache_key method can be used to generate the cache
key used for the given arguments.
Remember to call memoize when decorating a callable. If you forget, then a TypeError will occur.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
CacheProtocol
|
cache to store callable arguments and return values |
required |
|
str | None
|
name given for callable (default None, automatic) |
None
|
|
bool
|
cache different types separately (default False) |
False
|
|
float | None
|
seconds until arguments expire |
None
|
|
str | Iterable[str] | None
|
text to associate with arguments (default None) |
None
|
|
Iterable[str | int]
|
positional or keyword args to include (default ()) |
()
|
|
Iterable[str | int]
|
positional or keyword args to exclude (default ()) |
()
|
|
float
|
cache stampede protection factor (default 1) |
1
|
Returns:
| Type | Description |
|---|---|
MemoizedStampedeDecorator
|
memoized callable decorator |
Source code in src/typed_diskcache/utils/memo.py
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 | |
MemoizedDecorator
MemoizedStampedeDecorator
AsyncMemoized
AsyncMemoized(
*,
cache: CacheProtocol,
func: Callable[_P, Coroutine[Any, Any, _T]],
base: str,
typed: bool,
expire: float | None,
tags: frozenset[str],
include: frozenset[str | int],
exclude: frozenset[str | int]
)
Bases: Memoized[_P, Coroutine[Any, Any, _T]], Generic[_P, _T]
Source code in src/typed_diskcache/utils/memo.py
__call__
async
Source code in src/typed_diskcache/utils/memo.py
cache_key
Memoized
Memoized(
*,
cache: CacheProtocol,
func: Callable[_P, _T],
base: str,
typed: bool,
expire: float | None,
tags: frozenset[str],
include: frozenset[str | int],
exclude: frozenset[str | int]
)
Bases: Generic[_P, _T]