Invalidate old cache versions (#56)

* cache current extension version number

* refresh pronouns, if not from same addon version

* remove conflicting log messages

* move cache entry age checking to caching.js
This commit is contained in:
Vivien 2023-07-18 21:47:19 +02:00 committed by GitHub
parent 1b0bc5658e
commit 25d0280f8b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 23 deletions

View file

@ -1,5 +1,9 @@
import { debug, error } from "./logging";
import { storage } from "webextension-polyfill";
import { debug, info, error } from "./logging.js";
import { runtime, storage } from "webextension-polyfill";
const currentVersion = runtime.getManifest().version;
const cacheMaxAge = 24 * 60 * 60 * 1000; // time after which cached pronouns should be checked again: 24h
/**
* Appends an entry to the "pronounsCache" object in local storage.
*
@ -15,7 +19,12 @@ export async function cachePronouns(account, pronouns) {
cache = { pronounsCache: {} };
}
cache.pronounsCache[account] = { acct: account, timestamp: Date.now(), value: pronouns };
cache.pronounsCache[account] = {
acct: account,
timestamp: Date.now(),
value: pronouns,
version: currentVersion,
};
try {
await storage.local.set(cache);
debug(`${account} cached`);
@ -24,7 +33,12 @@ export async function cachePronouns(account, pronouns) {
}
}
export async function getPronouns() {
/**
*
* @param {string} accountName
* @returns {Promise<string>} Account's cached pronouns, or null if not saved or stale
*/
export async function getPronouns(accountName) {
const fallback = { pronounsCache: {} };
let cacheResult;
try {
@ -38,5 +52,21 @@ export async function getPronouns() {
cacheResult = fallback;
// ignore errors, we have an empty object as fallback.
}
return cacheResult;
// Extract the current cache by using object destructuring.
if (accountName in cacheResult.pronounsCache) {
const { value, timestamp, version } = cacheResult.pronounsCache[accountName];
// If we have a cached value and it's not outdated, use it.
if (value && Date.now() - timestamp < cacheMaxAge && version == currentVersion) {
info(`${accountName} in cache with value: ${value}`);
return value;
} else {
info(`${accountName} cache entry is stale, refreshing`);
}
} else {
info(`${accountName} not in cache, fetching status`);
}
return null;
}

View file

@ -3,7 +3,6 @@ import { cachePronouns, getPronouns } from "./caching";
import { normaliseAccountName } from "./protootshelpers";
import { extractFromStatus } from "./pronouns";
const cacheMaxAge = 24 * 60 * 60 * 1000; // time after which cached pronouns should be checked again: 24h
let conversationsCache;
/**
@ -17,30 +16,15 @@ let conversationsCache;
*/
export async function fetchPronouns(dataID, accountName, type) {
// log(`searching for ${account_name}`);
const cacheResult = await getPronouns();
const cacheResult = await getPronouns(accountName);
debug(cacheResult);
// Extract the current cache by using object destructuring.
if (accountName in cacheResult.pronounsCache) {
const { value, timestamp } = cacheResult.pronounsCache[accountName];
// If we have a cached value and it's not outdated, use it.
if (value && Date.now() - timestamp < cacheMaxAge) {
info(`${accountName} in cache with value: ${value}`);
return value;
} else {
info(`${accountName} cache entry is stale, refreshing`);
}
}
info(`${accountName} cache entry is stale, refreshing`);
if (cacheResult) return cacheResult;
if (!dataID) {
warn(`Could not fetch pronouns for user ${accountName}, because no status ID was passed.`);
return null;
}
info(`${accountName} not in cache, fetching status`);
let status;
if (type === "notification") {
status = await fetchNotification(dataID);