Skip to content

Commit

Permalink
Merge pull request #1 from rivet-gg/max/readd-cache-version
Browse files Browse the repository at this point in the history
Reimplement cache version checking
  • Loading branch information
NathanFlurry authored Oct 26, 2023
2 parents 0368175 + 6828ddb commit f517698
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/data/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ export namespace ThreadHistoryCache {
}

export async function get(threadId: string): Promise<Payload> {
return await readCache(['threads2', threadId, 'history']);
return await readCache(['threads', threadId, 'history']);
}

export function set(threadId: string, payload: Payload) {
writeCache(['threads2', threadId, 'history'], payload);
writeCache(['threads', threadId, 'history'], payload);
}
}

Expand All @@ -40,11 +40,11 @@ export namespace ThreadLiveCache {
}

export async function get(threadId: string): Promise<Payload> {
return await readCache(['threads2', threadId]);
return await readCache(['threads', threadId]);
}

export function set(threadId: string, payload: Payload) {
writeCache(['threads2', threadId], payload);
writeCache(['threads', threadId], payload);
}
}

Expand All @@ -55,11 +55,11 @@ export namespace RecentThreadsCache {
}

export async function get(): Promise<Payload> {
return await readCache(['threads2', 'recent']);
return await readCache(['threads', 'recent']);
}

export function set(payload: Payload) {
writeCache(['threads2', 'recent'], payload);
writeCache(['threads', 'recent'], payload);
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface CacheWrapper<T> {
data: T;

ts: number;
// version: string;
version: string;
}

export interface LocalStorageChange {
Expand Down Expand Up @@ -144,7 +144,11 @@ export async function readCache<T>(keyPath: string[]): Promise<T> {
let wrapper = data;

// Cache expired or outdated (used to use blobs)
if (wrapper instanceof Blob || Date.now() - (wrapper.ts as number) >= CACHE_LIFETIME) {
if (
wrapper instanceof Blob ||
Date.now() - wrapper.ts >= CACHE_LIFETIME ||
wrapper.version != config.GIT_COMMIT
) {
logging.debug('Cache expired', key);
deleteIDB(db, key);

Expand All @@ -165,7 +169,8 @@ export async function writeCache<T>(keyPath: string[], payload: T) {
// Add timestamp and version properties
let cacheWrapper: CacheWrapper<T> = {
data: payload,
ts: Date.now()
ts: Date.now(),
version: config.GIT_COMMIT
};

// Gracefully get DB
Expand Down

0 comments on commit f517698

Please sign in to comment.