Skip to main content

Memoized Lambda

MemoizedLambda is a class that provides an async invoke interface with deduplication of requests and memoization of responses for a Boto3 Lambda Client.

Usage

First, create a cache for the memoization:

from cachetools import TTLCache
cache = TTLCache(ttl=60, maxsize=1024)

The cache can be any object that implements collections.abc.MutableMapping.

Then, create a lambda client and a memoized lambda for some AWS Lambda function:

import boto3
from memoized_lambda import MemoizedLambda
client = boto3.client("lambda")
mlambda = MemoizedLambda(lambda_client=client, function_name="function", cache=cache)

Finally, invoke the lambda function:

import asyncio
loop = asyncio.get_event_loop()
coro = asyncio.gather(mlambda.invoke({}), mlambda.invoke([]), mlambda.invoke({}))
loop.run_until_complete(coro)

invoke returns the response payload, or raises a MemoizedLambdaError exception on error. The first invocation with a given input will invoke the lambda, while subsequent invocations for the same input will read the response payload from the cache. In the above example, the lambda is invoked once for {} and once for [].

The request and response payloads are transformed using configurable transform functions that default to JSON serialization and deserialization. If you want to override them, use the optional request_transform and response_transform arguments:

mlambda = MemoizedLambda(
    lambda_client=client,
    function_name="function",
    cache=cache,
    request_transform=lambda x: x,
    response_transform=lambda x, y: y)

The response_transform function gets the request and response payload as arguments.

If you want to avoid caching some responses, use the optional cache_filter argument:

mlambda = MemoizedLambda(
    lambda_client=client,
    function_name="function",
    cache=cache,
    cache_filter=lambda x: not isinstance(x, Exception))

Note that MemoizedLambdaError exceptions are never cached, even if raised by the response transform.

Release files for memoized-lambda 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for memoized-lambda 0.1
File Size Uploaded
memoized-lambda-0.1.tar.gz 8.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for memoized-lambda 0.1
File Interpreter ABI Platform
memoized_lambda-0.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.3 kB

Release files / memoized-lambda-0.1.tar.gz

Download URL memoized-lambda-0.1.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
83479e10b6795a0479f4a3fa8c3b0dc46bff4430ca048f84d13055cdb395e5e4
BLAKE2b-256 checksum
How to use checksums
d87358f2a9e69ce902e30c30c1ddc1c7c04f2731238ddec810751712bdb251c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.2

Release files / memoized_lambda-0.1-py3-none-any.whl

Download URL memoized_lambda-0.1-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8c2afa4c0d57167b2cc3bf4d0ffd9caaa5409048973676d245ab376a4438b6b8
BLAKE2b-256 checksum
How to use checksums
976803faf538a7210986a66e54c9edd50eabbbbc63ffe58beb7b39c24fde1d5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.2

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page