Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

simpledynamodbcache

1.0.4 • Public • Published

Simple DynamoDB Cache

I wrote this because I am using DynamoDB to store the backend data for a website but I also sometimes need to fetch information from slow legacy systems.

In the case that the cached data is "expired" whatever time period you determine that to be for you (in seconds) then you can fetch that legacy data and update the cache.

It will add a key to your object in dynamo, currently statically named 'last_fetch'

Usage:

 
//
// 60  - The default cache timeout to use ( in seconds )
// 'us-east-1'  - How you would pass the region
//
var c = require('simpledynamodbcache')(60, 'us-east-1');
 
var parms = {
    TableName: 'mytable', 
    Key : { mykey_id : '12345' }
}
 
res = await db.get(parms).promise(); // Perform some query... 
 
//
// res looks something like:
// Item: {
// someObject: {
// foo : 'bar'
// }
// }
//
 
if (c.cacheExpired(res.Item.someObject)) {
 
    // fetch from legacy ... 
    var freshValues = fetchFromLegacy();
 
    res.Item.someObject.foo = freshValues.foo; // Update from legacy
 
    // Update in DynamoDB
    await c.cache('mytable', { mykey_id : '12345' } , 'someObject', res.Item.someObject);
}
 
// else it's not expired, just use it... 
return res;

Alternately for flat objects you can use cacheItem() which will put the updated Item with the timestamp added.

 
if (c.cacheExpired(res.Item)) {
 
        // fetch from legacy ...
    var freshValues = fetchFromLegacy();
 
    res.Item.foo = freshValues.foo; // Update from legacy
 
    await c.cacheItem('mytable', res.Item);
}
 

Install

npm i simpledynamodbcache

DownloadsWeekly Downloads

63

Version

1.0.4

License

ISC

Unpacked Size

4.7 kB

Total Files

3

Last publish

Collaborators

  • avatar