mirror of
https://github.com/ItsVipra/ProToots
synced 2024-11-21 19:13:03 +00:00
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:
parent
1b0bc5658e
commit
25d0280f8b
2 changed files with 37 additions and 23 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue