Skip to content

Feat: Query Cache Implementation #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"@react-native/eslint-config": "^0.72.2",
"@tsconfig/react-native": "^3.0.5",
"@types/jest": "^28.1.2",
"@types/react": "~17.0.21",
"@types/react": "^18.3.10",
"@types/unzipper": "^0.10.9",
"clang-format": "^1.8.0",
"del-cli": "^5.0.0",
Expand All @@ -97,7 +97,7 @@
"zx": "^7.2.3"
},
"resolutions": {
"@types/react": "17.0.21"
"@types/react": "^18.3.10"
},
"peerDependencies": {
"@prisma/client": "*",
Expand Down
1 change: 1 addition & 0 deletions scripts/bump-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import { $ } from 'zx';

import { downloadEngine, ensureNpmTag, writeVersionFile } from './utils';

async function main() {
Expand Down
81 changes: 81 additions & 0 deletions src/CacheManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { useCallback, useEffect, useSyncExternalStore } from 'react';

export default class CacheManager {
private queue = new Map<string, NodeJS.Timeout>();
private listeners = new Map<string, Set<() => void>>();
private cache = new Map<string, Record<string, { data: any }>>();

private get(model: string) {
return this.cache.get(model);
}

set(model: string, key: string, data: any) {
const cache = this.get(model);
this.cache.set(model, { ...cache, [key]: { data } });
}

getSnapshot(model: string, key: string) {
const cache = this.get(model) || {};
return cache[key]?.data;
}

exist(model: string, key: string) {
const cache = this.get(model) || {};
return Boolean(cache[key]);
}

subscribe(model: string, listener: () => Promise<void>) {
if (!this.listeners.has(model)) {
this.listeners.set(model, new Set());
}

const listeners = this.listeners.get(model);
listeners?.add(listener);

return () => listeners?.delete(listener);
}

notifySubscribers(model: string) {
if (this.queue.has(model)) {
clearTimeout(this.queue.get(model)!);
}

const timer = setTimeout(() => {
const listeners = this.listeners.get(model);
listeners?.forEach((listener) => listener());
}, 100); // 100ms debounce, we can adjust this value as needed

this.queue.set(model, timer);
}
}

export const cache = new CacheManager();

// A custom hook for reading data from cache and subscribing to updates
export const usePrismaCache = <T>(
model: string,
key: string,
queryFn: () => Promise<T>
) => {
const listener = useCallback(
(callback: () => void) => async () => {
const data = await queryFn();
cache.set(model, key, data);
callback();
},
[]
);

const store = useSyncExternalStore<Awaited<T>>(
(cb) => cache.subscribe(model, listener(cb)),
() => cache.getSnapshot(model, key)
);

useEffect(() => {
if (!cache.exist(model, key)) {
cache.notifySubscribers(model);
}
}, []);

return store;
};
Loading