A full serverless cache for DynamoDB that uses ElectroDB under the hood for querying and TTL management.
- PNPM
- Docker (to run tests)
This library also requires a DynamoDB table to be created with the following required fields:
-
pk
: String (HASH key) -
sk
: String (SORT key) -
ttl
: Number (DynamoDB TTL field)
-
pnpm test
: Run all tests -
pnpm build
: Build the lib for CJS and ESM
Setup a DynamoDB table with the above fields (pk
, sk
and ttl
). Then create an instance of the cache as follows:
const client = new DynamoDBClient();
// pass in the client, table name, namespace (optional) and TTL in seconds (optional)
const cache = new DynamoDBCache(client, 'cache-table-name', 'my-app-name', 60);
To set an item on the cache, use:
await cache.set('keyName', 'value');
To fetch an item, use:
const value = await cache.get('keyName');
The other methods are:
await cache.delete('keyName');
const isKeyPreset: boolean = await cache.has('keyName');
// clear all values from a namespace
await cache.clear('my-app-name');