Since Apollo Server v4 it's recommend to use Keyv as a database adapter for cache and as Keyv improved support for cache compression, this project is no longer needed and will not be maintained anymore.
Instead I recommend using:
import { KeyvAdapter } from '@apollo/utils.keyvadapter';
import KeyvBrotli from '@keyv/compress-brotli';
import Keyv from 'keyv';
import zlib from 'zlib';
const keyv = new Keyv(process.env.REDIS_HOST, {
...
compression: new KeyvBrotli({
compressOptions: {
params: {
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
[zlib.constants.BROTLI_PARAM_QUALITY]: 3,
},
},
}),
...
});
const server = new ApolloServer<ApolloContext>({
...
cache: new KeyvAdapter(keyv, { disableBatchReads: true }),
...
});
This package exports an implementation of KeyValueCache
that allows wrapping any other
Apollo KeyValueCache
implementation with a
configurable (default: zlib deflate
) compression layer. Its main goal is
to limit the amount of memory used by the caching environment and at the same time the amount of
data being in-transit from and to the caching environment.
const { RedisCache } = require('apollo-server-cache-redis');
const { CompressionCacheWrapper } = require('apollo-server-compression-cache-wrapper');
const 'zlib' = require('zlib');
const redisCache = new RedisCache({
host: 'redis-server',
});
const server = new ApolloServer({
typeDefs,
resolvers,
cache: new CompressionCacheWrapper(redisCache, {
compress: (data: Buffer) => zlib.deflateSync(data, { level: 1 }),
decompress: (data: Buffer) => zlib.inflateSync(data),
minimumCompressionSize: 262144,
}),
dataSources: () => ({
moviesAPI: new MoviesAPI(),
}),
});
- compress (default:
zlib deflate
) - defines a custom compression method taking and returning aBuffer
. - decompress (default:
zlib deflate
) - defines a custom decompression method taking and returning aBuffer
. - minimumCompressionSize (default: 262144) - defines minimal length of the data string, after exceeding which data proxied to wrapped cache are compressed before being passed forward.
For better performance monitor of CompressionCacheWrapper module in your app, run your app with DEBUG env.
To get all debug messages from all modules:
DEBUG=* npm run start
To get debug messages only from compression-wrapper module:
DEBUG=compression-wrapper npm run start