From c2451865aadcefac4b457f60e28752c62f6637db Mon Sep 17 00:00:00 2001 From: Paladox none Date: Sat, 20 Apr 2024 21:20:41 +0000 Subject: Fix clearing cache in gr-rest-api We include the base url in urlWithParams so we have to do the same inside gr-rest-api by calling getBaseUrl(). Also clear cache in savePreferences(). Release-Notes: Fix clearing cache in gr-rest-api Bug: 335613081 Change-Id: Ic0fb6af3316c8cd77bcaa64b802cd777cff339d7 (cherry picked from commit 6915b8aa40620176313ec3b55aef90e28a3e6936) --- .../gr-rest-apis/gr-rest-api-helper.ts | 41 +++++++++++----------- .../app/services/gr-rest-api/gr-rest-api-impl.ts | 8 ++++- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper.ts b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper.ts index 108451217a..c048489f6a 100644 --- a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper.ts +++ b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper.ts @@ -59,6 +59,13 @@ export function parsePrefixedJSON(jsonWithPrefix: string): ParsedJSON { return JSON.parse(jsonWithPrefix.substring(JSON_PREFIX.length)) as ParsedJSON; } +// Adds base url if not added in cache key +// or doesn't add it if it already is there. +function addBaseUrl(key: string) { + if (!getBaseUrl()) return key; + return key.startsWith(getBaseUrl()) ? key : getBaseUrl() + key; +} + /** * Wrapper around Map for caching server responses. Site-based so that * changes to CANONICAL_PATH will result in a different cache going into @@ -78,27 +85,21 @@ export class SiteBasedCache { // so that we spare more round trips to the server when the app loads // initially. Object.entries(window.INITIAL_DATA).forEach(e => - this._cache().set(e[0], e[1] as unknown as ParsedJSON) + this._cache().set(addBaseUrl(e[0]), e[1] as unknown as ParsedJSON) ); } } // Returns the cache for the current canonical path. _cache(): Map { - if (!this.data.has(window.CANONICAL_PATH)) { - this.data.set( - window.CANONICAL_PATH, - new Map() - ); + if (!this.data.has(getBaseUrl())) { + this.data.set(getBaseUrl(), new Map()); } - return this.data.get(window.CANONICAL_PATH) as Map< - string, - ParsedJSON | null - >; + return this.data.get(getBaseUrl()) as Map; } has(key: string) { - return this._cache().has(key); + return this._cache().has(addBaseUrl(key)); } get(key: '/accounts/self/emails'): EmailInfo[] | null; @@ -108,7 +109,7 @@ export class SiteBasedCache { get(key: string): ParsedJSON | null; get(key: string): unknown { - return this._cache().get(key); + return this._cache().get(addBaseUrl(key)); } set(key: '/accounts/self/emails', value: EmailInfo[]): void; @@ -118,21 +119,21 @@ export class SiteBasedCache { set(key: string, value: ParsedJSON | null): void; set(key: string, value: unknown) { - this._cache().set(key, value); + this._cache().set(addBaseUrl(key), value); } delete(key: string) { - this._cache().delete(key); + this._cache().delete(addBaseUrl(key)); } invalidatePrefix(prefix: string) { const newMap = new Map(); for (const [key, value] of this._cache().entries()) { - if (!key.startsWith(prefix)) { + if (!key.startsWith(addBaseUrl(prefix))) { newMap.set(key, value); } } - this.data.set(window.CANONICAL_PATH, newMap); + this.data.set(getBaseUrl(), newMap); } } @@ -155,11 +156,11 @@ export class FetchPromisesCache { * @return true only if a value for a key sets and it is not undefined */ has(key: string): boolean { - return !!this.data[key]; + return !!this.data[addBaseUrl(key)]; } get(key: string) { - return this.data[key]; + return this.data[addBaseUrl(key)]; } /** @@ -167,13 +168,13 @@ export class FetchPromisesCache { * mark key as deleted. */ set(key: string, value: Promise | undefined) { - this.data[key] = value; + this.data[addBaseUrl(key)] = value; } invalidatePrefix(prefix: string) { const newData: FetchPromisesCacheData = {}; Object.entries(this.data).forEach(([key, value]) => { - if (!key.startsWith(prefix)) { + if (!key.startsWith(addBaseUrl(prefix))) { newData[key] = value; } }); diff --git a/polygerrit-ui/app/services/gr-rest-api/gr-rest-api-impl.ts b/polygerrit-ui/app/services/gr-rest-api/gr-rest-api-impl.ts index 347e24c021..edbd6a38bf 100644 --- a/polygerrit-ui/app/services/gr-rest-api/gr-rest-api-impl.ts +++ b/polygerrit-ui/app/services/gr-rest-api/gr-rest-api-impl.ts @@ -686,6 +686,9 @@ export class GrRestApiServiceImpl implements RestApiService, Finalizable { savePreferences( prefs: PreferencesInput ): Promise { + // Invalidate the cache. + this._cache.delete('/accounts/self/preferences'); + // Note (Issue 5142): normalize the download scheme with lower case before // saving. if (prefs.download_scheme) { @@ -834,7 +837,10 @@ export class GrRestApiServiceImpl implements RestApiService, Finalizable { const cachedAccount = this._cache.get('/accounts/self/detail'); if (cachedAccount) { // Replace object in cache with new object to force UI updates. - this._cache.set('/accounts/self/detail', {...cachedAccount, ...obj}); + this._cache.set('/accounts/self/detail', { + ...cachedAccount, + ...obj, + }); } } -- cgit v1.2.3