Registry / database / memoization

memoization

JSON →
library0.4.0pypypi✓ verified 24d ago

Memoization is a powerful caching library for Python, providing decorators for function memoization with features like Time-To-Live (TTL) expiration, multiple caching algorithms (LRU, LFU, FIFO), and extensibility. It aims to solve some limitations of the standard library's `functools.lru_cache`, such as handling unhashable arguments. The library is actively developed, with its latest release being v0.4.0.

pip install memoization
INSTALL
IMPORT
SIG · MEMOIZATION
M
memoization
databasepythonv0.4.0
Install
2.6s avg
Import
36ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.4.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.036s · 19.7MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.6s · import 0.036s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

cached
from memoization import cached

Demonstrates basic memoization with TTL and a quick example of an LRU cache. The `cached` decorator automatically caches the function's return values, avoiding re-computation for the same inputs within the specified TTL or cache size.

import time from memoization import cached @cached(ttl=2) # Cache results for 2 seconds def expensive_calculation(a, b): print(f"Calculating {a} + {b}...") time.sleep(1) # Simulate a slow operation return a + b print(expensive_calculation(1, 2)) # First call, calculates print(expensive_calculation(1, 2)) # Second call, uses cache time.sleep(3) # Wait for cache to expire print(expensive_calculation(1, 2)) # Cache expired, recalculates @cached(max_size=2, algorithm='lru') def lru_example(x): print(f"Calculating for {x}...") return x * x lru_example(1) lru_example(2) lru_example(3) # 1 will be evicted lru_example(1) # Re-calculates for 1, 2 is evicted
Debug
Known issues
breakingPython 2 support was entirely removed in version `0.2.2`. Additionally, support for Python 3.2 and 3.3 was dropped in `v0.1.4`. Users on these older Python versions must upgrade their Python environment to use `memoization` versions >= `0.2.2` (for Python 2) or >= `0.1.4` (for Python 3.2/3.3).
fix
Upgrade your Python environment to Python 3.4 or newer.
affects: <0.2.2 (for Python 2), <0.1.4 (for Python 3.2/3.3)
breakingThe API for on-demand partial cache clearing underwent significant changes. This functionality was initially present, then explicitly dropped in `v0.1.4`, and later reintroduced with new APIs in `v0.4.0`. Code relying on specific partial cache clearing methods in versions between `v0.1.4` and `v0.4.0` would have failed, and existing code for clearing would need updates for `v0.4.0`.
fix
For versions `v0.4.0` and above, utilize the new cache manipulation APIs like `f.cache_clear(args=...)` for partial clearing, `f.cache_pop(args=...)`, or `f.cache_delete_if(condition_func)`.
affects: 0.1.4 - <0.4.0
gotchaWhen using custom `key_maker` functions (introduced in `v0.3.1`), ensure they produce unique, hashable, and efficiently computable keys. A bug in versions prior to `v0.4.0` (`#8`) incorrectly required the key maker's signature to exactly match the cached function's signature. This strict requirement was relaxed in `v0.4.0`.
fix
In `v0.4.0`+, the `key_maker` no longer strictly needs the same signature. Always ensure your `key_maker` function deterministically produces unique and hashable keys that accurately represent the inputs for caching logic. Refer to the official documentation for best practices on custom key generation.
affects: <0.4.0 (for key_maker signature match), all versions (for key_maker correctness)
gotchaFor unhashable arguments (e.g., `list`, `dict`), `memoization` defaults to using `str()` to generate cache keys. While this feature allows caching functions with unhashable inputs (unlike `functools.lru_cache`), it can lead to unexpected cache behavior if the `str()` representation of mutable objects does not uniquely reflect their logical state or if different objects happen to have the same string representation. For complex or mutable unhashable arguments, a custom `key_maker` is often recommended.
fix
If the default `str()`-based key generation for unhashable arguments is not sufficient, provide a custom `key_maker` function that creates a robust, hashable key based on the relevant attributes or a deep hash of the unhashable inputs.
affects: All versions
gotchaThread safety was introduced in `v0.1.4`. Using `memoization` in multi-threaded applications with versions prior to `v0.1.4` could lead to race conditions, inconsistent cache states, or incorrect results due to concurrent access to the cache without proper locking. While `memoization` versions `v0.1.4` and later generally handle thread safety, be mindful of the performance implications and ensure it's enabled if required (`thread_safe=True` parameter).
fix
Upgrade to `memoization >= 0.1.4` and ensure the `thread_safe` parameter (if explicitly setting it) is configured appropriately for your application's concurrency model.
affects: <0.1.4
Errors
Common errors & fixes
TypeError: unhashable type: 'list'
This error typically occurs when attempting to use a mutable object (like a list, dictionary, or set) as a cache key in a hashing-based cache. While the `memoization` library can fall back to `str()` conversion for unhashable arguments, this default behavior might not always work correctly, especially for custom objects, and the error can still arise if the underlying caching mechanism or a custom key generation fails to produce a hashable key.
fix
For arguments that are unhashable, use `memoization.cached` with a custom `key_maker` function. This function should transform the unhashable arguments into a consistent, hashable representation (e.g., a tuple of `frozenset`s or a JSON string) that uniquely identifies the function call. For example, if a list is a simple sequence, convert it to a tuple: `@cached(key_maker=lambda *args, **kwargs: (tuple(args[0]) if isinstance(args[0], list) else args[0],) + args[1:])`.
ImportError: cannot import name 'memoize' from 'memoization'
Developers often use `memoize` as a generic term or a common decorator name for caching. However, the `memoization` library's primary decorator is named `cached`, not `memoize`. This `ImportError` occurs when trying to import a non-existent name from the library.
fix
Import the correct decorator name, which is `cached`. The import statement should be `from memoization import cached`.
AttributeError: 'function' object has no attribute 'cache_info'
This error occurs when a developer familiar with `functools.lru_cache` attempts to call `.cache_info()` on a function decorated with `memoization.cached`. The `memoization` library provides its own set of cache inspection methods (e.g., `cache_clear`, `cache_is_empty`, `cache_is_full`), but it does not expose an attribute or method specifically named `cache_info` like `lru_cache` does.
fix
To inspect the cache or interact with it, use the specific methods provided by the `memoization` library. For example, use `my_function.cache_clear()` to clear the cache, or access internal statistics if exposed by the library's API (the `memoization` library's README mentions 'Cache statistics' but details specific methods for clearing, checking empty/full, not a direct `cache_info`). If more detailed introspection is needed, consult the library's documentation for equivalent functionality or access the `f.__wrapped__` if available and inspect the cache directly.
Upgrade
Version history
0.4.0latest on PyPI · released Aug 1, 2021
Audit
Dependencies

No dependency data recorded yet.

Agent activity
17 hits · last 30 days
node
14
OpenAI (training)
1
Resources
memoization — pip install memoization · libregistry