diff --git a/apps/functional_chat/app/lib/cache.ts b/apps/functional_chat/app/lib/cache.ts index c18d79b..8648dcc 100644 --- a/apps/functional_chat/app/lib/cache.ts +++ b/apps/functional_chat/app/lib/cache.ts @@ -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; @@ -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; }; } \ No newline at end of file diff --git a/apps/functional_chat/pages/api/functions/newsSearch.ts b/apps/functional_chat/pages/api/functions/newsSearch.ts index 0cc46ab..cad0a40 100644 --- a/apps/functional_chat/pages/api/functions/newsSearch.ts +++ b/apps/functional_chat/pages/api/functions/newsSearch.ts @@ -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) { @@ -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; diff --git a/apps/functional_chat/pages/api/functions/renderChart.ts b/apps/functional_chat/pages/api/functions/renderChart.ts index 5d66b6c..3be76d9 100644 --- a/apps/functional_chat/pages/api/functions/renderChart.ts +++ b/apps/functional_chat/pages/api/functions/renderChart.ts @@ -1,4 +1,5 @@ import type { NextApiRequest, NextApiResponse } from 'next'; +import { cacheSend } from '~/lib/cache'; class Api { static async spec(req: NextApiRequest, res: NextApiResponse) { @@ -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; diff --git a/apps/functional_chat/pages/api/functions/stockQuote.ts b/apps/functional_chat/pages/api/functions/stockQuote.ts index c13a045..1f3e0d0 100644 --- a/apps/functional_chat/pages/api/functions/stockQuote.ts +++ b/apps/functional_chat/pages/api/functions/stockQuote.ts @@ -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) { @@ -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;