TTL Memoized - A memoized decorator with TTL (time-to-live) support.
The idea of memoized is great, as some resources are expensive, so you want to cache them.
However, there isn’t a memoized lib support TTL (time-to-live) at the moment, or I haven’t find any thing yet.
So I implement this ttl_memoized to fill in the gap here.
Also, this lib is meant to be thread-safe, using threading.local object to store the variables.
Installation:
Should be faily simple using pip:
pip install ttl_memoized
Usage
The usage is simple, and the best way to explain it, is with my test cases:
def test_basic():
@memoized(ttl=0.5)
def a(name):
return datetime.datetime.now()
@memoized(ttl=0.5)
def b(name, *args, **kwargs):
return datetime.datetime.now()
a1 = a(1)
b1 = b(1, 2, 3, what='ever', you='want', to='be')
for i in range(100):
assert a1 == a(1)
for i in range(100):
assert b1 is b(1, 2, 3, what='ever', you='want', to='be')
a2 = a(2)
assert a2 != a1
# let the cache expired...
time.sleep(0.51)
assert a(1) != a1
assert b1 != b(1, 2, 3, what='ever', you='want', to='be')
Ceveat
The parameter for the functions is required to be serializable with json libs, as the lib is using json to build the keys from parameters.
Metadata
Release files for ttl_memoized 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ttl_memoized-0.2.1-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Release files / ttl_memoized-0.2.1-py2.py3-none-any.whl
| Download URL | ttl_memoized-0.2.1-py2.py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
0ad0f1efbac5d2dc0295054b58a93651ffb55c2e67aa31def4bfdd28e23b52b4
|
|
BLAKE2b-256 checksum How to use checksums |
6dc71b0547aa87a97f2e464e569df190b5604dea2b5ad3f449a7eccbe8bbe54b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |