From 9e300e0ff0c9d93cbaeaa86c1e2a560523107382 Mon Sep 17 00:00:00 2001 From: Sebastien Castiel Date: Mon, 29 Jan 2024 22:37:13 -0500 Subject: Use React’s cache to avoid some queries to the database MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/cached-functions.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/app/cached-functions.ts (limited to 'src/app/cached-functions.ts') diff --git a/src/app/cached-functions.ts b/src/app/cached-functions.ts new file mode 100644 index 0000000..cc148fd --- /dev/null +++ b/src/app/cached-functions.ts @@ -0,0 +1,17 @@ +import { getGroup } from '@/lib/api' +import { cache } from 'react' + +function logAndCache

(fn: (...args: P) => R) { + const cached = cache((...args: P) => { + // console.log(`Not cached: ${fn.name}…`) + return fn(...args) + }) + return (...args: P) => { + // console.log(`Calling cached ${fn.name}…`) + return cached(...args) + } +} + +export const cached = { + getGroup: logAndCache(getGroup), +} -- cgit v1.3