Permalink
Browse files

Added caching of content blocks for speed enhancement.

  • Loading branch information...
1 parent c5a91ca commit 5aea2d5d557f9c47e12c1726e6d2cef5d362fa4c @primaryobjects committed Apr 4, 2013
Showing with 57 additions and 27 deletions.
  1. +46 −21 lib/contentblocks.js
  2. +7 −3 lib/managers/ControllerManager.js
  3. +4 −3 package.json
View
@@ -1,6 +1,7 @@
var cheerio = require('cheerio'),
commonManager = require('./managers/CommonManager'),
- controllerManager = require('./managers/ControllerManager');
+ controllerManager = require('./managers/ControllerManager'),
+ cache = require('memory-cache');
module.exports = function (params) {
if (params.app == null || params.host == null || params.pathFind == null || params.pathPost == null || params.pathPut == null) {
@@ -67,30 +68,33 @@ module.exports = function (params) {
// Process a CMS element.
var contentBlockItem = contentBlockItems[index];
- // Load the CMS document from the database.
- WebManager.send(req, res, contentBlockItem.host, contentBlockItem.path, 'GET', function (result) {
- if (result != null && result.length > 0) {
- // Find all properties in the CMS element.
- var properties = contentBlockItem.contentBlock.find('[property]');
+ var result = cache.get(contentBlockItem.id);
+ if (result != null && result.length > 0) {
+ // Populate the html content from cache.
+ ContentBlockManager.processHtml(contentBlockItem, options, result);
- // Add each property, along with its CMS element id, to the session variable, options.
- properties.each(function (i, elem) {
- var property = $(this);
- var propertyId = property.attr('property');
-
- // Get the value to display from the database document.
- var val = CommonManager.decodeId(result[0][CommonManager.encodeId('<http://viejs.org/ns/' + propertyId + '>')])
-
- // console.log('Found: <http://viejs.org/ns/' + propertyId + '> = ' + val);
-
- // Add the resulting id/value to the session variable, options.
- options[contentBlockItem.id.replace(/</, '').replace(/>/, '') + '_' + propertyId] = val;
- });
- }
+ // console.log('Loaded ' + contentBlockItem.id + ' from cache.');
// Recurse to the next CMS element.
webManagerSendSync(contentBlockItems, index + 1);
- });
+ }
+ else {
+ // Load the CMS document from the database.
+ WebManager.send(req, res, contentBlockItem.host, contentBlockItem.path, 'GET', function (result) {
+ if (result != null && result.length > 0) {
+ // Cache the result.
+ cache.put(contentBlockItem.id, result);
+
+ // Populate the html content.
+ ContentBlockManager.processHtml(contentBlockItem, options, result);
+
+ // console.log('Loaded ' + contentBlockItem.id + ' from database.');
+ }
+
+ // Recurse to the next CMS element.
+ webManagerSendSync(contentBlockItems, index + 1);
+ });
+ }
}
else {
// Render to the browser.
@@ -109,3 +113,24 @@ module.exports = function (params) {
}
}
};
+
+ContentBlockManager = {
+ processHtml: function (contentBlockItem, options, result) {
+ // Find all properties in the CMS element.
+ var properties = contentBlockItem.contentBlock.find('[property]');
+
+ // Add each property, along with its CMS element id, to the session variable, options.
+ properties.each(function (i, elem) {
+ var property = $(this);
+ var propertyId = property.attr('property');
+
+ // Get the value to display from the database document.
+ var val = CommonManager.decodeId(result[0][CommonManager.encodeId('<http://viejs.org/ns/' + propertyId + '>')])
+
+ // console.log('Found: <http://viejs.org/ns/' + propertyId + '> = ' + val);
+
+ // Add the resulting id/value to the session variable, options.
+ options[contentBlockItem.id.replace(/</, '').replace(/>/, '') + '_' + propertyId] = val;
+ });
+ }
+};
@@ -1,9 +1,10 @@
-var webManager = require('./WebManager');
+var webManager = require('./WebManager'),
+ cache = require('memory-cache');
ControllerManager = {
setup: function (app, host, pathPost, pathPut, pathDelete) {
- app.post('/create', function (req, res) { ControllerManager.insert(req, res, host, pathPost) });
- app.put('/create/:id', function (req, res) { ControllerManager.update(req, res, host, pathPut) });
+ app.post('/create', function (req, res) { ControllerManager.insert(req, res, host, pathPost) });
+ app.put('/create/:id', function (req, res) { ControllerManager.update(req, res, host, pathPut) });
app.delete('/create/:id', function (req, res) { ControllerManager.delete(req, res, host, pathDelete) });
},
@@ -15,6 +16,9 @@ ControllerManager = {
update: function (req, res, host, path) {
WebManager.send(req, res, host, path.replace('[id]', req.params['id']), 'PUT', function (result) {
+ // Clear the cache for this block.
+ cache.del(result.document['@subject']);
+
res.json(result, 200);
});
},
View
@@ -1,6 +1,6 @@
{
"name": "contentblocks",
- "version": "0.1.1",
+ "version": "0.1.2",
"description": "Create simple editable CMS content blocks in node.js. Wrapper for Create.js CMS framework.",
"author": {
"name": "Kory Becker",
@@ -17,7 +17,8 @@
"node": ">= 0.8.x",
"cheerio": "*",
"easypost": "*",
- "http": "*"
+ "http": "*",
+ "node-cache": "*"
},
"licenses": [
{
@@ -37,6 +38,6 @@
"hallo",
"aloha"
],
- "_id": "contentblocks@0.1.1",
+ "_id": "contentblocks@0.1.2",
"_from": "contentblocks"
}

0 comments on commit 5aea2d5

Please sign in to comment.