-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache project #837
Cache project #837
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { RouteMiddleware } from '../types/route-middleware'; | ||
|
||
export type SlowRequests = Record< | ||
string, | ||
{ | ||
key: string; | ||
count: number; | ||
avg: number; | ||
slowest: number; | ||
} | ||
>; | ||
|
||
const slowRequestStore: SlowRequests = {}; | ||
|
||
export const slowRequests: RouteMiddleware<{ slug: string }> = async (context, next) => { | ||
const requestStart = Date.now(); | ||
await next(); | ||
|
||
const routeKey = `${context.method} ${context._matchedRoute}`; | ||
|
||
const requestEnd = Date.now(); | ||
const requestTime = requestEnd - requestStart; | ||
if (!slowRequestStore[routeKey]) { | ||
slowRequestStore[routeKey] = { key: routeKey, count: 0, avg: 0, slowest: 0 }; | ||
} | ||
|
||
const existinRoute = slowRequestStore[routeKey]; | ||
|
||
existinRoute.count++; | ||
existinRoute.avg = (existinRoute.avg * (existinRoute.count - 1) + requestTime) / existinRoute.count; | ||
existinRoute.slowest = Math.max(existinRoute.slowest, requestTime); | ||
|
||
if (Object.keys(slowRequestStore).length > 50) { | ||
// Sort by slowest. | ||
const sorted = Object.values(slowRequestStore).sort((a, b) => b.slowest - a.slowest); | ||
const slowest = sorted.pop(); | ||
if (slowest && slowest.key) { | ||
delete slowRequestStore[slowest.key]; | ||
} | ||
} | ||
}; | ||
|
||
export function getSlowRequests() { | ||
return slowRequestStore; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import cache from 'memory-cache'; | ||
|
||
export async function cachePromise<T extends object>( | ||
key: string, | ||
getter: () => Promise<T>, | ||
timeInMs: number | ||
): Promise<T> { | ||
const resource = cache.get(key); | ||
if (resource) { | ||
return resource as T; | ||
} | ||
|
||
const result = await getter(); | ||
cache.put(key, result, timeInMs); | ||
return result; | ||
} | ||
|
||
const swcPromises: Record<string, Promise<any> | null> = {}; | ||
const DEFAULT_STALE_TIME = 1000 * 60 * 60 * 8; // 8 hours | ||
|
||
export async function cachePromiseSWR<T extends object | null>( | ||
key: string, | ||
getter: () => Promise<T>, | ||
timeInMs: number, | ||
staleTimeInMs: number = DEFAULT_STALE_TIME | ||
): Promise<T> { | ||
if (swcPromises[key]) { | ||
await swcPromises[key]; | ||
} | ||
const resource = cache.get(key); | ||
const staleResource = cache.get(`@stale/${key}`); | ||
if (!resource) { | ||
const promise = getter(); | ||
swcPromises[key] = promise; | ||
promise.then(result => { | ||
cache.put(key, result, timeInMs); | ||
cache.put(`@stale/${key}`, result, staleTimeInMs); | ||
Comment on lines
+36
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked the source code of |
||
|
||
delete swcPromises[key]; | ||
|
||
return result; | ||
}); | ||
|
||
if (staleResource) { | ||
return staleResource; | ||
} | ||
|
||
return promise; | ||
} | ||
|
||
return resource; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't ideal, but it prevents looping every entry on line 23. There is a finite number route too - so if it is a problem it can be removed completely.