Skip to content
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

Upgrading Mongo driver and cache-manager to make it work with Mongo 3.2 #10

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
prerender-mongodb-cache
=======================

Prerender plugin for MongoDB caching, to be used with the prerender node application from https://github.com/prerender/prerender.
Prerender plugin for MongoDB caching, to be used with the prerender node application from https://github.com/prerender/prerender. It relys on [cache-manager](https://github.com/BryanDonovan/node-cache-manager) and on the [mongo adapter](https://github.com/v4l3r10/node-cache-manager-mongodb)

How it works
------------

This plugin will store all prerendered pages into a MongoDB instance. There is currently no expiration functionality, which means that once a page is stored, future requests for prerendering a page will always be served from from the database cache if it's available and the page caches are never updated.

To get a fresh cache, you will have to delete the cache in the MongoDB instance manually or from another process.
To set the cache lifetime use the environment variable when launching prerender: `CACHE_TTL` (in seconds) by default it has been set to one month.

How to use
----------
Expand All @@ -19,11 +19,19 @@ In your local prerender project run:

Then in the server.js that initializes the prerender:

server.use(require('prerender-mongodb-cache'));
```js
server.use(require('prerender-mongodb-cache'));
```

Configuration
-------------

By default it will connect to your MongoDB instance running on localhost and use the *prerender* collection. You can overwrite this by setting the `MONGOLAB_URI` or `MONGOHQ_URL` environment variables to valid MongoDB connection strings.

This is done to make it work automatically when deployed on Heroku with the MongoDB add-ons.

Contributions
-------------

Creator: [lammertw](https://github.com/lammertw)
Contributor: [YouriT](https://github.com/YouriT)
99 changes: 63 additions & 36 deletions lib/mongoCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,85 @@ var mongoUri = process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost/prerender';

var database;
var ttl = process.env.CACHE_TTL || 2592000;

MongoClient.connect(mongoUri, function(err, db) {
database = db;
});

var cache_manager = require('cache-manager');
var cacheManager = require('cache-manager'),
mongoStore = require('cache-manager-mongodb'),
mongoCache;

module.exports = {
init: function() {
this.cache = cache_manager.caching({
store: mongo_cache
mongoCache = cacheManager.caching({
store: mongoStore,
uri: mongoUri,
options: {
collection: "pages",
compression: false,
server: {
poolSize: 5,
auto_reconnect: true
}
},
ttl: ttl
});
},

beforePhantomRequest: function(req, res, next) {
if(req.method !== 'GET') {

var currentTTL = req.headers['cache-ttl'] || ttl;

var url = req.url.replace(/%(25)+/gi, '%').replace(/%3f\_escaped\_fragment.*/gi, '');
var parts = req.url.split('/'),
lastPart = parts[parts.length - 1];
try {
url = url.replace(lastPart, decodeURIComponent(lastPart).replace(/\?\_escaped\_fragment.*/g, ''));
} catch (e) {};

if (req.method !== 'GET') {
return next();
}

this.cache.get(req.url, function (err, result) {
if (!err && result) {
res.send(200, result);
} else {
next();
}
});

try {
mongoCache.get(url, function(err, result) {
if (err) {
throw err;
}
if (!err && result) {
res.send(200, result);
} else {
next();
}
});
} catch (e) {
console.log(e.stack || e.message);
next();
}
},

afterPhantomRequest: function(req, res, next) {
this.cache.set(req.url, req.prerender.documentHTML);
next();
}
};

var currentTTL = req.headers['cache-ttl'] || ttl;

var mongo_cache = {
get: function(key, callback) {
database.collection('pages', function(err, collection) {
collection.findOne({key: key}, function (err, item) {
var value = item ? item.value : null;
callback(err, value);
});
});
},
set: function(key, value, callback) {
database.collection('pages', function(err, collection) {
var object = {key: key, value: value, created: new Date()};
collection.update({key: key}, object, {
upsert: true
}, function (err) {
var url = req.url.replace(/%(25)+/gi, '%').replace(/%3f\_escaped\_fragment.*/gi, '');
var parts = req.url.split('/'),
lastPart = parts[parts.length - 1];
try {
url = url.replace(lastPart, decodeURIComponent(lastPart).replace(/\?\_escaped\_fragment.*/g, ''));
} catch (e) {};

try {
mongoCache.set(url, req.prerender.documentHTML, {
ttl: currentTTL
}, function(err) {
if (err) {
throw err;
}
next();
});
});
} catch (e) {
console.log(e.stack || e.message);
next();
}
}
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"homepage": "https://github.com/lammertw/prerender-mongodb-cache",
"dependencies": {
"mongodb": "~2.1.0",
"cache-manager": "0.2.0"
"cache-manager": "^1.3.0",
"cache-manager-mongodb": "^0.1.6"
}
}