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

Add redis to useMetadataCache yoga plugin #5194

Merged
merged 5 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import { Plugin } from 'graphql-yoga';

export function useCachedMetadata(): Plugin {
const cache = new Map<string, any>();

export function useCachedMetadata(
cacheGetter: (key: string) => any,
cacheSetter: (key: string, value: any) => void,
operationsToCache: string[],
): Plugin {
const computeCacheKey = (serverContext: any) => {
const workspaceId = serverContext.req.workspace?.id ?? 'anonymous';
const cacheVersion = serverContext.req.cacheVersion ?? '0';

return `${workspaceId}:${cacheVersion}`;
};

const operationName = (serverContext: any) =>
serverContext?.req?.body?.operationName;

return {
onRequest: ({ endResponse, serverContext }) => {
const cacheKey = computeCacheKey(serverContext);
const foundInCache = cache.has(cacheKey);
onRequest: async ({ endResponse, serverContext }) => {
if (!operationsToCache.includes(operationName(serverContext))) {
return;
}

if (foundInCache) {
const cachedResponse = cache.get(cacheKey);
const cacheKey = computeCacheKey(serverContext);
const cachedResponse = await cacheGetter(cacheKey);

if (cachedResponse) {
const earlyResponse = Response.json(cachedResponse);

return endResponse(earlyResponse);
Expand All @@ -26,12 +33,12 @@ export function useCachedMetadata(): Plugin {
onResponse: async ({ response, serverContext }) => {
const cacheKey = computeCacheKey(serverContext);

const foundInCache = cache.has(cacheKey);
const cachedResponse = await cacheGetter(cacheKey);

if (!foundInCache) {
if (!cachedResponse) {
const responseBody = await response.json();

cache.set(cacheKey, responseBody);
cacheSetter(cacheKey, responseBody);
}
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@ import { EnvironmentService } from 'src/engine/integrations/environment/environm
import { ExceptionHandlerService } from 'src/engine/integrations/exception-handler/exception-handler.service';
import { DataloaderModule } from 'src/engine/dataloaders/dataloader.module';
import { DataloaderService } from 'src/engine/dataloaders/dataloader.service';
import { CacheStorageNamespace } from 'src/engine/integrations/cache-storage/types/cache-storage-namespace.enum';
import { CacheStorageModule } from 'src/engine/integrations/cache-storage/cache-storage.module';

@Module({
imports: [
GraphQLModule.forRootAsync<YogaDriverConfig>({
driver: YogaDriver,
useFactory: metadataModuleFactory,
imports: [GraphQLConfigModule, DataloaderModule],
inject: [EnvironmentService, ExceptionHandlerService, DataloaderService],
inject: [
EnvironmentService,
ExceptionHandlerService,
DataloaderService,
CacheStorageNamespace.WorkspaceSchema,
],
}),
MetadataEngineModule,
WorkspaceMigrationRunnerModule,
WorkspaceMigrationModule,
CacheStorageModule,
],
})
export class MetadataGraphQLApiModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import { MetadataGraphQLApiModule } from 'src/engine/api/graphql/metadata-graphq
import { renderApolloPlayground } from 'src/engine/utils/render-apollo-playground.util';
import { DataloaderService } from 'src/engine/dataloaders/dataloader.service';
import { useCachedMetadata } from 'src/engine/api/graphql/graphql-config/hooks/use-cached-metadata';
import { CacheStorageService } from 'src/engine/integrations/cache-storage/cache-storage.service';

export const metadataModuleFactory = async (
environmentService: EnvironmentService,
exceptionHandlerService: ExceptionHandlerService,
dataloaderService: DataloaderService,
workspaceSchemaCacheStorage: CacheStorageService,
): Promise<YogaDriverConfig> => {
const config: YogaDriverConfig = {
autoSchemaFile: true,
Expand All @@ -33,7 +35,11 @@ export const metadataModuleFactory = async (
useExceptionHandler({
exceptionHandlerService,
}),
useCachedMetadata(),
useCachedMetadata(
workspaceSchemaCacheStorage.get.bind(workspaceSchemaCacheStorage),
workspaceSchemaCacheStorage.set.bind(workspaceSchemaCacheStorage),
['ObjectMetadataItems'],
),
],
path: '/metadata',
context: () => ({
Expand Down
Loading