Skip to content

Instantly share code, notes, and snippets.

@christianstrang
Last active May 22, 2019 14:41
Show Gist options
  • Save christianstrang/91c3e42fda5d568db319f443582b6254 to your computer and use it in GitHub Desktop.
Save christianstrang/91c3e42fda5d568db319f443582b6254 to your computer and use it in GitHub Desktop.
sailsjs helper to easily save data in cache
var util = require('util');
module.exports = {
friendlyName: 'Cache write',
description: 'used to save data into cache using a key and a TTL in seconds until the cache expires the data.',
inputs: {
key: {
type: 'string',
example: 'foo',
description: 'key used to write the data from the cache',
required: true
},
ttlInSeconds: {
type: 'number',
defaultsTo: 60, // for 60 seconds
description: 'how long till the cache expires, in seconds',
},
data: {
type: 'string',
example: 'bar',
description: 'data (simple string or complex object). data is stringified before its stored, has to be parsed when it is retrieved from storage.',
required: true
}
},
exits: {
success: {
description: 'All done.',
},
},
fn: async function (inputs) {
await sails.getDatastore('cache').leaseConnection(async (db)=>{
await (util.promisify(db.setex).bind(db))(inputs.key, inputs.ttlInSeconds, inputs.data);
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment