autottlcache
A time-to-live (TTL) cache dictionary with auto-expiry.
Installation
To install from PyPI:
$ pip install autottlcache
Usage
The TTL cache behaves very much like a dictionary. In fact, it is a MutableMapping so supports the
dictionary interface. It has one important difference though, which is that the items within it will
expire and automatically be removed.
Let's create an AutoTTLCache with a a maximum size of twelve entires, and a TTL of 30 seconds:
>>> from autottlcache import AutoTTLCache
>>> cache = AutoTTLCache(maxsize=12, ttl=30.0)
We can add entries to the cache:
>>> cache[1] = 56
>>> cache[2] = 89
>>> cache[3] = 99
>>> cache[4] = 10
>>> cache[5] = 20
And watch them expire, by repeatedly asking the cache to list its keys:
>>> list(cache.keys())
[2, 3, 4, 5]
We took long enough adding the keys that the first one had already expired:
>>> list(cache.keys())
[3, 4, 5]
Now the second one has gone too:
>>> list(cache.keys())
[3, 4, 5]
This time we're quick enough to get in before the third one expires:
>>> list(cache.keys())
[3, 4, 5]
>>> list(cache.keys())
[3, 4, 5]
>>> list(cache.keys())
[3, 4, 5]
>>> list(cache.keys())
[4, 5]
>>> list(cache.keys())
[4, 5]
>>> list(cache.keys())
[4, 5]
>>> list(cache.keys())
[5]
>>> list(cache.keys())
[5]
>>> list(cache.keys())
[5]
>>> list(cache.keys())
[5]
>>> list(cache.keys())
[5]
>>> list(cache.keys())
[]
All gone.
Stale items are removed from the cache either when an operation on the cache object is invoked, such as when an item is retrieved, or by a continuously running background thread that periodically removes expired items. The cache will be checked with a frequency of ten times the TTL period, so an item in a cache with a TTL of 30 seconds should live no longer than 33 seconds.
Programming style
Owing to the fact that items will auto-expire, to avoid race conditions you should strongly prefer an Easier to Ask for Forgiveness than Permission (EAFP) programming style, rather than a Look Before You Leap (LBYL) style.
Risky race-condition:
from autottlcache import AutoTTLCache
cache = AutoTTLCache(maxsize=100, ttl=30)
cache["daffodils"] = Image("daffodils-3280x2040.png")
# ...
if "daffodils" in cache:
# What happens if "daffodils" expires here?
image = cache["daffodils"] # This could raise a key error which is unhandled
else:
print("No flowers for you!")
Better to use a single operation to retrieve an object, and handle the failure:
from autottlcache import AutoTTLCache
cache = AutoTTLCache(maxsize=100, ttl=30)
cache["daffodils"] = Image("daffodils-3280x2040.png")
# ...
try:
image = cache["daffodils"]
except KeyError:
print("No flowers for you!")
else:
display(image)
Resource use
A single additional thread will be running during any periods for which one or more AutoTTLCache
objects are extant. The thread will start when the first AutoTTLCache is created, and will
terminate shortly after the last AutoTTLCache is finalized.
Development
To release, there is a short manual process:
$ bumpversion patch
$ python setup.py sdist bdist_wheel
$ twine upload dist/* --config-file=path/to/sixty-north.pypirc
Release files for autottlcache 1.0.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| autottlcache-1.0.8.tar.gz | 6.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| autottlcache-1.0.8-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.7 kB
Release files / autottlcache-1.0.8.tar.gz
| Download URL | autottlcache-1.0.8.tar.gz |
|---|---|
| Size | 6.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
74a39ae350ae25ddf3b65de7b9fb306f90b6235d5c81c80776437beb7cfca73a
|
|
BLAKE2b-256 checksum How to use checksums |
facc1c7f283fbf487974ba163d2b37fb40ec66805513d91bacde5461244ea6a4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.0
|
Release files / autottlcache-1.0.8-py3-none-any.whl
| Download URL | autottlcache-1.0.8-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c10ed48ec17a4381b68b24e89c65c47619faf4a959e04e259f530944563abc67
|
|
BLAKE2b-256 checksum How to use checksums |
cea6944dea17fd0adb41dd94bc881d91454a5ce42bcfd3ca3710e9515460401b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.0
|