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