mirror of
https://github.com/CompeyDev/fxtwitter-docker.git
synced 2025-04-06 11:00:54 +01:00
Improved caching logic to hit all requests
This commit is contained in:
parent
fdad942285
commit
e9bc664668
1 changed files with 28 additions and 18 deletions
|
@ -23,19 +23,8 @@ const statusRequest = async (request: any, event: FetchEvent, flags: Flags = {})
|
||||||
|
|
||||||
if (isBotUA || flags.direct) {
|
if (isBotUA || flags.direct) {
|
||||||
console.log('Matched bot UA');
|
console.log('Matched bot UA');
|
||||||
// https://developers.cloudflare.com/workers/examples/cache-api/
|
|
||||||
const cacheUrl = new URL(request.url);
|
|
||||||
const cacheKey = new Request(cacheUrl.toString(), request);
|
|
||||||
const cache = caches.default;
|
|
||||||
|
|
||||||
let response = await cache.match(cacheKey);
|
let response: Response;
|
||||||
|
|
||||||
if (response) {
|
|
||||||
console.log('Cache hit');
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('Cache miss');
|
|
||||||
|
|
||||||
let status = await handleStatus(
|
let status = await handleStatus(
|
||||||
id.match(/\d{2,20}/)?.[0],
|
id.match(/\d{2,20}/)?.[0],
|
||||||
|
@ -60,11 +49,6 @@ const statusRequest = async (request: any, event: FetchEvent, flags: Flags = {})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store the fetched response as cacheKey
|
|
||||||
// Use waitUntil so you can return the response without blocking on
|
|
||||||
// writing to cache
|
|
||||||
event.waitUntil(cache.put(cacheKey, response.clone()));
|
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
} else {
|
} else {
|
||||||
console.log('Matched human UA');
|
console.log('Matched human UA');
|
||||||
|
@ -143,9 +127,35 @@ router.all('*', async request => {
|
||||||
return Response.redirect(Constants.REDIRECT_URL, 307);
|
return Response.redirect(Constants.REDIRECT_URL, 307);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const cacheWrapper = async (event: FetchEvent): Promise<Response> => {
|
||||||
|
const { request } = event;
|
||||||
|
// https://developers.cloudflare.com/workers/examples/cache-api/
|
||||||
|
const cacheUrl = new URL(request.url);
|
||||||
|
const cacheKey = new Request(cacheUrl.toString(), request);
|
||||||
|
const cache = caches.default;
|
||||||
|
|
||||||
|
let cachedResponse = await cache.match(cacheKey);
|
||||||
|
|
||||||
|
if (cachedResponse) {
|
||||||
|
console.log('Cache hit');
|
||||||
|
return cachedResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Cache miss');
|
||||||
|
|
||||||
|
let response = await router.handle(event.request, event);
|
||||||
|
|
||||||
|
// Store the fetched response as cacheKey
|
||||||
|
// Use waitUntil so you can return the response without blocking on
|
||||||
|
// writing to cache
|
||||||
|
event.waitUntil(cache.put(cacheKey, response.clone()));
|
||||||
|
|
||||||
|
return response;
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Event to receive web requests on Cloudflare Worker
|
Event to receive web requests on Cloudflare Worker
|
||||||
*/
|
*/
|
||||||
addEventListener('fetch', (event: FetchEvent) => {
|
addEventListener('fetch', (event: FetchEvent) => {
|
||||||
event.respondWith(router.handle(event.request, event));
|
event.respondWith(cacheWrapper(event));
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue