Skip to main content

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

This version

0.1

Download files

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

Source Distribution

memoized-lambda-0.1.tar.gz (8.4 kB view hashes)

Uploaded Source

Built Distribution

memoized_lambda-0.1-py3-none-any.whl (7.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