1.0.3 • Published 6 years ago

@repit/lambda-cache v1.0.3

Weekly downloads
1
License
MIT
Repository
-
Last release
6 years ago

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

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago