No project description provided
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file memoized-lambda-0.1.tar.gz
.
File metadata
- Download URL: memoized-lambda-0.1.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83479e10b6795a0479f4a3fa8c3b0dc46bff4430ca048f84d13055cdb395e5e4 |
|
MD5 | af64e4ebe75b888eb724104bc9e8ac1b |
|
BLAKE2b-256 | d87358f2a9e69ce902e30c30c1ddc1c7c04f2731238ddec810751712bdb251c7 |
File details
Details for the file memoized_lambda-0.1-py3-none-any.whl
.
File metadata
- Download URL: memoized_lambda-0.1-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c2afa4c0d57167b2cc3bf4d0ffd9caaa5409048973676d245ab376a4438b6b8 |
|
MD5 | d9ef43a49301899964caf5b153138ecf |
|
BLAKE2b-256 | 976803faf538a7210986a66e54c9edd50eabbbbc63ffe58beb7b39c24fde1d5e |