Skip to content

Instantly share code, notes, and snippets.

@christianstrang
Last active May 22, 2019 14:41
Show Gist options
  • Save christianstrang/d7b888551946671b278db345b0090b6a to your computer and use it in GitHub Desktop.
Save christianstrang/d7b888551946671b278db345b0090b6a to your computer and use it in GitHub Desktop.
sailsjs helper to easily retrieve data from cache
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