Skip to content

Commit

Permalink
Use SessionStorage to store cache #3434
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Jan 23, 2024
1 parent 1eb00fa commit 504cd51
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions webextensions/background/background-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ function log(...args) {
const kCONTENTS_VERSION = 5;

let mActivated = false;
const mCaches = {};

export function activate() {
mActivated = true;
Expand Down Expand Up @@ -302,9 +301,9 @@ async function updateWindowCache(owner, key, value) {
if (!configs.persistCachedTree) {
const storageKey = `backgroundCache-${await UniqueId.ensureWindowId(owner.windowId)}-${key}`;
if (value)
mCaches[storageKey] = value;
sessionStorage.setItem(storageKey, value);
else
delete mCaches[storageKey];
sessionStorage.removeItem(storageKey);
return;
}

Expand Down Expand Up @@ -341,7 +340,7 @@ export function markWindowCacheDirtyFromTab(tab, akey) {
async function getWindowCache(owner, key) {
if (!configs.persistCachedTree) {
const storageKey = `backgroundCache-${await UniqueId.ensureWindowId(owner.windowId)}-${key}`;
return mCaches[storageKey];
return sessionStorage.getItem(storageKey);
}
return browser.sessions.getWindowValue(owner.windowId, key).catch(ApiTabs.createErrorHandler());
}
Expand Down Expand Up @@ -520,13 +519,13 @@ Tab.onHidden.addListener(tab => {
browser.runtime.onMessage.addListener((message, _sender) => {
switch (message && message.type) {
case Constants.kCOMMAND_GET_ON_MEMORY_CACHE:
return mCaches[message.key];
return sessionStorage.getItem(message.key);

case Constants.kCOMMAND_SET_ON_MEMORY_CACHE:
if (message.value)
mCaches[message.key] = message.value;
sessionStorage.setItem(message.key, message.value);
else
delete mCaches[message.key];
sessionStorage.removeItem(message.key);
return;

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

browser.windows.onRemoved.addListener(async windowId => {
const storageKeyPart = `Cache-${await UniqueId.ensureWindowId(windowId)}-`;
for (const key in mCaches) {
for (let i = sessionStorage.length; i > -1; i--) {
const key = sessionStorage.key(i);
if (key.includes(storageKeyPart))
delete mCaches[key];
sessionStorage.removeItem(key);
}
});

Expand Down

0 comments on commit 504cd51

Please sign in to comment.