Skip to content

Commit

Permalink
feat(worker): prefetch resources after cache
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Aug 11, 2023
1 parent 64a5f12 commit 1bc9eba
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"anitomy": "workspace:*",
"hono": "^3.4.1",
"memofunc": "^0.0.5",
"ohash": "^1.1.3",
"simptrad": "^0.1.0"
},
"devDependencies": {
Expand Down
26 changes: 14 additions & 12 deletions packages/worker/src/query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Context } from 'hono';
import type { Resource, Team, User } from '@prisma/client/edge';

import { hash } from 'ohash';
import { memoAsync } from 'memofunc';
import {
parseSearchURL,
Expand Down Expand Up @@ -36,7 +37,13 @@ export async function queryResourceDetail(ctx: Context<{ Bindings: Env }>) {
return ctx.json({ detail });
}

export const getSearchResources = memoAsync(
export const PrefetchFilter = [
parseSearchURL(new URLSearchParams('?page=1')),
parseSearchURL(new URLSearchParams('?page=2')),
parseSearchURL(new URLSearchParams('?page=3'))
];

export const findResourcesFromDB = memoAsync(
async (env: Env, options: ResolvedFilterOptions) => {
const prisma = makePrisma(env);

Expand Down Expand Up @@ -118,38 +125,33 @@ export const getSearchResources = memoAsync(
{
external: {
async get([env, params]) {
return getResourcesStore(env).get(JSON.stringify({ params }));
return getResourcesStore(env).get(hash(params));
},
async set([env, params], value) {
return getResourcesStore(env).put(JSON.stringify({ params }), value, {
return getResourcesStore(env).put(hash(params), value, {
expirationTtl: 300
});
},
async remove([env, params]) {
return getResourcesStore(env).remove(JSON.stringify({ params }));
return getResourcesStore(env).remove(hash(params));
},
async clear() {}
}
}
);

async function getTimestamp(ctx: Context<{ Bindings: Env }>) {
const timestamp = await getRefreshTimestamp(ctx.env);
return timestamp;
}

export async function searchResources(ctx: Context<{ Bindings: Env }>) {
const url = new URL(ctx.req.url);
const filter = parseSearchURL(url.searchParams, await ctx.req.json().catch(() => undefined));
if (!filter) {
return ctx.json({ message: 'Request is not valid' }, 400);
}

const timestampPromise = getTimestamp(ctx);
const timestampPromise = getRefreshTimestamp(ctx.env);

const result = !isNoCache(ctx)
? await getSearchResources(ctx.env, filter)
: await getSearchResources.raw(ctx.env, filter);
? await findResourcesFromDB(ctx.env, filter)
: await findResourcesFromDB.raw(ctx.env, filter);

const complete = result.length > filter.pageSize;
const resources = resolveQueryResult(result.slice(0, filter.pageSize));
Expand Down
7 changes: 7 additions & 0 deletions packages/worker/src/scheduled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { Env } from './types';

import { makePrisma } from './prisma';
import { updateRefreshTimestamp } from './state';
import { findResourcesFromDB, PrefetchFilter } from './query';

const teams = new Set<number>();
const users = new Set<number>();
Expand Down Expand Up @@ -73,6 +74,12 @@ export async function handleScheduled(env: Env) {

if (sum > 0) {
await updateRefreshTimestamp(env);
await Promise.all(
PrefetchFilter.map(async (filter) => {
await findResourcesFromDB.clear(env, filter);
await findResourcesFromDB(env, filter);
})
);
}

return { count: sum };
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1bc9eba

Please sign in to comment.