Skip to main content

An implementation of multimap with per-item expiration backed up by Redis.

Project description


pypi python Build Status codecov

An implementation of multimap with per-item expiration backed up by Redis.


This lib is based on: without the need for an extra job to delete old items.

Values are internally stored on Redis using Sorted Sets :

key1: { (score1, value1), (score2, value2), ... }
key2: { (score3, value3), (score4, value4), ... }

Where the score is the timestamp when the value was added. We use the timestamp to filter expired values and when an insertion happens, we opportunistically garbage collect expired values.

The key itself is set to expire through redis ttl mechanism together with the newest value. These operations result in a simulated multimap with item expiration.

You can use to keep track of values associated to keys, when the value has a notion of expiration.

>>> s = MultiMapWithTTL(redis_client, 'multimap')
>>> s.add('a', 1, 2, 3)
>>> sorted(s.get('a'))
[1, 2, 3]
>>> s.add_many([('b', (4, 5, 6)), ('c', (7, 8, 9)), ])
>>> sorted(sorted(values) for values in s.get_many('a', 'b', 'c')))
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-multimapwithttl-0.1.0.tar.gz (8.0 kB view hashes)

Uploaded source

Built Distribution

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page