Improve cache performance by compressing records (#14833)

* Utils to compress/decompress data
Gzip was chosen because we want smaller data but quick algorithm since this will be ran for every request

* Compress system cache

* Decompress system cache

* Set/Get compressed cache for individual requests

* Switch from gzip to snappy, use json compression too

* Fix cache exp set/get

* Remove unused import

Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
This commit is contained in:
José Varela
2022-08-04 22:35:27 +01:00
committed by GitHub
parent 1300f5c3de
commit cc343fdf91
9 changed files with 203 additions and 14 deletions

View File

@@ -4,6 +4,7 @@ import env from './env';
import logger from './logger';
import { getConfigFromEnv } from './utils/get-config-from-env';
import { validateEnv } from './utils/validate-env';
import { compress, decompress } from './utils/compress';
let cache: Keyv | null = null;
let systemCache: Keyv | null = null;
@@ -50,10 +51,33 @@ export async function setSystemCache(key: string, value: any, ttl?: number): Pro
const { systemCache, lockCache } = getCache();
if (!(await lockCache.get('system-cache-lock'))) {
await systemCache.set(key, value, ttl);
await setCacheValue(systemCache, key, value, ttl);
}
}
export async function getSystemCache(key: string): Promise<Record<string, any>> {
const { systemCache } = getCache();
return await getCacheValue(systemCache, key);
}
export async function setCacheValue(
cache: Keyv,
key: string,
value: Record<string, any> | Record<string, any>[],
ttl?: number
) {
const compressed = await compress(value);
await cache.set(key, compressed, ttl);
}
export async function getCacheValue(cache: Keyv, key: string): Promise<any> {
const value = await cache.get(key);
if (!value) return undefined;
const decompressed = await decompress(value);
return decompressed;
}
function getKeyvInstance(ttl: number | undefined, namespaceSuffix?: string): Keyv {
switch (env.CACHE_STORE) {
case 'redis':