Skip to content
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

New cache revalidation system #9

Merged
merged 1 commit into from
Jan 16, 2025
Merged
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
12 changes: 1 addition & 11 deletions app/(api)/_actions/handleApolloRequest.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use server';

import { revalidatePath, revalidateTag } from 'next/cache';

import handler from '@datalib/apolloServer';

export default async function handleApolloRequest(
query: string,
variables: object,
revalidateCache?: { path?: string; type?: 'page' | 'layout'; tag?: string }
variables: object
) {
const headers = {
'Content-Type': 'application/json',
Expand All @@ -25,12 +22,5 @@ export default async function handleApolloRequest(

const res = await handler(req);

if (revalidateCache?.path) {
revalidatePath(revalidateCache.path, revalidateCache.type);
}
if (revalidateCache?.tag) {
revalidateTag(revalidateCache.tag);
}

return res.json();
}
12 changes: 12 additions & 0 deletions app/(api)/_actions/revalidateCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use server';

import { revalidatePath } from 'next/cache';
import revalidationPaths, { RevalidationKey } from '@cache/revalidationPaths';

export default async function revalidateCache(keys: RevalidationKey[]) {
for (const key of keys) {
for (const path of revalidationPaths[key]) {
revalidatePath(path);
}
}
}
9 changes: 9 additions & 0 deletions app/(api)/_cache/revalidationPaths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type RevalidationKey = 'users' | 'playlists' | 'songs';

const revalidationPaths = {
users: ['/'],
playlists: ['/'],
songs: ['/'],
};

export default revalidationPaths;
5 changes: 4 additions & 1 deletion app/(api)/_datalib/_services/Playlists.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import prisma from '../_prisma/client';
import { PlaylistInput } from '@datatypes/Playlist';
import revalidateCache from '@actions/revalidateCache';

export default class Playlists {
// CREATE
Expand All @@ -11,6 +12,7 @@ export default class Playlists {
name,
},
});
revalidateCache(['playlists']);
return playlist;
}

Expand All @@ -32,6 +34,7 @@ export default class Playlists {
songId,
},
});
revalidateCache(['playlists', 'songs']);
return true;
} catch (e) {
return false;
Expand All @@ -54,7 +57,7 @@ export default class Playlists {
},
},
});

revalidateCache(['playlists', 'songs']);
return songs;
}
}
2 changes: 2 additions & 0 deletions app/(api)/_datalib/_services/Songs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import prisma from '../_prisma/client';
import { SongInput } from '@datatypes/Song';
import revalidateCache from '@actions/revalidateCache';

export default class Songs {
// CREATE
Expand All @@ -10,6 +11,7 @@ export default class Songs {
name,
},
});
revalidateCache(['songs']);
return song;
}

Expand Down
4 changes: 4 additions & 0 deletions app/(api)/_datalib/_services/Users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { UserInput } from '@datatypes/User';
import prisma from '../_prisma/client';
import revalidateCache from '@actions/revalidateCache';

export default class Users {
// CREATE
Expand All @@ -10,6 +11,7 @@ export default class Users {
name,
},
});
revalidateCache(['users']);
return user;
}

Expand Down Expand Up @@ -42,6 +44,7 @@ export default class Users {
},
data: input,
});
revalidateCache(['users']);
return user;
} catch (e) {
return null;
Expand All @@ -56,6 +59,7 @@ export default class Users {
id,
},
});
revalidateCache(['users']);
return true;
} catch (e) {
return false;
Expand Down
5 changes: 2 additions & 3 deletions app/(pages)/_utils/sendApolloRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import handleApolloRequest from '@actions/handleApolloRequest';

export default async function sendApolloRequest(
query: DocumentNode,
variables: object,
revalidateCache?: { path?: string; type?: 'page' | 'layout'; tag?: string }
variables: object
) {
return handleApolloRequest(print(query), variables, revalidateCache);
return handleApolloRequest(print(query), variables);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ const query = gql`
// Example IDs
const variables = {
ids: [
'7bae85c0-7862-4d8e-b991-41c73d51936d',
'9e327de1-38f1-4324-9427-48ec9a670ca3',
'47ca283a-2178-4760-814f-7f847f580af3',
// '7bae85c0-7862-4d8e-b991-41c73d51936d',
// '9e327de1-38f1-4324-9427-48ec9a670ca3',
// '47ca283a-2178-4760-814f-7f847f580af3',
],
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const query = gql`
// Example IDs
const variables = {
ids: [
'7bae85c0-7862-4d8e-b991-41c73d51936d',
'9e327de1-38f1-4324-9427-48ec9a670ca3',
'47ca283a-2178-4760-814f-7f847f580af3',
// '7bae85c0-7862-4d8e-b991-41c73d51936d',
// '9e327de1-38f1-4324-9427-48ec9a670ca3',
// '47ca283a-2178-4760-814f-7f847f580af3',
],
};

Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@actions/*": ["./app/(api)/_actions/*"],
"@datalib/*": ["./app/(api)/_datalib/*"],
"@datatypes/*": ["./app/(api)/_types/*"],
"@cache/*": ["./app/(api)/_cache/*"],
}
},
"include": ["nextEnv.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
Expand Down