Skip to content

Commit 504cd51

Browse files
committed
Use SessionStorage to store cache #3434
1 parent 1eb00fa commit 504cd51

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

webextensions/background/background-cache.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ function log(...args) {
3131
const kCONTENTS_VERSION = 5;
3232

3333
let mActivated = false;
34-
const mCaches = {};
3534

3635
export function activate() {
3736
mActivated = true;
@@ -302,9 +301,9 @@ async function updateWindowCache(owner, key, value) {
302301
if (!configs.persistCachedTree) {
303302
const storageKey = `backgroundCache-${await UniqueId.ensureWindowId(owner.windowId)}-${key}`;
304303
if (value)
305-
mCaches[storageKey] = value;
304+
sessionStorage.setItem(storageKey, value);
306305
else
307-
delete mCaches[storageKey];
306+
sessionStorage.removeItem(storageKey);
308307
return;
309308
}
310309

@@ -341,7 +340,7 @@ export function markWindowCacheDirtyFromTab(tab, akey) {
341340
async function getWindowCache(owner, key) {
342341
if (!configs.persistCachedTree) {
343342
const storageKey = `backgroundCache-${await UniqueId.ensureWindowId(owner.windowId)}-${key}`;
344-
return mCaches[storageKey];
343+
return sessionStorage.getItem(storageKey);
345344
}
346345
return browser.sessions.getWindowValue(owner.windowId, key).catch(ApiTabs.createErrorHandler());
347346
}
@@ -520,13 +519,13 @@ Tab.onHidden.addListener(tab => {
520519
browser.runtime.onMessage.addListener((message, _sender) => {
521520
switch (message && message.type) {
522521
case Constants.kCOMMAND_GET_ON_MEMORY_CACHE:
523-
return mCaches[message.key];
522+
return sessionStorage.getItem(message.key);
524523

525524
case Constants.kCOMMAND_SET_ON_MEMORY_CACHE:
526525
if (message.value)
527-
mCaches[message.key] = message.value;
526+
sessionStorage.setItem(message.key, message.value);
528527
else
529-
delete mCaches[message.key];
528+
sessionStorage.removeItem(message.key);
530529
return;
531530

532531
default:
@@ -536,9 +535,10 @@ browser.runtime.onMessage.addListener((message, _sender) => {
536535

537536
browser.windows.onRemoved.addListener(async windowId => {
538537
const storageKeyPart = `Cache-${await UniqueId.ensureWindowId(windowId)}-`;
539-
for (const key in mCaches) {
538+
for (let i = sessionStorage.length; i > -1; i--) {
539+
const key = sessionStorage.key(i);
540540
if (key.includes(storageKeyPart))
541-
delete mCaches[key];
541+
sessionStorage.removeItem(key);
542542
}
543543
});
544544

0 commit comments

Comments
 (0)