Skip to main content

TTLMemoized - a memoized decorator with TTL (time-to-live) support

Project description

PyPI version Build Status Supported Python Versions

TTL Memoized - A memoized decorator with TTL (time-to-live) support.

The idea of memoized is great, as some resources are expensive, so you want to cache them.

However, there isn’t a memoized lib support TTL (time-to-live) at the moment, or I haven’t find any thing yet.

So I implement this ttl_memoized to fill in the gap here.

Also, this lib is meant to be thread-safe, using threading.local object to store the variables.


Should be faily simple using pip:

pip install ttl_memoized


The usage is simple, and the best way to explain it, is with my test cases:

def test_basic():

  def a(name):

  def b(name, *args, **kwargs):

  a1 = a(1)
  b1 = b(1, 2, 3, what='ever', you='want', to='be')

  for i in range(100):
    assert a1 == a(1)

  for i in range(100):
    assert b1 is b(1, 2, 3, what='ever', you='want', to='be')

  a2 = a(2)
  assert a2 != a1

  # let the cache expired...

  assert a(1) != a1
  assert b1 != b(1, 2, 3, what='ever', you='want', to='be')


The parameter for the functions is required to be serializable with json libs, as the lib is using json to build the keys from parameters.

Project details

Download files

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

Built Distribution

ttl_memoized-0.2.1-py2.py3-none-any.whl (4.4 kB view hashes)

Uploaded 2 7

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