update: remove unused cache func

This commit is contained in:
mbaharip
2025-01-24 17:40:55 +07:00
parent c25dbaa96a
commit 8ace6fdff2
3 changed files with 2 additions and 91 deletions
+2 -3
View File
@@ -2,10 +2,9 @@
import { revalidatePath } from "next/cache"; import { revalidatePath } from "next/cache";
import { cookies } from "next/headers"; 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 { type ActionResponseSchema } from "~/types";
import { PathValidationCache } from "~/lib/cache";
import { toUrlPath } from "~/lib/utils"; import { toUrlPath } from "~/lib/utils";
import { encryptionService, gdriveNoCache } from "~/lib/utils.server"; import { encryptionService, gdriveNoCache } from "~/lib/utils.server";
@@ -128,7 +127,7 @@ export async function CheckPagePassword(
? await encryptionService.decrypt(config.apiConfig.sharedDrive!) ? await encryptionService.decrypt(config.apiConfig.sharedDrive!)
: undefined; : undefined;
const pathsArray = USE_CACHE ? PathValidationCache.get(toUrlPath(paths)) ?? paths : paths; const pathsArray = paths;
const folderIds: string[] = []; const folderIds: string[] = [];
for (const path of pathsArray) { for (const path of pathsArray) {
-23
View File
@@ -1,9 +1,7 @@
"use server"; "use server";
import { USE_CACHE } from "~/constant";
import { type ActionResponseSchema } from "~/types"; import { type ActionResponseSchema } from "~/types";
import { PathCache, PathValidationCache } from "~/lib/cache";
import { encryptionService, gdrive } from "~/lib/utils.server"; import { encryptionService, gdrive } from "~/lib/utils.server";
import config from "config"; import config from "config";
@@ -24,15 +22,6 @@ export async function GetFilePaths(fileName: string, parentId?: string): Promise
while (parentIdTemp) { while (parentIdTemp) {
if (parentIdTemp === decryptedRootId) break; 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({ const { data } = await gdrive.files.get({
fileId: parentIdTemp, fileId: parentIdTemp,
fields: "id,name,parents", fields: "id,name,parents",
@@ -41,11 +30,6 @@ export async function GetFilePaths(fileName: string, parentId?: string): Promise
if (!data.name) break; if (!data.name) break;
paths.unshift(data.name); paths.unshift(data.name);
if (USE_CACHE)
PathCache.set(parentIdTemp, {
id: data.parents?.[0] ?? "",
name: data.name,
});
parentIdTemp = data.parents?.[0]; parentIdTemp = data.parents?.[0];
} }
@@ -75,11 +59,6 @@ export async function ValidatePaths(
? await encryptionService.decrypt(config.apiConfig.sharedDrive!) ? await encryptionService.decrypt(config.apiConfig.sharedDrive!)
: undefined; : 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<PathFetch | null>[] = []; const promises: Promise<PathFetch | null>[] = [];
for (const [index, path] of paths.entries()) { for (const [index, path] of paths.entries()) {
const list = gdrive.files const list = gdrive.files
@@ -173,8 +152,6 @@ export async function ValidatePaths(
}); });
} }
if (USE_CACHE) PathValidationCache.set(paths.join("/"), response);
return { return {
success: true, success: true,
message: "Paths validated", message: "Paths validated",
-65
View File
@@ -1,65 +0,0 @@
type PathValidationData = { id: string; path: string; mimeType: string };
type PathData = { id: string; name: string };
class CacheService<T> {
private cache: Map<string, { data: T; timestamp: number }>;
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<PathValidationData[]>();
export const PathCache = new CacheService<PathData>();