diff --git a/app.js b/app.js index 6eb0849..d9b8ab6 100644 --- a/app.js +++ b/app.js @@ -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); @@ -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; @@ -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); } @@ -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))) { diff --git a/package.json b/package.json index 41b8d5b..237d578 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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",