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 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
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
import { Plugin } from 'graphql-yoga';

export function useCachedMetadata(): Plugin {
const cache = new Map<string, any>();
export type CacheMetadataPluginConfig = {
cacheGetter: (key: string) => any;
cacheSetter: (key: string, value: any) => void;
operationsToCache: string[];
};

export function useCachedMetadata(config: CacheMetadataPluginConfig): Plugin {
const computeCacheKey = (serverContext: any) => {
const workspaceId = serverContext.req.workspace?.id ?? 'anonymous';
const cacheVersion = serverContext.req.cacheVersion ?? '0';

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

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

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

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

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

return endResponse(earlyResponse);
}
},
onResponse: async ({ response, serverContext }) => {
if (!config.operationsToCache.includes(getOperationName(serverContext))) {
return;
}

const cacheKey = computeCacheKey(serverContext);

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

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

cache.set(cacheKey, responseBody);
config.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,15 @@ export const metadataModuleFactory = async (
useExceptionHandler({
exceptionHandlerService,
}),
useCachedMetadata(),
useCachedMetadata({
cacheGetter: workspaceSchemaCacheStorage.get.bind(
workspaceSchemaCacheStorage,
),
cacheSetter: workspaceSchemaCacheStorage.set.bind(
workspaceSchemaCacheStorage,
),
operationsToCache: ['ObjectMetadataItems'],
}),
],
path: '/metadata',
context: () => ({
Expand Down
Loading