π OpenHands: AI-Driven Development
Don't persist cache on reload (#4854)
Robert Brennan35c68863dc92
diff --git a/frontend/__tests__/utils/cache.test.ts b/frontend/__tests__/utils/cache.test.tsindex 0a3bbcce05..6b3762c38a 100644--- a/frontend/__tests__/utils/cache.test.ts+++ b/frontend/__tests__/utils/cache.test.ts@@ -8,7 +8,6 @@ describe("Cache", () => {const testTTL = 1000; // 1 secondbeforeEach(() => {- localStorage.clear();vi.useFakeTimers();});@@ -16,17 +15,7 @@ describe("Cache", () => {vi.useRealTimers();});- it("sets data in localStorage with expiration", () => {- cache.set(testKey, testData, testTTL);- const cachedEntry = JSON.parse(- localStorage.getItem(`app_cache_${testKey}`) || "",- );-- expect(cachedEntry.data).toEqual(testData);- expect(cachedEntry.expiration).toBeGreaterThan(Date.now());- });-- it("gets data from localStorage if not expired", () => {+ it("gets data from memory if not expired", () => {cache.set(testKey, testData, testTTL);expect(cache.get(testKey)).toEqual(testData);@@ -39,7 +28,6 @@ describe("Cache", () => {vi.advanceTimersByTime(5 * 60 * 1000 + 1);expect(cache.get(testKey)).toBeNull();- expect(localStorage.getItem(`app_cache_${testKey}`)).toBeNull();});it("returns null if cached data is expired", () => {@@ -47,28 +35,19 @@ describe("Cache", () => {vi.advanceTimersByTime(testTTL + 1);expect(cache.get(testKey)).toBeNull();- expect(localStorage.getItem(`app_cache_${testKey}`)).toBeNull();});- it("deletes data from localStorage", () => {+ it("deletes data from memory", () => {cache.set(testKey, testData, testTTL);cache.delete(testKey);-- expect(localStorage.getItem(`app_cache_${testKey}`)).toBeNull();+ expect(cache.get(testKey)).toBeNull();});- it("clears all data with the app prefix from localStorage", () => {+ it("clears all data with the app prefix from memory", () => {cache.set(testKey, testData, testTTL);cache.set("anotherKey", { data: "More data" }, testTTL);cache.clearAll();-- expect(localStorage.length).toBe(0);- });-- it("does not retrieve non-prefixed data from localStorage when clearing", () => {- localStorage.setItem("nonPrefixedKey", "should remain");- cache.set(testKey, testData, testTTL);- cache.clearAll();- expect(localStorage.getItem("nonPrefixedKey")).toBe("should remain");+ expect(cache.get(testKey)).toBeNull();+ expect(cache.get("anotherKey")).toBeNull();});});diff --git a/frontend/src/utils/cache.ts b/frontend/src/utils/cache.tsindex 5edfdc5bf5..0d09fa017c 100644--- a/frontend/src/utils/cache.ts+++ b/frontend/src/utils/cache.ts@@ -5,26 +5,17 @@ type CacheEntry<T> = {};class Cache {- private prefix = "app_cache_";-private defaultTTL = 5 * 60 * 1000; // 5 minutes- /**- * Generate a unique key with prefix for local storage- * @param key The key to be stored in local storage- * @returns The unique key with prefix- */- private getKey(key: CacheKey): string {- return `${this.prefix}${key}`;- }+ private cacheMemory: Record<string, string> = {};/**- * Retrieve the cached data from local storage- * @param key The key to be retrieved from local storage- * @returns The data stored in local storage+ * Retrieve the cached data from memory+ * @param key The key to be retrieved from memory+ * @returns The data stored in memory*/public get<T>(key: CacheKey): T | null {- const cachedEntry = localStorage.getItem(this.getKey(key));+ const cachedEntry = this.cacheMemory[key];if (cachedEntry) {const { data, expiration } = JSON.parse(cachedEntry) as CacheEntry<T>;if (Date.now() < expiration) return data;@@ -35,34 +26,34 @@ class Cache {}/**- * Store the data in local storage with expiration- * @param key The key to be stored in local storage- * @param data The data to be stored in local storage+ * Store the data in memory with expiration+ * @param key The key to be stored in memory+ * @param data The data to be stored in memory* @param ttl The time to live for the data in milliseconds* @returns void*/public set<T>(key: CacheKey, data: T, ttl = this.defaultTTL): void {const expiration = Date.now() + ttl;const entry: CacheEntry<T> = { data, expiration };- localStorage.setItem(this.getKey(key), JSON.stringify(entry));+ this.cacheMemory[key] = JSON.stringify(entry);}/**- * Remove the data from local storage- * @param key The key to be removed from local storage+ * Remove the data from memory+ * @param key The key to be removed from memory* @returns void*/public delete(key: CacheKey): void {- localStorage.removeItem(this.getKey(key));+ delete this.cacheMemory[key];}/**- * Clear all data with the app prefix from local storage+ * Clear all data* @returns void*/public clearAll(): void {- Object.keys(localStorage).forEach((key) => {- if (key.startsWith(this.prefix)) localStorage.removeItem(key);+ Object.keys(this.cacheMemory).forEach((key) => {+ delete this.cacheMemory[key];});}}