add log
This commit is contained in:
@@ -44,13 +44,16 @@ export const workspaceMiddleware = async (
|
||||
const user = context.get(userContext)!
|
||||
|
||||
const cacheKey = buildWorkspaceCacheKey(request, user.sub, orgId)
|
||||
const cached = await getFromCache(cacheKey)
|
||||
const cached = await getWorkspaceFromCache(cacheKey)
|
||||
|
||||
if (cached) {
|
||||
console.log('Warm start')
|
||||
context.set(workspaceContext, cached)
|
||||
return next()
|
||||
}
|
||||
|
||||
console.log('Cold start')
|
||||
|
||||
const r = await req({
|
||||
url: `/users/${user.sub}/orgs?limit=25`,
|
||||
request,
|
||||
@@ -88,7 +91,7 @@ export const workspaceMiddleware = async (
|
||||
context
|
||||
}).then((r) => r.json())) as any
|
||||
|
||||
const workspace = {
|
||||
const workspace: WorkspaceContextProps = {
|
||||
activeWorkspace,
|
||||
workspaces,
|
||||
subscription: org?.['subscription'] || null,
|
||||
@@ -116,22 +119,26 @@ function buildWorkspaceCacheKey(
|
||||
})
|
||||
}
|
||||
|
||||
async function getFromCache(
|
||||
async function getWorkspaceFromCache(
|
||||
key: Request
|
||||
): Promise<WorkspaceContextProps | null> {
|
||||
const cache: Cache = await caches.open('saladeaula.digital')
|
||||
const cached = await cache.match(key)
|
||||
const cached: Response | undefined = await cache.match(key)
|
||||
|
||||
if (!cached) {
|
||||
return null
|
||||
}
|
||||
|
||||
return cached.json()
|
||||
return (await cached.json()) as WorkspaceContextProps
|
||||
}
|
||||
|
||||
async function saveToCache(key: Request, data: WorkspaceContextProps) {
|
||||
async function saveToCache(
|
||||
key: Request,
|
||||
workspace: WorkspaceContextProps
|
||||
): Promise<void> {
|
||||
const cache: Cache = await caches.open('saladeaula.digital')
|
||||
const response = new Response(JSON.stringify(data), {
|
||||
|
||||
const response: Response = new Response(JSON.stringify(workspace), {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': `public, max-age=${60 * 10}`
|
||||
|
||||
Reference in New Issue
Block a user