Last active
May 22, 2019 14:41
-
-
Save christianstrang/d7b888551946671b278db345b0090b6a to your computer and use it in GitHub Desktop.
sailsjs helper to easily retrieve data from cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var util = require('util'); | |
module.exports = { | |
friendlyName: 'Cache read', | |
description: 'used to retrieve data from cache via a key, undefined if data doesnt exist or expired.', | |
inputs: { | |
key: { | |
type: 'string', | |
example: 'foo', | |
description: 'key used to read the data from the cache', | |
required: true | |
} | |
}, | |
exits: { | |
success: { | |
description: 'All done.', | |
}, | |
}, | |
fn: async function (inputs, exits) { | |
await sails.getDatastore('cache').leaseConnection(async (db)=>{ | |
var found = await (util.promisify(db.get).bind(db))(inputs.key); | |
if (found === null) { | |
return exits.success(undefined); | |
} else { | |
return exits.success(JSON.parse(found)); | |
} | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment