3.0.1 • Published 2 years ago

@lamware/memoize v3.0.1

Weekly downloads
-
License
GPL-3.0-only
Repository
github
Last release
2 years ago

This Lamware Middleware allows you to memoize variables and instances outside of the Lambda Handler with ease.

Why?

A lesser known feature of Lambda is that variables declared outside of the handler persist through the handler lifecycle. Since a Lambda instance, once cold-started, stays alive for 5-15 minutes (varies based on activity and the type of Lambda function), this provides a clear performance gain over having all logic instantiated inside the handler itself, since it'll only execute once (on the first request/invocation).

Currently, Lambda doesn't support top-level async/await, meaning set-up that involves non-synchronous operations isn't as simple as placing it outside the handler. By allowing you to supply a closure, and Lamware having support for asynchronous initialization, this middleware allows you to asynchronously memoize/load outside of the handler.

Installation

This package is available via NPM:

yarn add @lamware/memoize

# or

npm install @lamware/memoize

Usage

import type { APIGatewayProxyHandlerV2 } from 'aws-lambda';
import { memoize } from '@lamware/memoize';
import { lamware } from '@lamware/core';

interface MemoizePayload {
  count: number;
}

const { handler } = lamware<APIGatewayProxyHandlerV2<any>>()
  .use(memoize<MemoizePayload>(async () => {
    return { count: 1 };
  }, {
    // [optional] Whether to throw an `Error` if the memoize closure fails [default: true]
    throwOnError: false,
  }))
  .execute(async ({ state }) => {
    return {
      statusCode: 200,
      message: 'count should always be `1` since it is memoized!',
      count: state.count,
    };
  });

export { handler };
2.2.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.2

3 years ago

1.3.0

3 years ago

1.2.22

3 years ago

2.0.1

3 years ago

1.2.21

3 years ago

1.2.20

3 years ago

1.2.19

3 years ago

1.2.18

3 years ago

1.2.17

3 years ago

1.2.16

3 years ago

1.2.15

3 years ago

1.2.14

3 years ago

1.2.13

3 years ago

1.2.12

3 years ago

1.2.11

3 years ago

1.2.10

3 years ago

1.2.9

3 years ago

1.2.8

3 years ago

1.2.7

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago