comparison js/modules/cache.js @ 81:e3de1fe32f40

made cache module require 'storage' instead of 'window'
author Atul Varma <avarma@mozilla.com>
date Sun, 25 Apr 2010 22:44:00 -0700
parents 4bb45ff5788a
children 00d23d6d41b4
comparison
equal deleted inserted replaced
80:677df912e92d 81:e3de1fe32f40
1 Require.modules["cache"] = function(exports, require) { 1 Require.modules["cache"] = function(exports, require) {
2 const CACHE_NAME = "cache"; 2 const CACHE_NAME = "cache";
3 3
4 var window = require("window"); 4 var storage = require("storage");
5 var cache = window.localStorage.getItem(CACHE_NAME); 5 var cache = storage.getItem(CACHE_NAME);
6 6
7 if (cache) 7 if (cache)
8 cache = JSON.parse(cache); 8 cache = JSON.parse(cache);
9 else 9 else
10 cache = {}; 10 cache = {};
12 exports.set = function set(key, value) { 12 exports.set = function set(key, value) {
13 cache[key] = value; 13 cache[key] = value;
14 14
15 // Remove the key first, to get around a strange iPad 15 // Remove the key first, to get around a strange iPad
16 // issue: http://stackoverflow.com/questions/2603682/is-anyone-else-receiving-a-quota-exceeded-err-on-their-ipad-when-accessing-locals 16 // issue: http://stackoverflow.com/questions/2603682/is-anyone-else-receiving-a-quota-exceeded-err-on-their-ipad-when-accessing-locals
17 window.localStorage.removeItem(CACHE_NAME); 17 storage.removeItem(CACHE_NAME);
18 18
19 // TODO: We should really catch QUOTA_EXCEEDED_ERR here, 19 // TODO: We should really catch QUOTA_EXCEEDED_ERR here,
20 // which could be thrown if the user is in private 20 // which could be thrown if the user is in private
21 // browsing mode. 21 // browsing mode.
22 window.localStorage.setItem(CACHE_NAME, JSON.stringify(cache)); 22 storage.setItem(CACHE_NAME, JSON.stringify(cache));
23 }; 23 };
24 24
25 exports.get = function get(key) { 25 exports.get = function get(key) {
26 return cache[key]; 26 return cache[key];
27 }; 27 };
28 28
29 exports.clear = function clear() { 29 exports.clear = function clear() {
30 window.localStorage.removeItem(CACHE_NAME); 30 storage.removeItem(CACHE_NAME);
31 cache = {}; 31 cache = {};
32 }; 32 };
33 }; 33 };