Skip to content

Instantly share code, notes, and snippets.

@evantahler
Last active October 7, 2017 21:19
Show Gist options
  • Save evantahler/9e168862fc452cc2191733258a8aad61 to your computer and use it in GitHub Desktop.
Save evantahler/9e168862fc452cc2191733258a8aad61 to your computer and use it in GitHub Desktop.
V17 to V18 cache ActionHero action
exports.cacheTest = {
name: 'cacheTest',
description: 'I will test the internal cache functions of the API',
inputs: {
key: {
required: true,
formatter: function (s) { return String(s) }
},
value: {
required: true,
formatter: function (s) { return String(s) },
validator: function (s) {
if (s.length < 3) {
return '`value` should be at least 3 letters long'
} else { return true }
}
}
},
run: function (api, data, next) {
const key = 'cacheTest_' + data.params.key
const value = data.params.value
data.response.cacheTestResults = {}
api.cache.save(key, value, 5000, function (error, resp) {
if (error) { return next(error) }
data.response.cacheTestResults.saveResp = resp
api.cache.size(function (error, numberOfCacheObjects) {
if (error) { return next(error) }
data.response.cacheTestResults.sizeResp = numberOfCacheObjects
api.cache.load(key, function (error, resp, expireTimestamp, createdAt, readAt) {
if (error) { return next(error) }
data.response.cacheTestResults.loadResp = {
key: key,
value: resp,
expireTimestamp: expireTimestamp,
createdAt: createdAt,
readAt: readAt
}
api.cache.destroy(key, function (error, resp) {
data.response.cacheTestResults.deleteResp = resp
next(error)
})
})
})
})
}
}
const {Action, api} = require('actionhero')
module.exports = class CacheTest extends Action {
constructor () {
super()
this.name = 'cacheTest'
this.description = 'I will test the internal cache functions of the API'
}
inputs () {
return {
key: {
required: true,
formatter: this.stringFormatter,
validator: this.stringValidator
},
value: {
required: true,
formatter: this.stringFormatter,
validator: this.stringValidator
}
}
}
stringFormatter (s) {
return String(s)
}
stringValidator (s) {
if (s.length < 3) {
throw new Error('inputs should be at least 3 letters long')
} else {
return true
}
}
async run ({params, response}) {
const key = 'cacheTest_' + params.key
const value = params.value
response.cacheTestResults = {
saveResp: await api.cache.save(key, value, 5000),
sizeResp: await api.cache.size(),
loadResp: await api.cache.load(key),
deleteResp: await api.cache.destroy(key)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment