Add config for GQL requests cache (#228)

* Add config and a method to set cache control settings for GQL requests

* Add option for max size in GQL cache config

* Fix failing tests
This commit is contained in:
prathamesh0
2022-11-15 14:56:08 +05:30
committed by GitHub
parent 6f8ededd52
commit a52bdf64b1
5 changed files with 33 additions and 40 deletions
+10
View File
@@ -29,6 +29,13 @@ export interface JobQueueConfig {
prefetchBlockCount: number;
}
export interface GQLCacheConfig {
enabled: boolean;
maxCacheSize?: number;
maxAge: number;
timeTravelMaxAge: number;
}
export interface ServerConfig {
host: string;
port: number;
@@ -54,6 +61,9 @@ export interface ServerConfig {
// Boolean to load GQL query nested entity relations sequentially.
loadRelationsSequential: boolean;
// GQL cache-control max-age settings (in seconds)
gqlCache: GQLCacheConfig
}
export interface UpstreamConfig {
+2
View File
@@ -23,3 +23,5 @@ export const UNKNOWN_EVENT_NAME = '__unknown__';
export const KIND_ACTIVE = 'active';
export const KIND_LAZY = 'lazy';
export const DEFAULT_MAX_GQL_CACHE_SIZE = Math.pow(2, 20) * 8; // 8 MB
+16 -1
View File
@@ -9,17 +9,20 @@ import { hideBin } from 'yargs/helpers';
import { utils, providers } from 'ethers';
import JSONbig from 'json-bigint';
import Decimal from 'decimal.js';
import { GraphQLResolveInfo } from 'graphql';
import _ from 'lodash';
import { EthClient } from '@cerc-io/ipld-eth-client';
import { DEFAULT_CONFIG_PATH } from './constants';
import { Config } from './config';
import { GQLCacheConfig, Config } from './config';
import { JobQueue } from './job-queue';
import { GraphDecimal } from './graph-decimal';
import * as EthDecoder from './eth';
import { getCachedBlockSize } from './block-size-cache';
import { ResultEvent } from './indexer';
import { EventInterface } from './types';
import { BlockHeight } from './database';
const JSONbigNative = JSONbig({ useNativeBigInt: true });
@@ -287,3 +290,15 @@ export const getResultEvent = (event: EventInterface): ResultEvent => {
proof: JSON.parse(event.proof)
};
};
export const setGQLCacheHints = (info: GraphQLResolveInfo, block: BlockHeight, gqlCache: GQLCacheConfig): void => {
if (!gqlCache || !gqlCache.enabled) {
return;
}
assert(gqlCache.maxAge, 'Missing server gqlCache.maxAge');
assert(gqlCache.timeTravelMaxAge, 'Missing server gqlCache.timeTravelMaxAge');
const maxAge = _.isEmpty(block) ? gqlCache.maxAge : gqlCache.timeTravelMaxAge;
info.cacheControl.setCacheHint({ maxAge });
};