XCACHE
It’s like lru_caches but can be cleared automatically or via context managers.
Why?
There are only two hard things in Computer Science: cache invalidation and naming things.
– Phil Karlton
So, caches are fairly easy, but their invalidation is kind of hard to get right.
What type of cache is used?
The default implementation is the lru_cache of the stdlib but you can drop in your own.
How?
Python features garbage collection aka. automatic memory management. Let’s make use of it:
from xcache import cache_gen
request_cache = cache_gen(lambda: request) # default cache_impl=lru_cache
Here, we defined our own cache that needs the global request for invalidating the cache. As soon as request is garbage-collected, the cached data is freed.
@request_cache()
def fib(n):
return fib(n-1) + fib(n-2) if n > 1 else 1
Here, we see how to decorate a function to work like a lru_cache.
Now, let’s see how this works:
class Request(): pass
request = Request()
print(fib(10))
print(fib(20))
print(fib.cache_info()) # fib cache contains 2 items
request = Request() # invalidates all caches
print(fib.cache_info()) # fib cache contains 0 items
print(fib(10))
print(fib(20))
Context Manager for more more control over cache invalidation
If you need more control, the context manager clean_caches is what you need:
from xcache import cached, clean_caches
@cached()
def fib(n):
return fib(n-1) + fib(n-2) if n > 1 else 1
with clean_caches():
print(fib(10))
print(fib(20))
print(fib.cache_info()) # fib cache contains 2 items
print(fib.cache_info()) # fib cache contains 0 items
You can even specify what object the caches should be attached to:
@cached()
def fib(n):
return fib(n-1) + fib(n-2) if n > 1 else 1
with clean_caches(Request()) as request:
print(fib(10))
print(fib(20))
print(fib.cache_info()) # fib cache contains 2 items
print(fib.cache_info()) # fib cache contains 0 items
Can these context managers be nested?
Sure. At each entrance and and exit of each context, all associated caches are emptied.
Conclusion
Good
cache invalidation done easy
works via garbage collection
works via context managers
works with Python2 and Python3
Bad
unkown ;-)
Ideas are always welcome. :-)
Metadata
Release files for xcache 0.2
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 | |
|---|---|---|---|---|
| xcache-0.2.tar.gz | 3.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| xcache-0.2-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 5.8 kB
Release files / xcache-0.2.tar.gz
| Download URL | xcache-0.2.tar.gz |
|---|---|
| Size | 3.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
baff70b9c911315ab96a11d262db1da55b594311358618b955db1aef01aa482c
|
|
BLAKE2b-256 checksum How to use checksums |
4e1430842615cf687f6267e83a166b6824b2b3c15381f38c4d2f39e9cfa273e6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / xcache-0.2-py2.py3-none-any.whl
| Download URL | xcache-0.2-py2.py3-none-any.whl |
|---|---|
| Size | 2.6 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
ead03a7a87c1461cd4a00cbf7f36f960311e803219cfe9ae44d7d9340bdc8411
|
|
BLAKE2b-256 checksum How to use checksums |
c724f410a7c7e21cd8aa3b677761002ca4aff412156bd86f119401c7701b08dd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for xcache 0.2
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.