From 331d8e25df49e8f3c5ef1138be6b48c5b786926d Mon Sep 17 00:00:00 2001 From: Ajay Date: Thu, 29 Feb 2024 03:06:01 -0500 Subject: [PATCH] Move cache class to lib --- maze-utils | 2 +- src/thumbnails/thumbnailDataCache.ts | 2 +- src/utils/cache.ts | 41 ---------------------------- 3 files changed, 2 insertions(+), 43 deletions(-) delete mode 100644 src/utils/cache.ts diff --git a/maze-utils b/maze-utils index a35272aa..40eb8f0f 160000 --- a/maze-utils +++ b/maze-utils @@ -1 +1 @@ -Subproject commit a35272aa0a7f836990809b8bb5fa28386a63b436 +Subproject commit 40eb8f0f7aab79ae419ff36cff924c9ce7cf65b3 diff --git a/src/thumbnails/thumbnailDataCache.ts b/src/thumbnails/thumbnailDataCache.ts index fb3d7147..6dde5bec 100644 --- a/src/thumbnails/thumbnailDataCache.ts +++ b/src/thumbnails/thumbnailDataCache.ts @@ -1,6 +1,6 @@ import { ChannelID } from "../../maze-utils/src/video"; import { VideoID } from "../../maze-utils/src/video"; -import { DataCache } from "../utils/cache"; +import { DataCache } from "../../maze-utils/src/cache"; export interface PlaybackUrl { url: string; diff --git a/src/utils/cache.ts b/src/utils/cache.ts deleted file mode 100644 index 0dcf6318..00000000 --- a/src/utils/cache.ts +++ /dev/null @@ -1,41 +0,0 @@ -interface CacheRecord { - lastUsed: number; -} - -const cacheLimit = 2000; - -export class DataCache { - private cache: Record; - private init: () => V; - - constructor(init: () => V) { - this.cache = {}; - this.init = init; - } - - public getFromCache(key: T): V & CacheRecord | undefined { - return this.cache[key]; - } - - public setupCache(key: T): V & CacheRecord { - if (!this.cache[key]) { - this.cache[key] = { - ...this.init(), - lastUsed: Date.now() - }; - - if (Object.keys(this.cache).length > cacheLimit) { - const oldest = Object.entries(this.cache).reduce((a, b) => a[1].lastUsed < b[1].lastUsed ? a : b); - delete this.cache[oldest[0]]; - } - } - - return this.cache[key]; - } - - public cacheUsed(key: T): boolean { - if (this.cache[key]) this.cache[key].lastUsed = Date.now(); - - return !!this.cache[key]; - } -} \ No newline at end of file