Skip to main content

ttlstore provides dict like functionality that automatically removes the element from memory after ttl seconds

Project description

Installation:

pip install ttlstore

Getting started:

import time

from ttlstore import TTLStore


d = TTLStore(ttl=1) # use d like a python dictionary
d['a'] = 'b'
time.sleep(2)
# 'a' is not present any more in the memory
print(d)

def call_me(key, value):
   print(key, value)

d = TTLStore(ttl=1, callback=call_me)
d['a'] = 'b'
time.sleep(2)
# 'a' is not present any more in the memory and call_me is called.
print(d)

To run example script:

➜  python example.py
INFO:root:-------- executing example 1 ----------
DEBUG:root:contents of dict: {}
INFO:root:-------- end of example 1 ---------
INFO:root:-------- executing example 2 ----------
DEBUG:root:executing call back for deleted k: 1, v: 3
DEBUG:root:executing call back for deleted k: 2, v: 4
DEBUG:root:contents of dict: {}
INFO:root:-------- end of example 2 ---------
INFO:root:-------- executing example 3 ------
DEBUG:root:done inserting 1000 elements in a tight loop, time taken: 0.0015082359313964844
DEBUG:root:length of dict: 0
DEBUG:root:done inserting 10000 elements sporadically, time taken: 3.6316630840301514
DEBUG:root:length of dict: 0
INFO:root:-------- end of example 3 ----

To run tests:

python -m unittest -v tests/ttlstore_test.py

Unittest results:

 ➜ python -m unittest -v tests/ttlstore_test.py
test_basics (tests.ttlstore_test.TTLStoreTestCases) ... ok
test_create (tests.ttlstore_test.TTLStoreTestCases) ... ok
test_get (tests.ttlstore_test.TTLStoreTestCases) ... ok
test_iter (tests.ttlstore_test.TTLStoreTestCases) ... ok
test_not_implemented (tests.ttlstore_test.TTLStoreTestCases) ... ok
test_pop (tests.ttlstore_test.TTLStoreTestCases) ... ok
test_reset_of_key_no_trim (tests.ttlstore_test.TTLStoreTestCases)
Re-setting an existing key should not cause a non-expired key to be dropped ... ok
test_set (tests.ttlstore_test.TTLStoreTestCases) ... ok
test_setdefault (tests.ttlstore_test.TTLStoreTestCases) ... ok

----------------------------------------------------------------------
Ran 9 tests in 0.089s

OK

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

ttlstore-0.1.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ttlstore-0.1.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file ttlstore-0.1.0.tar.gz.

File metadata

  • Download URL: ttlstore-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.13

File hashes

Hashes for ttlstore-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bb378346411b703f676e5b3389cedf087e156951f9037beeabd394989503373b
MD5 2b564010aa1c50af76c05e275dfcb4fe
BLAKE2b-256 b019756b83301750756e8af4abc4336f3a326d7d89d4e706cd4c49bf0109e8e6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ttlstore-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.13

File hashes

Hashes for ttlstore-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 00e372ed54ea71416be7eb646883f4938c0b29d295f45d8a327b01b8abbd88a0
MD5 45435fda69971c4cc99f9af2c3f3684b
BLAKE2b-256 696fefd12e2707e8af3e7f5869031cbc83d0d7a1611df3a63884363e42a8440b

See more details on using hashes here.

Supported by

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