-
Notifications
You must be signed in to change notification settings - Fork 457
Api calls caching via redis - Closes #484 #577
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looking for the change to config.json to allow the flag to be toggled. Default option should be OFF or FALSE
test/common/initModule.js
Outdated
@@ -13,6 +13,8 @@ var database = require(path.join(dirname, '/helpers', 'database.js')); | |||
var genesisblock = require(path.join(dirname, '/genesisBlock.json')); | |||
var Logger = require(dirname + '/logger.js'); | |||
var z_schema = require('../../helpers/z_schema.js'); | |||
var cache = require('../../helpers/cache.js'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this naming is confusing, is there a better way? Cache
and cache
requires...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, having cache
and Cache
variable might be confusing. I'll update it to something like cacheHelper
.
@Isabello, I have those options in my config.json file, I'll push them and update the description of the pull request as well. |
…o 484-api_calls_caching
Thank you! Looks good. I will do another check over it in the morning |
helpers/httpApi.js
Outdated
@@ -176,6 +176,24 @@ var middleware = { | |||
} else { | |||
next(); | |||
} | |||
}, | |||
flushCache: function (logger, cache, req, res, next) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need req here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Express calls this function internally with (req, res, next)
parameters. So, (based on my knowledge), we need to specify req
parameter here.
config.json
Outdated
@@ -21,6 +22,10 @@ | |||
"error" | |||
] | |||
}, | |||
"cache": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this below API? Or even inside API since caching is related to API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this would definitely make more sense. I'll update this bit. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please ensure that the config.json in test/
has the correct schema :)
@@ -15,3 +15,5 @@ stacktrace* | |||
test/.coverage-unit | |||
tmp | |||
sftp-config.json | |||
*.swp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fine with me.
@@ -19,6 +20,7 @@ before_script: | |||
- tar -zxvf lisk-node-Linux-x86_64.tar.gz | |||
- cd test/lisk-js/; npm install; cd ../.. | |||
- cp test/config.json test/genesisBlock.json . | |||
- redis-server --port 6380 & |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good thanks
"redis": { | ||
"host": "127.0.0.1", | ||
"port": 6380, | ||
"db": 0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good
schema/config.js
Outdated
type: 'string' | ||
} | ||
} | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Require these fields.
@@ -38,6 +38,9 @@ module.exports = { | |||
topAccounts: { | |||
type: 'boolean' | |||
}, | |||
cacheEnabled: { | |||
type: 'boolean' | |||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Require this field
test/unit/modules/cache.js
Outdated
cache.setJsonForKey(key, value, function (err, status) { | ||
expect(err).to.not.exist; | ||
expect(status).to.equal('OK'); | ||
//this function doesn't accept a callback, so waiting 2 seconds before checking if the actions were completed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
optional cb should be ok, waiting on timeout makes tests fragile
modules/cache.js
Outdated
if(!self.isReady()) { return; } | ||
async.map(['/api/blocks*', '/api/transactions*'], function (pattern) { | ||
Cache.prototype.onNewBlock = function (block, broadcast, cb) { | ||
cb = cb ? cb: function () {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only call callback in one place so if (cb) { return cb(); }
is more readable. If we want to keep current approach cb = cb || function () {};
is shorter and cleaner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use cb in multiple places based on certain conditions in other functions. So, using the second approach which you suggested. But good advice. Thanks 👍
modules/cache.js
Outdated
@@ -16,14 +21,28 @@ function Cache (cb, scope) { | |||
setImmediate(cb, null, self); | |||
} | |||
|
|||
/* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/**
, globally
This pull request introduces 2 new fields in
config.json
file.cacheEnabled
field for enabling/disabling cache.cache
field for specifying the configuration of Redis cache.Closes #484