Vikash [Agenty]
Vikash [Agenty]
CDCloudflare Developers
Created by Vikash [Agenty] on 4/18/2024 in #workers-help
How to implement code-level caching service?
How to use caching on code-level? I need to cache the getAuthUser(userId) function result on code level to avoid querying D1 again and again as the getAuthUser is a middleware used on all the API routes. So, I built a get/set caching function using a fake url, user/1, user/2 etc. But it doesn't seems to be working and I see undefined in cache.match(cacheKey) response always.
export const getCached = async (request: HonoRequest, key: string) => {
const cacheUrl = new URL(request.url);
cacheUrl.pathname = '/' + key;

const cacheKey = new Request(cacheUrl.toString(), {
method: 'GET',
cf: {
cacheEverything: true,
cacheTtl: 86400,
},
});

const cache = caches.default;
const response = await cache.match(cacheKey);
console.log('r', response);
if (response) {
const json = await response.json();
console.log('json', json);
}
};

export const setCached = async (
request: HonoRequest,
key: string,
data: any
) => {
const cacheUrl = new URL(request.url);
cacheUrl.pathname = '/' + key;

const cacheKey = new Request(cacheUrl.toString(), {
method: 'GET',
cf: {
cacheEverything: true,
cacheTtl: 86400,
},
});
const cache = caches.default;
const response = new Response(data);
response.headers.append('Cache-Control', 's-maxage=30');
await cache.put(cacheKey, response);
};
export const getCached = async (request: HonoRequest, key: string) => {
const cacheUrl = new URL(request.url);
cacheUrl.pathname = '/' + key;

const cacheKey = new Request(cacheUrl.toString(), {
method: 'GET',
cf: {
cacheEverything: true,
cacheTtl: 86400,
},
});

const cache = caches.default;
const response = await cache.match(cacheKey);
console.log('r', response);
if (response) {
const json = await response.json();
console.log('json', json);
}
};

export const setCached = async (
request: HonoRequest,
key: string,
data: any
) => {
const cacheUrl = new URL(request.url);
cacheUrl.pathname = '/' + key;

const cacheKey = new Request(cacheUrl.toString(), {
method: 'GET',
cf: {
cacheEverything: true,
cacheTtl: 86400,
},
});
const cache = caches.default;
const response = new Response(data);
response.headers.append('Cache-Control', 's-maxage=30');
await cache.put(cacheKey, response);
};
6 replies
CDCloudflare Developers
Created by Vikash [Agenty] on 3/27/2024 in #workers-help
Catch-All email routing not working
No description
9 replies
CDCloudflare Developers
Created by Vikash [Agenty] on 10/22/2023 in #pages-help
Uncaught Error: No such module "__STATIC_CONTENT_MANIFEST".
No description
20 replies
CDCloudflare Developers
Created by Vikash [Agenty] on 10/2/2023 in #workers-help
How to allow customers to use custom domain?
For example, I have my application hosted on Cloudflare worker with domain dayschedule.com and each customer get their subdomain on signup with wildcard routing - For example vikash.dayschedule.com How can I use Cloudflare to allow our customers to map their domain via CNAME mapping? For example - - vikash.com > vikash.dayschedule.com - alpha.com > alpha.dayschedule.com I will use D1 to store mapping when reigstered by customers, also the HTTPS expected to work on all links.
6 replies
CDCloudflare Developers
Created by Vikash [Agenty] on 9/9/2023 in #pages-help
How to deploy functions with Pages?
No description
3 replies