Power Cache
Simple (but powerful) Caching Tools.
Why another caching library
There are many libraries out there to deal with the same problem that this library tries to solve, but most of them fall short on at least one of the following points:
- Minimalism.
- Providing proper type hints to ease the user's life when using the library.
- Providing out-of-the-box support for asynchronous functions.
- Simplicity:
- In
power_cache, capacity is measured just counting items, and not using their size as other libraries do. There are legitimate reasons to avoid the "sizes approach": it decreases performance, and it's highly error prone.
- In
- Extra flexibility:
power_cacheallows its decorators to not cache certain chosen values. This can be quite handy for some nullish/emptyish results. - Correctness:
- Some popular implementations incorrectly implement
__eq__by just comparing object hashes. - Some popular implementations implement
__hash__in a way that collisions will be more frequent than desirable.
- Some popular implementations incorrectly implement
- Performance: Even though
power_cacheis not outstanding in any sense when it comes to performance, at least it avoids some questionable decisions made in other libraries (like relying ondatetimeinstead ofmonotonicforttlcaches).
Usage
LRU Cache
from power_cache import LRUCache
cache = LRUCache(capacity=3)
# We can also specify key & value types if we are using `mypy` or `pytypes`
cache = LRUCache[str, int](capacity=3)
cache['the answer to everything'] = 42
cache['the answer to everything'] # returns 42
cache['a'] = 1
cache['b'] = 2
cache['c'] = 3
# Raises KeyError, because the key was the least recently used, and the capacity
# is only 3, so the previous value was evicted.
cache['the answer to everything']
TTL Cache
TTLCache is very similar to LRUCache, with the distinction that it marks
values as expired if they are too old.
from time import sleep
from power_cache import TTLCache
cache = TTLCache(capacity=3, ttl=120) # Values valid only for 2 minutes
# We can also specify key & value types if we are using `mypy` or `pytypes`
cache = TTLCache[str, int](capacity=3, ttl=120)
cache['the answer to everything'] = 42
cache['the answer to everything'] # returns 42
cache['a'] = 1
cache['b'] = 2
cache['c'] = 3
# Raises KeyError, because the key was the least recently used, and the capacity
# is only 3, so the previous value was evicted.
cache['the answer to everything']
assert len(cache) == 3
cache.evict_expired() # We can manually evict all expired values
assert len(cache) == 3 # Nothing was evicted because values are too recent
sleep(121)
# Now all values are marked as expired, but not evicted automatically, because
# that would spend too much CPU time.
assert len(cache) == 3
cache.evict_expired() # We can manually evict all expired values
assert len(cache) == 0
Memoize
from power_cache import Memoize
# Runtime annotations are preserved.
# `capacity` must be always specified, while `cache_type` is "lru" by default.
@Memoize(capacity=3, cache_type="lru")
def my_function(): ...
@Memoize(capacity=3, cache_type="ttl", ttl=120)
def another_function(): ...
# We can instruct our memoizer to not save certain results, like `None`
@Memoize(capacity=3, results_to_discard=(None,))
def my_function(): ...
AsyncMemoize
from power_cache import AsyncMemoize
# Runtime annotations are preserved.
# `capacity` must be always specified, while `cache_type` is "lru" by default.
@AsyncMemoize(capacity=3, cache_type="lru")
async def my_function(): ...
@AsyncMemoize(capacity=3, cache_type="ttl", ttl=120)
async def another_function(): ...
# We can instruct our memoizer to not save certain results, like `None`
@AsyncMemoize(capacity=3, results_to_discard=(None,))
def my_function(): ...
Metadata
Release files for power-cache 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| power_cache-0.1.1.tar.gz | 8.1 kB | Without provenance | Details for power_cache-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| power_cache-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for power_cache-0.1.1-py3-none-any.whl |
Total release size: 16.9 kB
Release files / power_cache-0.1.1.tar.gz
| Download URL | power_cache-0.1.1.tar.gz |
|---|---|
| Size | 8.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
68f8ad3b96f908c4cda0378cc95f4cf0d9356d33cb8db98702534482991bc246
|
|
BLAKE2b-256 checksum How to use checksums |
5aa188c24fc2aa016df23662368569e19178070440c35b5b2d12fed18e69dc13
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.10 CPython/3.9.7 Darwin/20.6.0
|
Release files / power_cache-0.1.1-py3-none-any.whl
| Download URL | power_cache-0.1.1-py3-none-any.whl |
|---|---|
| Size | 8.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d58818f40ac27ac427d302fd85c70b0e1f3e4a8f330afdadbcb998f8d57b58aa
|
|
BLAKE2b-256 checksum How to use checksums |
946b40c3d091514be6000ad7fd9c178addafd8815264f6f2390fbc86cd695d54
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.10 CPython/3.9.7 Darwin/20.6.0
|
Security for power-cache 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.