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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file python-multimapwithttl-0.1.0.tar.gz.

File metadata

  • Download URL: python-multimapwithttl-0.1.0.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8

File hashes

Hashes for python-multimapwithttl-0.1.0.tar.gz
Algorithm Hash digest
SHA256 96ea281839b8b6bddf44befd5ec775735f7f0afbcae925bc62fe338df89bca19
MD5 978ed293067eef21448956bee28eafab
BLAKE2b-256 a529311c99f63bf2cafd8b8ae5723036a4804b9ea0a845d44d8fe5c8acdf60fd

See more details on using hashes here.

File details

Details for the file python_multimapwithttl-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: python_multimapwithttl-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8

File hashes

Hashes for python_multimapwithttl-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 858d5692be630ab32fcfe3573363832475fc351537e1f11607c62187865131bd
MD5 580620b067ea82f4992935a968703e14
BLAKE2b-256 55b7326bbd8cf6b28ef6c64ac59c8e0fd4746b1cb9ced9e4f150821249f0fe78

See more details on using hashes here.

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