TTLMemoized - a memoized decorator with TTL (time-to-live) support
Project description
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 it.
The python3 functools comes with LRU cache.
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 lib to fill in the gap here.
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')
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 Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for ttl_memoized-0.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c47339222f3926e8892b0edfe06d168168d738574c2a4477da1ab8c4a354dfde |
|
MD5 | edfe5dbbabde9208a4eeb1ac112483a5 |
|
BLAKE2b-256 | 25b6a8e6363de0ef770bfeff01c1b3d4a45ab50b6f364baec901ce5b81a3386b |