Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion apps/functional_chat/app/lib/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface CacheOptions {
ttl?: number;
}

export function cache({ keyGenerator, ttl = DEFAULT_TTL }: CacheOptions) {
export function cacheJson({ keyGenerator, ttl = DEFAULT_TTL }: CacheOptions) {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;

Expand Down Expand Up @@ -56,6 +56,40 @@ export function cache({ keyGenerator, ttl = DEFAULT_TTL }: CacheOptions) {
await originalMethod.apply(this, args);
};

return descriptor;
};
}

export function cacheSend({ keyGenerator, ttl = DEFAULT_TTL }: CacheOptions) {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;

descriptor.value = async function (...args: any[]) {
const [req, res] = args;

if (ttl <= 0) {
// If TTL is less than or equal to 0, bypass cache
return await originalMethod.apply(this, args);
}

const cacheKey = keyGenerator(...args);
const cachedData = getCache(cacheKey);

if (cachedData) {
res.send(cachedData); // Set the cached response
return;
}

// Capture the original send method to extract data
const originalSend = res.send.bind(res);
res.send = (buffer: any) => {
setCache(cacheKey, buffer, ttl);
originalSend(buffer);
};

await originalMethod.apply(this, args);
};

return descriptor;
};
}
4 changes: 2 additions & 2 deletions apps/functional_chat/pages/api/functions/newsSearch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { cache } from '~/lib/cache';
import { cacheJson } from '~/lib/cache';

class Api {
static async spec(req: NextApiRequest, res: NextApiResponse) {
Expand All @@ -26,7 +26,7 @@ class Api {
});
}

@cache({ keyGenerator: (req: NextApiRequest) => JSON.stringify(req.query) })
@cacheJson({ keyGenerator: (req: NextApiRequest) => JSON.stringify(req.query) })
static async call(req: NextApiRequest, res: NextApiResponse) {
// Access additional parameters from req.query
const { args } = req.query;
Expand Down
2 changes: 2 additions & 0 deletions apps/functional_chat/pages/api/functions/renderChart.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { cacheSend } from '~/lib/cache';

class Api {
static async spec(req: NextApiRequest, res: NextApiResponse) {
Expand Down Expand Up @@ -56,6 +57,7 @@ class Api {
});
}

@cacheSend({ keyGenerator: (req: NextApiRequest) => JSON.stringify(req.query) })
static async call(req: NextApiRequest, res: NextApiResponse) {
// Access additional parameters from req.query
const { args } = req.query;
Expand Down
4 changes: 2 additions & 2 deletions apps/functional_chat/pages/api/functions/stockQuote.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { cache } from '~/lib/cache';
import { cacheJson } from '~/lib/cache';

class Api {
static async spec(req: NextApiRequest, res: NextApiResponse) {
Expand All @@ -26,7 +26,7 @@ class Api {
});
}

@cache({ keyGenerator: (req: NextApiRequest) => JSON.stringify(req.query) })
@cacheJson({ keyGenerator: (req: NextApiRequest) => JSON.stringify(req.query) })
static async call(req: NextApiRequest, res: NextApiResponse) {
// Access additional parameters from req.query
const { args } = req.query;
Expand Down