Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Api calls caching via redis - Closes #484 #577

Merged
merged 66 commits into from
Jun 8, 2017
Merged
Show file tree
Hide file tree
Changes from 65 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
a935984
added caching functionality basic structure
SargeKhan May 9, 2017
c3f6d5e
ignore swap files created by vi
SargeKhan May 10, 2017
5e18420
refined cache logic, created express middleware for caching
SargeKhan May 10, 2017
527daaa
updated router.map to support middleware, added caching for getBroadhash
SargeKhan May 11, 2017
739aab8
updated cache error handling, added unit tests for cache module
SargeKhan May 15, 2017
3843a40
removed some comments
SargeKhan May 15, 2017
2289adc
Merge branch '0.9.0' into 484-api_calls_caching
SargeKhan May 16, 2017
9ce548d
added cache options in config.json
SargeKhan May 16, 2017
7711523
Merge branch '484-api_calls_caching' of github.com:SargeKhan/lisk int…
SargeKhan May 16, 2017
232b40e
added one more cache test
SargeKhan May 16, 2017
6ed4fc6
added db flushing middleware, updated error handling
SargeKhan May 16, 2017
9258f38
updated cache tests to skip when cacheEnable=false
SargeKhan May 16, 2017
9aaaa32
updated cache helper name
SargeKhan May 16, 2017
6256cb2
updated config.json, added middlware
SargeKhan May 18, 2017
2427a94
added cache config to test config file
SargeKhan May 18, 2017
b4be92b
Updated cache configuration key in tests
SargeKhan May 19, 2017
541b2a3
added space, removed router.map changes
SargeKhan May 21, 2017
5b9eb6d
added test cases for blocks, transaction and delegates GET caching
SargeKhan May 22, 2017
f7f17db
added cache flushing on new block, delegate trs and round finish
SargeKhan May 22, 2017
cf023de
refactored cache tests, closed cache connection on tests completion
SargeKhan May 22, 2017
1713b90
added test for caching flushing on new block
SargeKhan May 22, 2017
8b6cffa
Merge branch '484-api_calls_caching' of github.com:SargeKhan/lisk int…
SargeKhan May 22, 2017
faa2192
removed flushCache middleware
SargeKhan May 23, 2017
ffe39ac
reverted router.map
SargeKhan May 26, 2017
a6f11e0
added comments, closed connection on cleanup
SargeKhan May 26, 2017
aed1314
added test for new round
SargeKhan May 26, 2017
2ad988b
updated tests to resolve successfully
SargeKhan May 26, 2017
6e95945
add redis service on travis
SargeKhan May 26, 2017
8b8ca31
removed itIfCacheenabled, set cacheEnabled to true
SargeKhan May 29, 2017
8cf680f
updated listeners to del their respective subset of cache entries
SargeKhan May 30, 2017
f2e6ca3
fixed test completion bug
SargeKhan May 30, 2017
1d98768
updated logs on cache clearing
SargeKhan May 30, 2017
044eb11
disabled cache while syncing, reordered funcs in modules/cache
SargeKhan May 30, 2017
1c91295
added further unit test cases
SargeKhan May 30, 2017
07e0a46
resolved module conflicts
SargeKhan May 31, 2017
ae3111f
fixed mispellings
SargeKhan May 31, 2017
48b0457
updated redis server port
SargeKhan May 31, 2017
bddbd89
removed cache monitoring
SargeKhan May 31, 2017
4f67bf6
updated log messages
SargeKhan May 31, 2017
a8fb6b9
updating travis redis port
SargeKhan May 31, 2017
06c9b3d
updating travis redis to run in background
SargeKhan May 31, 2017
44afeff
disable all redis features when syncing
SargeKhan May 31, 2017
6dcf5d6
updated redis config structure
SargeKhan May 31, 2017
9998671
test cache config. updated
SargeKhan May 31, 2017
3044b15
added test cases for disabling cache when syncing
SargeKhan Jun 1, 2017
ebfddd0
fixed typo
SargeKhan Jun 1, 2017
a2bf761
updated callbackCalled to a more descriptive name
SargeKhan Jun 1, 2017
d9a3978
refactored to remove nesting level
SargeKhan Jun 1, 2017
b9500a4
wait for cache.quit to complete before proceeding
SargeKhan Jun 1, 2017
ecc6fab
triggered clear cache after saving delegate trs in block
SargeKhan Jun 2, 2017
edac1ce
fixed inconsistent name
SargeKhan Jun 2, 2017
096c47d
fixed indentation
SargeKhan Jun 2, 2017
03a2c8d
disabled cache on default
SargeKhan Jun 2, 2017
bd6ad04
replaced err with null
SargeKhan Jun 5, 2017
a2ba561
Merge branch '0.9.0' into 484-api_calls_caching
4miners Jun 6, 2017
2b4507e
updated cache logs status from info to debug
SargeKhan Jun 6, 2017
aa098d2
Merge branch '484-api_calls_caching' of github.com:SargeKhan/lisk int…
SargeKhan Jun 6, 2017
5bc07e8
required redis related fields in schema
SargeKhan Jun 6, 2017
488d55b
add callbacks for all events listners in cache module
SargeKhan Jun 6, 2017
79cc021
add callbacks for all events listners in cache module
SargeKhan Jun 6, 2017
19fb17d
remove timeouts, use callbacks for events
SargeKhan Jun 6, 2017
029ff44
updated default cb definition
SargeKhan Jun 6, 2017
98aee83
removed orphan comment
SargeKhan Jun 6, 2017
70539a2
added docs for functions
SargeKhan Jun 6, 2017
7947002
added some missing comments
SargeKhan Jun 6, 2017
1d5c273
Merge branch '0.9.0' into 484-api_calls_caching
4miners Jun 8, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ stacktrace*
test/.coverage-unit
tmp
sftp-config.json
*.swp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fo what we need those ignored?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my editor generates while editing, I don't think we'd be needing these file types. So, I think it's safe to ignore them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, fine.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fine with me.

*.swo
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ cache:
- node_modules
services:
- postgresql
- redis-server
addons:
postgresql: '9.6'
install:
Expand All @@ -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 &
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good thanks

- node app.js &> .app.log &
notifications:
webhooks: https://coveralls.io/webhook?repo_token=l6rLvPBYHIwA92FQQmCUUTLI4zPuS4r5C
Expand Down
7 changes: 6 additions & 1 deletion api/http/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ var httpApi = require('../../helpers/httpApi');
* @param {scope} app - Network app.
*/
// Constructor
function BlocksHttpApi (blocksModule, app) {
function BlocksHttpApi (blocksModule, app, logger, cache) {

var router = new Router();

// attach a middlware to endpoints
router.attachMiddlwareForUrls(httpApi.middleware.useCache.bind(null, logger, cache), [
'get /'
]);

router.map(blocksModule.shared, {
'get /get': 'getBlock',
'get /': 'getBlocks',
Expand Down
5 changes: 4 additions & 1 deletion api/http/delegates.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ var httpApi = require('../../helpers/httpApi');
* @param {scope} app - Network app.
*/
// Constructor
function DelegatesHttpApi (delegatesModule, app) {
function DelegatesHttpApi (delegatesModule, app, logger, cache) {

var router = new Router();

// attach a middlware to endpoints
router.attachMiddlwareForUrls(httpApi.middleware.useCache.bind(null, logger, cache), ['get /']);

router.map(delegatesModule.shared, {
'get /count': 'count',
'get /search': 'search',
Expand Down
7 changes: 6 additions & 1 deletion api/http/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ var httpApi = require('../../helpers/httpApi');
* @param {scope} app - Network app.
*/
// Constructor
function TransactionsHttpApi (transactionsModule, app) {
function TransactionsHttpApi (transactionsModule, app, logger, cache) {

var router = new Router();

// attach a middlware to endpoints
router.attachMiddlwareForUrls(httpApi.middleware.useCache.bind(null, logger, cache), [
'get /'
]);

router.map(transactionsModule.shared, {
'get /': 'getTransactions',
'get /get': 'getTransaction',
Expand Down
2 changes: 1 addition & 1 deletion api/http/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var schema = require('../../schema/transport');
* @param {function} logger
*/
// Constructor
function TransportHttpApi (transportModule, app, logger) {
function TransportHttpApi (transportModule, app, logger, cache) {

var router = new Router();

Expand Down
21 changes: 15 additions & 6 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ process.env.TOP = appConfig.topAccounts;
*/
var config = {
db: appConfig.db,
cache: appConfig.redis,
cacheEnabled: appConfig.cacheEnabled,
modules: {
server: './modules/server.js',
accounts: './modules/accounts.js',
Expand All @@ -135,7 +137,8 @@ var config = {
multisignatures: './modules/multisignatures.js',
dapps: './modules/dapps.js',
crypto: './modules/crypto.js',
sql: './modules/sql.js'
sql: './modules/sql.js',
cache: './modules/cache.js'
},
api: {
accounts: { http: './api/http/accounts.js' },
Expand Down Expand Up @@ -431,12 +434,18 @@ d.run(function () {
};
cb(null, new bus());
}],

db: function (cb) {
var db = require('./helpers/database.js');
db.connect(config.db, logger, cb);
},

/**
* It tries to connect with redis server based on config. provided in config.json file
* @param {function} cb
*/
cache: function (cb) {
var cache = require('./helpers/cache.js');
cache.connect(config.cacheEnabled, config.cache, logger, cb);
},
/**
* Once db, bus, schema and genesisblock are completed,
* loads transaction, block, account and peers from logic folder.
Expand Down Expand Up @@ -486,7 +495,6 @@ d.run(function () {
}
}, cb);
}],

/**
* Once network, connect, config, logger, bus, sequence,
* dbSequence, balancesSequence, db and logic are completed,
Expand All @@ -496,7 +504,8 @@ d.run(function () {
* at leats will contain the required elements.
* @param {nodeStyleCallback} cb - Callback function with resulted load.
*/
modules: ['network', 'connect', 'config', 'logger', 'bus', 'sequence', 'dbSequence', 'balancesSequence', 'db', 'logic', function (scope, cb) {
modules: ['network', 'connect', 'config', 'logger', 'bus', 'sequence', 'dbSequence', 'balancesSequence', 'db', 'logic', 'cache', function (scope, cb) {

var tasks = {};

Object.keys(config.modules).forEach(function (name) {
Expand Down Expand Up @@ -535,7 +544,7 @@ d.run(function () {
var apiEndpointPath = config.api[moduleName][protocol];
try {
var ApiEndpoint = require(apiEndpointPath);
new ApiEndpoint(scope.modules[moduleName], scope.network.app, scope.logger);
new ApiEndpoint(scope.modules[moduleName], scope.network.app, scope.logger, scope.modules.cache);
} catch (e) {
scope.logger.error('Unable to load API endpoint for ' + moduleName + ' of ' + protocol, e);
}
Expand Down
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"consoleLogLevel": "info",
"trustProxy": false,
"topAccounts": false,
"cacheEnabled": false,
"db": {
"host": "localhost",
"port": 5432,
Expand All @@ -21,6 +22,13 @@
"error"
]
},
"redis": {
"host": "127.0.0.1",
"port": 6380,
"db": 0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good

"user": "",
"password": ""
},
"api": {
"enabled": true,
"access": {
Expand Down
38 changes: 38 additions & 0 deletions helpers/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

var redis = require('redis');

/**
* Connects with redis server using the config provided via parameters
* @param {Boolean} cacheEnabled
* @param {Object} config - Redis configuration
* @param {Object} logger
* @param {Function} cb
*/
module.exports.connect = function (cacheEnabled, config, logger, cb) {
var isRedisLoaded = false;

Copy link
Contributor

@4miners 4miners Jun 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That line can be removed

if (!cacheEnabled) {
return cb(null, { cacheEnabled: cacheEnabled, client: null });
}

var client = redis.createClient(config);

client.on('connect', function () {
logger.info('App connected with redis server');

if (!isRedisLoaded) {
isRedisLoaded = true;
return cb(null, { cacheEnabled: cacheEnabled, client: client });
}
});

client.on('error', function (err) {
logger.error('Redis:', err);
// Only throw an error if cache was enabled in config but were unable to load it properly
if (!isRedisLoaded) {
isRedisLoaded = true;
return cb('Unable to connect to redis server', { cacheEnabled: cacheEnabled, client: null });
}
});
};
33 changes: 33 additions & 0 deletions helpers/httpApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,39 @@ var middleware = {
attachResponseHeaders: function (getHeaders, req, res, next) {
res.set(getHeaders());
return next();
},

/**
* Lookup cache, and reply with cached response if it's a hit.
* If it's a miss, forward the request but cache the response if it's a success.
* @param {Object} req
* @param {Object} res
* @param {Function} next
*/
useCache: function (logger, cache, req, res, next) {
if (!cache.isReady()) {
return next();
}

var key = req.originalUrl;
cache.getJsonForKey(key, function (err, cachedValue) {
//there was an error or value doesn't exist for key
if (err || !cachedValue) {
// Monkey patching res.json function only if we expect to cache response
var expressSendJson = res.json;
res.json = function (response) {
if (response.success) {
logger.debug('cached response for key: ', req.url);
cache.setJsonForKey(key, response);
}
expressSendJson.call(res, response);
};
next();
} else {
logger.debug(['serving response for url:', req.url, 'from cache'].join(' '));
res.json(cachedValue);
}
});
}
};

Expand Down
17 changes: 16 additions & 1 deletion helpers/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ var extend = require('extend');
* @returns {Object} router express
* @throws {Error} If config is invalid
*/

var Router = function () {
var router = require('express').Router();

Expand All @@ -34,8 +33,24 @@ var Router = function () {
});
});
};
/**
* Adds one middleware to an array of routes.
* @param {Function} middleware
* @param {String} routes
*/
router.attachMiddlwareForUrls = function (middleware, routes) {
routes.forEach(function (entry) {
var route = entry.split(' ');

if (route.length !== 2 || ['post', 'get', 'put'].indexOf(route[0]) === -1) {
throw Error('Invalid map config');
}
router[route[0]](route[1], middleware);
});
};

return router;

};

module.exports = Router;
1 change: 1 addition & 0 deletions modules/blocks/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Chain.prototype.saveBlock = function (block, cb) {
* @return {Object} cb.err Error if occurred
*/
__private.afterSave = function (block, cb) {
library.bus.message('transactionsSaved', block.transactions);
// Execute afterSave callbacks for each transaction, depends on tx type
// see: logic.outTransfer.afterSave, logic.dapp.afterSave
async.eachSeries(block.transactions, function (transaction, cb) {
Expand Down
Loading