mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-10-02 16:20:36 -05:00
Move app to apps/web-react in pnpm workspace layout
This commit is contained in:
33
apps/web-react/app/utils/cache.server.ts
Normal file
33
apps/web-react/app/utils/cache.server.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import type { CacheEntry } from "@epic-web/cachified";
|
||||
import { ServerConfig } from "~/config.server";
|
||||
import { LRUCache } from "~/modules/cache";
|
||||
|
||||
declare global {
|
||||
// This preserves the LRU cache during development
|
||||
var __lruCache: LRUCache<string, CacheEntry<unknown>> | undefined;
|
||||
}
|
||||
|
||||
// biome-ignore lint/suspicious/noAssignInExpressions: trick to only create one
|
||||
export const cache = (global.__lruCache = global.__lruCache
|
||||
? global.__lruCache
|
||||
: new LRUCache<string, CacheEntry<unknown>>({ max: 5000 }));
|
||||
|
||||
export const ttl = (ms: number) => (ServerConfig.disableCache ? 0 : ms);
|
||||
|
||||
export function syncCached<T>(key: string, getFreshValue: () => T) {
|
||||
if (cache.has(key)) {
|
||||
return cache.get(key) as T;
|
||||
}
|
||||
|
||||
const value = getFreshValue();
|
||||
cache.set(key, value as any);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
export const IN_MILLISECONDS = {
|
||||
HALF_HOUR: 30 * 60 * 1000,
|
||||
ONE_HOUR: 60 * 60 * 1000,
|
||||
TWO_HOURS: 2 * 60 * 60 * 1000,
|
||||
TWO_DAYS: 2 * 24 * 60 * 60 * 1000,
|
||||
};
|
||||
Reference in New Issue
Block a user