Skip to main content

Memoization decorator for Python, with optional TTL (measured in time or function calls) for the cached results.

Project description

pymesis

Memoization decorator for Python, with optional TTL (measured in time or function calls) for the cached results.

Installation

Using pipenv (recommended):

pipenv install pymesis

Using pip:

pip install pymesis

Usage

Basic usage:

from pymesis import memoize
from time import time, sleep

@memoize
def slowFunction(*args, **kwargs):
    sleep(1)
    return 'Completed'

start = time()
print(slowFunction('some', 'data')
print(f'Time elapsed: {time() - start :.1f} seconds\n')  # First call is slow

start = time()
print(slowFunction('some', 'data')
print(f'Time elapsed: {time() - start :.1f} seconds\n')  # Second call is fast, as data is cached

start = time()
print(slowFunction('some', 'new', 'data')
print(f'Time elapsed: {time() - start :.1f} seconds\n')  # This call is slow, as attributes have changed

With TTL:

from pymesis import memoize, TTLUnit
from time import time, sleep

@memoize(ttl=1, ttl_unit=TTLUnit.CALL_COUNT) # Only return cached result once, then go back to calling flowFunction
def slowFunction(*args, **kwargs):
    sleep(1)
    return 'Completed'

start = time()
print(slowFunction('some', 'data')
print(f'Time elapsed: {time() - start :.1f} seconds\n')  # First call is slow

start = time()
print(slowFunction('some', 'data')
print(f'Time elapsed: {time() - start :.1f} seconds\n')  # Second call is fast

start = time()
print(slowFunction('some', 'data')
print(f'Time elapsed: {time() - start :.1f} seconds\n')  # Third call is slow, as cache has expired (TTL=1).

Note that functions are assumed to be unchanged as long as the name is unchanged. Redefined function (with decorator applied again) will return cached result of similar call to the original function.

The decorator works with methods as well as functions. Note that the same method on two different instances of the same class are considered different methods, therefore a call to the second will not give the cached result from the first.

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

pymesis-0.1.0.tar.gz (5.8 kB view hashes)

Uploaded Source

Built Distribution

pymesis-0.1.0-py3-none-any.whl (3.9 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