-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache Mongo-DB calls (in-memory and/or Redis) #926
Draft
jason-fox
wants to merge
29
commits into
telefonicaid:master
Choose a base branch
from
jason-fox:feature/cache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
4e6f9f1
Cache Mongo-DB calls.
jason-fox 7872370
Linting - move functions.
jason-fox c2a7ea0
Merge branch 'master' into feature/cache
jason-fox 9471f42
Make Caching configurable.
jason-fox 7a8edc8
Add documentation
jason-fox d0fe4f8
Amend documentation.
jason-fox 5d08c0e
Tidying up code and tests.
jason-fox 6b6dba7
Merge branch 'master' into feature/cache
jason-fox 3c1190c
Merge commit 'master' into feature/cache
jason-fox 60d7dca
Complete linting using ESLint.
jason-fox 56c8023
Merge branch 'master' into feature/cache
jason-fox 90d1318
Merge branch 'master' into feature/cache
jason-fox acad317
Fixing merge
jason-fox 4b7a08a
Adding Redis Cache
jason-fox b0daf0a
Adding Start-up test for Redis and Memcache
jason-fox 5eaef79
Updating Docs
jason-fox 94bc29e
Merge branch 'master' into feature/cache
jason-fox 76dcd15
Remove debug
jason-fox 7d88f72
Merge branch 'master' into feature/cache
jason-fox bb0c277
Merge branch 'master' into feature/cache
jason-fox 55e017b
Merge branch 'master' into feature/cache
jason-fox a0b1dd8
Merge branch 'master' into feature/cache
jason-fox 880f164
Merge branch 'master' into feature/cache
jason-fox cfdd819
Merge branch 'master' into feature/cache
jason-fox d39a90b
Merge branch 'master' into feature/cache
jason-fox e98c02e
Set cache rather than don't cache
jason-fox 7753fec
Delete file.
jason-fox f3d6622
Merge branch 'master' into feature/cache
jason-fox 12f9bcb
Merge branch 'master' into feature/cache
jason-fox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,18 +21,45 @@ | |
* please contact with::[email protected] | ||
*/ | ||
|
||
var logger = require('logops'), | ||
dbService = require('../../model/dbConn'), | ||
config = require('../../commonConfig'), | ||
fillService = require('./../common/domain').fillService, | ||
alarmsInt = require('../common/alarmManagement').intercept, | ||
errors = require('../../errors'), | ||
constants = require('../../constants'), | ||
Device = require('../../model/Device'), | ||
async = require('async'), | ||
context = { | ||
op: 'IoTAgentNGSI.MongoDBDeviceRegister' | ||
}; | ||
const logger = require('logops'); | ||
const dbService = require('../../model/dbConn'); | ||
const config = require('../../commonConfig'); | ||
const fillService = require('./../common/domain').fillService; | ||
const alarmsInt = require('../common/alarmManagement').intercept; | ||
const errors = require('../../errors'); | ||
const constants = require('../../constants'); | ||
const Device = require('../../model/Device'); | ||
const async = require('async'); | ||
const cacheManager = require('cache-manager'); | ||
const memoryCache = cacheManager.caching({store: 'memory', max: 1000, ttl: 10/*seconds*/}); | ||
let context = { | ||
op: 'IoTAgentNGSI.MongoDBDeviceRegister' | ||
}; | ||
|
||
const attributeList = [ | ||
'id', | ||
'type', | ||
'name', | ||
'service', | ||
'subservice', | ||
'lazy', | ||
'commands', | ||
'staticAttributes', | ||
'active', | ||
'registrationId', | ||
'internalId', | ||
'internalAttributes', | ||
'resource', | ||
'apikey', | ||
'protocol', | ||
'endpoint', | ||
'transport', | ||
'polling', | ||
'timestamp', | ||
'autoprovision', | ||
'explicitAttrs', | ||
'expressionLanguage' | ||
]; | ||
|
||
/** | ||
* Generates a handler for the save device operations. The handler will take the customary error and the saved device | ||
|
@@ -52,20 +79,24 @@ function saveDeviceHandler(callback) { | |
}; | ||
} | ||
|
||
/** | ||
* Empties the memory cache | ||
*/ | ||
function clearCache(){ | ||
memoryCache.reset(); | ||
} | ||
|
||
/** | ||
* Create a new register for a device. The device object should contain the id, type and registrationId | ||
* | ||
* @param {Object} newDevice Device object to be stored | ||
*/ | ||
function storeDevice(newDevice, callback) { | ||
var deviceObj = new Device.model(), | ||
attributeList = ['id', 'type', 'name', 'service', 'subservice', 'lazy', 'commands', 'staticAttributes', | ||
'active', 'registrationId', 'internalId', 'internalAttributes', 'resource', 'apikey', 'protocol', | ||
'endpoint', 'transport', 'polling', 'timestamp', 'autoprovision', 'explicitAttrs', 'expressionLanguage']; | ||
const deviceObj = new Device.model(); | ||
|
||
for (var i = 0; i < attributeList.length; i++) { | ||
deviceObj[attributeList[i]] = newDevice[attributeList[i]]; | ||
} | ||
attributeList.forEach((key) => { | ||
deviceObj[key] = newDevice[key]; | ||
}); | ||
|
||
// Ensure protocol is in newDevice | ||
if ( !newDevice.protocol && config.getConfig().iotManager && config.getConfig().iotManager.protocol) { | ||
|
@@ -99,7 +130,7 @@ function storeDevice(newDevice, callback) { | |
* @param {String} subservice Subservice inside the service for the removed device. | ||
*/ | ||
function removeDevice(id, service, subservice, callback) { | ||
var condition = { | ||
const condition = { | ||
id: id, | ||
service: service, | ||
subservice: subservice | ||
|
@@ -114,7 +145,7 @@ function removeDevice(id, service, subservice, callback) { | |
callback(new errors.InternalDbError(error)); | ||
} else { | ||
logger.debug(context, 'Device [%s] successfully removed.', id); | ||
|
||
clearCache(); | ||
callback(null); | ||
} | ||
}); | ||
|
@@ -130,8 +161,8 @@ function removeDevice(id, service, subservice, callback) { | |
* @param {Number} offset Number of entries to skip for pagination. | ||
*/ | ||
function listDevices(type, service, subservice, limit, offset, callback) { | ||
var condition = {}, | ||
query; | ||
const condition = {}; | ||
let query; | ||
|
||
if (type) { | ||
condition.type = type; | ||
|
@@ -174,32 +205,36 @@ function listDevices(type, service, subservice, limit, offset, callback) { | |
* @param {String} subservice Division inside the service (optional). | ||
*/ | ||
function getDeviceById(id, service, subservice, callback) { | ||
var query, | ||
queryParams = { | ||
id: id, | ||
service: service, | ||
subservice: subservice | ||
}; | ||
let query; | ||
const queryParams = { | ||
id: id, | ||
service: service, | ||
subservice: subservice | ||
}; | ||
context = fillService(context, queryParams); | ||
logger.debug(context, 'Looking for device with id [%s].', id); | ||
|
||
query = Device.model.findOne(queryParams); | ||
query.select({__v: 0}); | ||
memoryCache.wrap(JSON.stringify(queryParams), function (cacheCallback) { | ||
query = Device.model.findOne(queryParams); | ||
query.select({__v: 0}); | ||
|
||
query.exec(function handleGet(error, data) { | ||
if (error) { | ||
logger.debug(context, 'Internal MongoDB Error getting device: %s', error); | ||
query.exec(function handleGet(error, data) { | ||
if (error) { | ||
logger.debug(context, 'Internal MongoDB Error getting device: %s', error); | ||
|
||
callback(new errors.InternalDbError(error)); | ||
} else if (data) { | ||
context = fillService(context, data); | ||
logger.debug(context, 'Device data found: %j', data); | ||
callback(null, data); | ||
} else { | ||
logger.debug(context, 'Device [%s] not found.', id); | ||
cacheCallback(new errors.InternalDbError(error)); | ||
} else if (data) { | ||
context = fillService(context, data); | ||
logger.debug(context, 'Device data found: %j', data); | ||
cacheCallback(null, data); | ||
} else { | ||
logger.debug(context, 'Device [%s] not found.', id); | ||
|
||
callback(new errors.DeviceNotFound(id)); | ||
} | ||
cacheCallback(new errors.DeviceNotFound(id)); | ||
} | ||
}); | ||
}, (error, data) => { | ||
callback(error, data); | ||
}); | ||
} | ||
|
||
|
@@ -222,7 +257,7 @@ function getDevice(id, service, subservice, callback) { | |
} | ||
|
||
function getByName(name, service, servicepath, callback) { | ||
var query; | ||
let query; | ||
context = fillService(context, { service: service, subservice: servicepath}); | ||
logger.debug(context, 'Looking for device with name [%s].', name); | ||
|
||
|
@@ -250,8 +285,9 @@ function getByName(name, service, servicepath, callback) { | |
} | ||
|
||
/** | ||
* Updates the given device into the database. Only the following attributes: lazy, active and internalId will be | ||
* updated. | ||
* Updates the given device into the database. Only the following attributes: | ||
* lazy, active and internalId, commanda, endpoint, name, type, explicitAttrs | ||
* will be updated. | ||
* | ||
* @param {Object} device Device object with the new values to write. | ||
*/ | ||
|
@@ -260,6 +296,7 @@ function update(device, callback) { | |
if (error) { | ||
callback(error); | ||
} else { | ||
clearCache(); | ||
data.lazy = device.lazy; | ||
data.active = device.active; | ||
data.internalId = device.internalId; | ||
|
@@ -269,7 +306,6 @@ function update(device, callback) { | |
data.name = device.name; | ||
data.type = device.type; | ||
data.explicitAttrs = device.explicitAttrs; | ||
|
||
data.save(saveDeviceHandler(callback)); | ||
} | ||
}); | ||
|
@@ -291,8 +327,8 @@ function itemToObject(i) { | |
} | ||
|
||
function getDevicesByAttribute(name, value, service, subservice, callback) { | ||
var query, | ||
filter = {}; | ||
let query; | ||
const filter = {}; | ||
|
||
if (service) { | ||
filter.service = service; | ||
|
@@ -324,6 +360,7 @@ function getDevicesByAttribute(name, value, service, subservice, callback) { | |
}); | ||
} | ||
|
||
|
||
exports.getDevicesByAttribute = alarmsInt(constants.MONGO_ALARM, getDevicesByAttribute); | ||
exports.store = alarmsInt(constants.MONGO_ALARM, storeDevice); | ||
exports.update = alarmsInt(constants.MONGO_ALARM, update); | ||
|
@@ -333,3 +370,4 @@ exports.get = alarmsInt(constants.MONGO_ALARM, getDevice); | |
exports.getSilently = getDevice; | ||
exports.getByName = alarmsInt(constants.MONGO_ALARM, getByName); | ||
exports.clear = alarmsInt(constants.MONGO_ALARM, clear); | ||
exports.clearCache = clearCache; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Magic numbers. MUST be config.
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.
In addition, I think the whole possibilty of not using cache (e.g. something like
cache: true|false
in configuration) should be provided.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.
Fixed 9471f42 and subsequent pushes.