Skip to main content

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

Project description

MultiMapWithTTL

pypi python Build Status codecov

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

Description

This lib is based on: https://quickleft.com/blog/how-to-create-and-expire-list-items-in-redis/ 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

python_multimapwithttl-0.1.0-py3-none-any.whl (6.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page