Skip to content

Commit

Permalink
Replace existing caching with CacheGoose, make sure we invalidate ite…
Browse files Browse the repository at this point in the history
…m query when updating (openhab#289)

Signed-off-by: digitaldan <[email protected]>
  • Loading branch information
digitaldan authored Feb 24, 2020
1 parent 56c6c78 commit e37d0a6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
21 changes: 13 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,15 @@ var flash = require('connect-flash'),

// MongoDB connection settings
var mongoose = require('mongoose');
var cacheOpts = {
max: 5000,
maxAge: 1000 * 60 * 10
};

require('mongoose-cache').install(mongoose, cacheOpts);
// MongoDB Caching for Item updates
var cachegoose = require('cachegoose');
cachegoose(mongoose, {
engine: 'redis',
port: config.redis.port,
host: config.redis.host,
password: config.redis.password,
});
var cacheTTL = config.cacheTTL || 600;

// Try to setup a mongodb connection, otherwise stopping
var mongoConnect = new MongoConnect(system);
Expand Down Expand Up @@ -764,7 +767,7 @@ io.sockets.on('connection', function (socket) {
logger.info('openHAB-cloud: Item ' + itemName + ' status.length (' + (itemStatus ? itemStatus.length : 'null') + ') is too big or null, ignoring update');
return;
}
Openhab.findById(self.openhabId).cache().exec(function (error, openhab) {
Openhab.findById(self.openhabId).cache(cacheTTL).exec(function (error, openhab) {
if (error) {
logger.warn('openHAB-cloud: Unable to find openHAB for itemUpdate: ' + error);
return;
Expand All @@ -774,10 +777,11 @@ io.sockets.on('connection', function (socket) {
return;
}
// Find the item (which should belong to this openhab)
var cacheKey = openhab.id + '-' + itemName;
Item.findOne({
openhab: openhab.id,
name: itemName
}).cache().exec(function (error, itemToUpdate) {
}).cache(cacheTTL, cacheKey).exec(function (error, itemToUpdate) {
if (error) {
logger.warn('openHAB-cloud: Unable to find item for itemUpdate: ' + error);
}
Expand Down Expand Up @@ -805,6 +809,7 @@ io.sockets.on('connection', function (socket) {
if (error) {
logger.error('openHAB-cloud: Error saving item: ' + error);
}
cachegoose.clearCache(cacheKey);
});
// Check if the new state is int or float to store it to Number and create new item update event
if (!isNaN(parseFloat(itemStatus))) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"bcrypt-cache": "^1.0.2",
"bcrypt": "1.0.2",
"body-parser": "^1.17.2",
"cachegoose": "8.0.0",
"chokidar": "0.6.2",
"connect-flash": "0.1.1",
"connect-mongodb": "1.1.5",
Expand All @@ -35,7 +36,6 @@
"method-override": "^2.3.9",
"moment": "2.20.1",
"mongoose": "5.7.6",
"mongoose-cache": "0.1.4",
"mongoose-types": "1.0.3",
"morgan": "^1.8.2",
"node-gcm": "0.14.4",
Expand Down

0 comments on commit e37d0a6

Please sign in to comment.