aboutsummaryrefslogtreecommitdiffstats
path: root/src/app/cached-functions.ts
diff options
context:
space:
mode:
authorSebastien Castiel <sebastien@castiel.me>2024-01-29 22:37:13 -0500
committerSebastien Castiel <sebastien@castiel.me>2024-01-30 12:57:21 -0500
commit9e300e0ff0c9d93cbaeaa86c1e2a560523107382 (patch)
treeebeaea60117fbac781a415e3299395f3f8e637d7 /src/app/cached-functions.ts
parent3847a67a192a04098643e19953549a99ca152b8a (diff)
Use React’s cache to avoid some queries to the database
Diffstat (limited to 'src/app/cached-functions.ts')
-rw-r--r--src/app/cached-functions.ts17
1 files changed, 17 insertions, 0 deletions
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<P extends any[], R>(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),
+}