Permalink
Browse files

Caching Implementation per browser session.

Instead of fetching the contents from XHR everysingle time, it will
only fetch once and reuse it from the cache. I am not afraid of the
cache to increase because it is just couple of posts. Later on
we will add a cleanup stage.
  • Loading branch information...
mohamedmansour committed Oct 29, 2011
1 parent 64faa16 commit a0def58d8f2f71d86d02a387699d64eecf1410aa
Showing with 18 additions and 4 deletions.
  1. +17 −3 js/background_controller.js
  2. +1 −1 manifest.json
@@ -6,6 +6,7 @@
*/
BackgroundController = function() {
this.onExtensionLoaded();
+ this._embedCache = {};
};
/**
@@ -51,9 +52,22 @@ BackgroundController.prototype.init = function() {
chrome.extension.onRequest.addListener(this.onMessage.bind(this));
};
+/**
+ * Message Passing Listener.
+ */
BackgroundController.prototype.onMessage = function(request, sender, response) {
if (request.method == 'GetEmbedHTML') {
- this.getEmbedHTML(request.data.domain, request.data.url, response);
+ var url = request.data.url;
+ var cachedHTML = this._embedCache[url];
+ if (cachedHTML) {
+ response({data: cachedHTML});
+ }
+ else {
+ this.getEmbedHTML(request.data.domain, url, function(html) {
+ this._embedCache[url] = html;
+ response({data: html});
+ }.bind(this));
+ }
}
};
@@ -80,14 +94,14 @@ BackgroundController.prototype.getEmbedHTML = function(domain, url, callback) {
if (domain == 'soundcloud') {
html = html.replace(/http:\/\//g, 'https://');
}
- callback({data: html});
+ callback(html);
break;
case 'photo':
var html = '<p><strong>' + response.title + '</strong></p><a href="' +
response.author_url + '">' + '<img src="' + response.url +
'" alt="' + response.title + '" width="' + response.width +
'" height="' + response.height + '"/></a>'
- callback({data: html});
+ callback(html);
break;
}
}
View
@@ -1,6 +1,6 @@
{
"name": "Extended Embed BETA for Google Plus",
- "version": "0.0.8",
+ "version": "1.0.0",
"description": "Automatically embed a rich content in Google+ such as flickr, soundcloud, hulu, slideshare, smugmug, and revision3.",
"icons": {
"16": "img/icon16.png",

0 comments on commit a0def58

Please sign in to comment.