Skip to content

Commit

Permalink
feat: signed 캐시 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Tekiter committed Jul 29, 2023
1 parent 9e09da1 commit b7d7756
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
38 changes: 31 additions & 7 deletions apps/ssr-gateway/src/router/recruit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const recruitRouter = router({
page: internalProcedure.input(z.object({ id: z.string().optional() })).query(async ({ ctx, input }) => {
const pageStorage = getPageStorage(ctx.kv);
const allowedPagesStorage = getAllowedPagesStorage(ctx.kv);
const signedPageStorage = getSignedPageStorage(ctx.kv);

const pageId = parsePageId(input.id ?? ctx.recruit.rootPageId);
if (!pageId) {
Expand All @@ -25,6 +26,12 @@ export const recruitRouter = router({
return { status: 'NOT_FOUND' } as const;
}

const cached = await signedPageStorage.get(pageId);
if (cached) {
console.log('cached');
return { status: 'SUCCESS', ...cached } as const;
}

const data = await pageStorage.get(pageId);

if (!data) {
Expand All @@ -37,6 +44,13 @@ export const recruitRouter = router({
return await ctx.recruit.notionClient.SignFileUrls(blockMap);
})();

const newSignedPage = {
...pageData,
blockMap: blockMapSigned,
};

await signedPageStorage.put(pageId, newSignedPage, { expirationTtl: 60 * 50 });

return { status: 'SUCCESS', ...pageData, blockMap: blockMapSigned } as const;
}),
refresh: publicProcedure.mutation(async ({ ctx }) => {
Expand All @@ -52,15 +66,23 @@ type PathFragment = {
title: string;
};

const pageSchema = z.object({
id: z.string(),
title: z.string().nullable(),
path: z.array(z.custom<PathFragment>()),
blockMap: z.custom<Record<string, Block>>(),
});

const getPageStorage = storageFactory({
version: 1,
version: 2,
prefix: 'recruit:page:',
type: z.object({
id: z.string(),
title: z.string().nullable(),
path: z.array(z.custom<PathFragment>()),
blockMap: z.custom<Record<string, Block>>(),
}),
type: pageSchema,
});

const getSignedPageStorage = storageFactory({
version: 2,
prefix: 'recruit:signedPage:',
type: pageSchema,
});

const getAllowedPagesStorage = storageFactory({
Expand All @@ -72,6 +94,7 @@ const getAllowedPagesStorage = storageFactory({
async function refetchPages(ctx: Context) {
const pageStorage = getPageStorage(ctx.kv);
const allowedPagesStorage = getAllowedPagesStorage(ctx.kv);
const signedPagesStorage = getSignedPageStorage(ctx.kv);

const rootPageId = parsePageId(ctx.recruit.rootPageId);
const allowedPages: string[] = [];
Expand Down Expand Up @@ -114,4 +137,5 @@ async function refetchPages(ctx: Context) {

await traverse(rootPageId);
await allowedPagesStorage.put('', allowedPages);
await signedPagesStorage.deleteAll();
}
8 changes: 4 additions & 4 deletions apps/ssr-gateway/src/storage/kv.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { KVNamespace } from '@cloudflare/workers-types';
import type { KVNamespace, KVNamespacePutOptions } from '@cloudflare/workers-types';
import { z } from 'zod';

interface StorageConfig<T, V extends number> {
Expand Down Expand Up @@ -28,12 +28,12 @@ export function createStorageClient<T, V extends number>(kv: KVNamespace, config
return parsed.data.data;
}

async function put(key: string, value: T) {
async function put(key: string, value: T, option?: KVNamespacePutOptions) {
const newData = {
version,
data: value,
};
await kv.put(prefix + key, JSON.stringify(newData));
await kv.put(prefix + key, JSON.stringify(newData), option);
}

async function remove(key: string) {
Expand All @@ -50,7 +50,7 @@ export function createStorageClient<T, V extends number>(kv: KVNamespace, config
async function deleteAll() {
const keys = await list();

await Promise.all(keys.map((key) => remove(key.name)));
await Promise.all(keys.map(async (key) => await kv.delete(key.name)));
}

return {
Expand Down

0 comments on commit b7d7756

Please sign in to comment.