feat: implemented a basic store and emulatorjs
This commit is contained in:
parent
2f32cbc730
commit
7286541822
121 changed files with 5900 additions and 1092 deletions
34
src/bun/api/cache.ts
Normal file
34
src/bun/api/cache.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import { eq } from "drizzle-orm";
|
||||
import { cache } from "./app";
|
||||
import cacheSchema from "@schema/cache";
|
||||
|
||||
export const CACHE_KEYS = {
|
||||
ROM_PLATFORMS: 'rom-platforms',
|
||||
STORE_GAME: (path: string) => `store-game-${path}`,
|
||||
STORE_GAME_MANIFEST: 'store-game-manifest'
|
||||
} as const;
|
||||
|
||||
export async function getOrCached<T> (key: string, getter: () => Promise<T>, options?: { expireMs?: number; }): Promise<T>
|
||||
{
|
||||
const cached = await cache.query.item_cache.findFirst({ where: eq(cacheSchema.item_cache.key, key) });
|
||||
const updated_at = new Date();
|
||||
|
||||
if (cached && cached.expire_at > updated_at)
|
||||
{
|
||||
return cached.data as T;
|
||||
}
|
||||
|
||||
const data = await getter();
|
||||
|
||||
const expire_at = options?.expireMs ? new Date(updated_at.getTime() + options.expireMs) : new Date(updated_at.getTime() + 24 * 60 * 60 * 1000);
|
||||
|
||||
await cache.insert(cacheSchema.item_cache)
|
||||
.values({ key, data, updated_at, expire_at })
|
||||
.onConflictDoUpdate({
|
||||
target: cacheSchema.item_cache.key,
|
||||
set: { data, updated_at, expire_at }
|
||||
})
|
||||
.run();
|
||||
|
||||
return data;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue