@repit/lambda-cache

1.0.3 • Public • Published

Lambda Cache

Short-term, in-memory caching for AWS Lambda.

Installation

$ npm install @repit/lambda-cache --save

Usage

'use strict'

// Load the Cache class.
const Cache = require('@repit/lambda-cache')

// Create a Cache object.
const cache = new Cache()

exports.handler = (event, context, callback) => {
  // Used for cache invalidation.
  cache.validate(event)

  // Get the number of items in the cache.
  cache.size() // 0 ... n

  // Check if cache is empty.
  cache.empty() // true || false

  // Add a new item to cache.
  // Note: When `ttl` is not provided item will not expire.
  cache.set(key, value, ttl)

  // Get an item from cache.
  // Note: Returns `undefined` when item is not found or it is expired.
  cache.get(key)

  // Check if an item is in cache and it is not expired.
  cache.has(key) // true || false

  // Remove an item from cache.
  cache.remove(key)

  // Remove all items from cache.
  cache.clear()
}

Cache invalidation.

Cache invalidation is done by comparing a saved cache token with a given cache token. If they don't match then cache token is updated with the last one and cache is cleared.

To check if cache should be invalidated call cache.validate(event) at the top of your lambda function handler.

Cache tokens are expected to be found as follows:

{
  "cacheToken": "TOKEN_VALUE",
  "queryStringParameters": {
    "tkn": "TOKEN_VALUE"
  }
}

Notes

  • Each instance of Cache class has it's own cache store.
  • Cache will be kept as long as your lambda function is warm.

Testing

$ npm test

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @repit/lambda-cache

Weekly Downloads

4

Version

1.0.3

License

MIT

Last publish

Collaborators

  • sumanion