This commit is contained in:
2026-01-27 13:51:51 -03:00
parent 028297443a
commit d5db9b6eff

View File

@@ -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}`