From 8ace6fdff2049b85fdc66cc20a14d17241ecdeba Mon Sep 17 00:00:00 2001 From: mbaharip <62494292+mbahArip@users.noreply.github.com> Date: Fri, 24 Jan 2025 17:40:55 +0700 Subject: [PATCH] update: remove unused cache func --- src/actions/password.ts | 5 ++-- src/actions/paths.ts | 23 --------------- src/lib/cache.ts | 65 ----------------------------------------- 3 files changed, 2 insertions(+), 91 deletions(-) delete mode 100644 src/lib/cache.ts diff --git a/src/actions/password.ts b/src/actions/password.ts index f2636a0..b1d14ff 100644 --- a/src/actions/password.ts +++ b/src/actions/password.ts @@ -2,10 +2,9 @@ import { revalidatePath } from "next/cache"; import { cookies } from "next/headers"; -import { COOKIES_NAME, COOKIES_OPTIONS, USE_CACHE } from "~/constant"; +import { COOKIES_NAME, COOKIES_OPTIONS } from "~/constant"; import { type ActionResponseSchema } from "~/types"; -import { PathValidationCache } from "~/lib/cache"; import { toUrlPath } from "~/lib/utils"; import { encryptionService, gdriveNoCache } from "~/lib/utils.server"; @@ -128,7 +127,7 @@ export async function CheckPagePassword( ? await encryptionService.decrypt(config.apiConfig.sharedDrive!) : undefined; - const pathsArray = USE_CACHE ? PathValidationCache.get(toUrlPath(paths)) ?? paths : paths; + const pathsArray = paths; const folderIds: string[] = []; for (const path of pathsArray) { diff --git a/src/actions/paths.ts b/src/actions/paths.ts index 70fc7f9..a536e60 100644 --- a/src/actions/paths.ts +++ b/src/actions/paths.ts @@ -1,9 +1,7 @@ "use server"; -import { USE_CACHE } from "~/constant"; import { type ActionResponseSchema } from "~/types"; -import { PathCache, PathValidationCache } from "~/lib/cache"; import { encryptionService, gdrive } from "~/lib/utils.server"; import config from "config"; @@ -24,15 +22,6 @@ export async function GetFilePaths(fileName: string, parentId?: string): Promise while (parentIdTemp) { if (parentIdTemp === decryptedRootId) break; - if (PathCache.has(parentIdTemp) && USE_CACHE) { - const cachedPath = PathCache.get(parentIdTemp)!; - if (cachedPath) { - paths.unshift(cachedPath.name); - parentIdTemp = cachedPath.id; - continue; - } - } - const { data } = await gdrive.files.get({ fileId: parentIdTemp, fields: "id,name,parents", @@ -41,11 +30,6 @@ export async function GetFilePaths(fileName: string, parentId?: string): Promise if (!data.name) break; paths.unshift(data.name); - if (USE_CACHE) - PathCache.set(parentIdTemp, { - id: data.parents?.[0] ?? "", - name: data.name, - }); parentIdTemp = data.parents?.[0]; } @@ -75,11 +59,6 @@ export async function ValidatePaths( ? await encryptionService.decrypt(config.apiConfig.sharedDrive!) : undefined; - if (PathValidationCache.has(paths.join("/")) && USE_CACHE) { - const cachedPath = PathValidationCache.get(paths.join("/"))!; - if (cachedPath) return { success: true, message: "Paths validated from cache", data: cachedPath }; - } - const promises: Promise[] = []; for (const [index, path] of paths.entries()) { const list = gdrive.files @@ -173,8 +152,6 @@ export async function ValidatePaths( }); } - if (USE_CACHE) PathValidationCache.set(paths.join("/"), response); - return { success: true, message: "Paths validated", diff --git a/src/lib/cache.ts b/src/lib/cache.ts deleted file mode 100644 index 315568b..0000000 --- a/src/lib/cache.ts +++ /dev/null @@ -1,65 +0,0 @@ -type PathValidationData = { id: string; path: string; mimeType: string }; -type PathData = { id: string; name: string }; - -class CacheService { - private cache: Map; - private ttl: number; - - constructor(ttlInSeconds = 60) { - this.cache = new Map(); - this.ttl = ttlInSeconds * 1000; - } - - set(key: string, value: T): void { - this.cache.set(key, { - data: value, - timestamp: Date.now(), - }); - } - - get(key: string): T | undefined { - const item = this.cache.get(key); - if (!item) return undefined; - - if (Date.now() - item.timestamp > this.ttl) { - this.cache.delete(key); - return undefined; - } - - return item.data; - } - - has(key: string): boolean { - return this.cache.has(key) && !this.isExpired(key); - } - - delete(key: string): void { - this.cache.delete(key); - } - - clear(): void { - this.cache.clear(); - } - - private isExpired(key: string): boolean { - const item = this.cache.get(key); - if (!item) return true; - - return Date.now() - item.timestamp > this.ttl; - } -} - -/** - * Currently not used since it will throw error when validating the paths for password - * TODO: Figure out why cached paths arguments are not being passed correctly to the server actions - * - * LOG: - * (page.tsx) [Params] ['Protected Folder - pass loremipsum'] - * (page.tsx) [ValidatePaths] From cache: true - * (page.tsx) [ValidatePaths] Paths: [{ id: "***", mimeType: "application/vnd.google-apps.folder", path: "Protected Folder - pass loremipsum" }] - * (Password.tsx) [CheckPagePassword] Paths: [] // This should be the same as the one above - * (Password.tsx) [CheckPagePassword] FolderIds: [] - * (Password.tsx) [CheckPagePassword] Unlocked: { success: true, message: "All folders on current path are public (/)" } - */ -export const PathValidationCache = new CacheService(); -export const PathCache = new CacheService();