clean caches when needed
Project description
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. :-)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file xcache-0.2.tar.gz
.
File metadata
- Download URL: xcache-0.2.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | baff70b9c911315ab96a11d262db1da55b594311358618b955db1aef01aa482c |
|
MD5 | 3c038c984243274ca505e376ff2c7ad3 |
|
BLAKE2b-256 | 4e1430842615cf687f6267e83a166b6824b2b3c15381f38c4d2f39e9cfa273e6 |
File details
Details for the file xcache-0.2-py2.py3-none-any.whl
.
File metadata
- Download URL: xcache-0.2-py2.py3-none-any.whl
- Upload date:
- Size: 2.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ead03a7a87c1461cd4a00cbf7f36f960311e803219cfe9ae44d7d9340bdc8411 |
|
MD5 | 7b2b0f28b9630cf48c599b72e51115e9 |
|
BLAKE2b-256 | c724f410a7c7e21cd8aa3b677761002ca4aff412156bd86f119401c7701b08dd |