1.0.0 • Published 4 years ago

lambda-cache v1.0.0

Weekly downloads
1
License
ISC
Repository
github
Last release
4 years ago

lambda-cache

Package to cache objects in memory for NodeJS Lambda Functions

How to use

import { APIGatewayEvent } from 'aws-lambda';
import { Cache } from '../../../common/cache';

const cache = new Cache();

export const handler = async (ev: APIGatewayEvent) => {

    const testKey = "testkey";

    const val = cache.get(testKey);

    if(val) {
        console.log("Cache hit")

        return {
            statusCode: 200,
            body: JSON.stringify(val),
            headers: {
                'Content-type': 'application/json'
            },
        };
    }

    console.log("Cache miss");

    let test = {"k":"testing"};
    cache.set("test", test, 10); // cache object for 10 seconds

    return {
            statusCode: 200,
            body: JSON.stringify(test),
            headers: {
                'Content-type': 'application/json'
            },
        };
}