diff js/modules/cache.js @ 93:00d23d6d41b4

made cache module into cache/html5 and a factory function.
author Atul Varma <avarma@mozilla.com>
date Mon, 26 Apr 2010 19:02:23 -0700
parents e3de1fe32f40
children
line wrap: on
line diff
--- a/js/modules/cache.js	Mon Apr 26 18:48:58 2010 -0700
+++ b/js/modules/cache.js	Mon Apr 26 19:02:23 2010 -0700
@@ -1,33 +1,34 @@
-Require.modules["cache"] = function(exports, require) {
-  const CACHE_NAME = "cache";
-
-  var storage = require("storage");
-  var cache = storage.getItem(CACHE_NAME);
+Require.modules["cache/html5"] = function(exports, require) {
+  exports.create = function create(name, storage) {
+    var cache = storage.getItem(name);
 
-  if (cache)
-    cache = JSON.parse(cache);
-  else
-    cache = {};
+    if (cache)
+      cache = JSON.parse(cache);
+    else
+      cache = {};
 
-  exports.set = function set(key, value) {
-    cache[key] = value;
+    return {
+      set: function set(key, value) {
+        cache[key] = value;
 
-    // Remove the key first, to get around a strange iPad
-    // issue: http://stackoverflow.com/questions/2603682/is-anyone-else-receiving-a-quota-exceeded-err-on-their-ipad-when-accessing-locals
-    storage.removeItem(CACHE_NAME);
+        // Remove the key first, to get around a strange iPad
+        // issue: http://stackoverflow.com/questions/2603682/is-anyone-else-receiving-a-quota-exceeded-err-on-their-ipad-when-accessing-locals
+        storage.removeItem(name);
+
+        // TODO: We should really catch QUOTA_EXCEEDED_ERR here,
+        // which could be thrown if the user is in private
+        // browsing mode.
+        storage.setItem(name, JSON.stringify(cache));
+      },
 
-    // TODO: We should really catch QUOTA_EXCEEDED_ERR here,
-    // which could be thrown if the user is in private
-    // browsing mode.
-    storage.setItem(CACHE_NAME, JSON.stringify(cache));
-  };
+      get: function get(key) {
+        return cache[key];
+      },
 
-  exports.get = function get(key) {
-    return cache[key];
-  };
-
-  exports.clear = function clear() {
-    storage.removeItem(CACHE_NAME);
-    cache = {};
+      clear: function clear() {
+        storage.removeItem(name);
+        cache = {};
+      }
+    };
   };
 };