Skip to content

Commit

Permalink
Clear chats cache if last updated more than 30 days ago (#6913)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Nov 28, 2024
1 parent 528413a commit ca9deb4
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion frontend/openchat-agent/src/utils/caching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
MessageContextMap,
MAX_EVENTS,
MAX_MESSAGES,
ONE_DAY,
updateCreatedUser,
} from "openchat-shared";
import type { Principal } from "@dfinity/principal";
Expand Down Expand Up @@ -354,7 +355,21 @@ export async function getCachedChats(
db: Database,
principal: Principal,
): Promise<ChatStateFull | undefined> {
return await (await db).get("chats", principal.toString());
const resolvedDb = await db;
const chats = await resolvedDb.get("chats", principal.toString());

if (
chats !== undefined &&
chats.latestUserCanisterUpdates < BigInt(Date.now() - 30 * ONE_DAY)
) {
// If the cache was last updated more than 30 days ago, clear the cache and return undefined
const storeNames = resolvedDb.objectStoreNames;
for (let i = 0; i < storeNames.length; i++) {
await resolvedDb.clear(storeNames[i]);
}
return undefined;
}
return chats;
}

export async function setCachedChats(
Expand Down

0 comments on commit ca9deb4

Please sign in to comment.