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);
};
3 Replies
℠
5mo ago
What does cache.put return? And you're not showing how the two funcs are implemented which might be relevant
Vikash [Agenty]
Vikash [Agenty]5mo ago
This is how I am using this cache function.
export const getUserById = async (
ctx: Context,
user_id: number,
account_id: number
) => {
const cacheKey = `users-${user_id}`;
const cacheValue = await getCached(ctx.req, cacheKey);
if(cacheValue) return cacheValue;

const sqlQuery = ctx.env.DB.prepare(
`SELECT * from Users where user_id = ? and account_id = ?`
).bind(Number(user_id), Number(account_id));

const authUser: AuthUser = await sqlQuery.first();
await setCached(ctx.req, cacheKey, authUser);
return authUser;
};
export const getUserById = async (
ctx: Context,
user_id: number,
account_id: number
) => {
const cacheKey = `users-${user_id}`;
const cacheValue = await getCached(ctx.req, cacheKey);
if(cacheValue) return cacheValue;

const sqlQuery = ctx.env.DB.prepare(
`SELECT * from Users where user_id = ? and account_id = ?`
).bind(Number(user_id), Number(account_id));

const authUser: AuthUser = await sqlQuery.first();
await setCached(ctx.req, cacheKey, authUser);
return authUser;
};
The cache.put() : Promise<void> is a void function, so it returns undefined
Vikash [Agenty]
Vikash [Agenty]5mo ago
Ok, I get it working. So issue resolved, but It doesn't expire. Even when I set s-maxage=30 header, the match return the value after 30s as well.
No description
Want results from more Discord servers?
Add your server