mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
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:
12
api/src/utils/compress.ts
Normal file
12
api/src/utils/compress.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { compress as compressSnappy, uncompress as uncompressSnappy } from 'snappy';
|
||||
import { compress as compressJSON, decompress as decompressJSON } from '@directus/shared/utils';
|
||||
|
||||
export async function compress(raw: Record<string, any> | Record<string, any>[]): Promise<Buffer> {
|
||||
if (!raw) return raw;
|
||||
return await compressSnappy(compressJSON(raw));
|
||||
}
|
||||
|
||||
export async function decompress(compressed: Buffer): Promise<any> {
|
||||
if (!compressed) return compressed;
|
||||
return decompressJSON((await uncompressSnappy(compressed, { asBuffer: false })) as string);
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import { Accountability, Permission, SchemaOverview } from '@directus/shared/typ
|
||||
import { deepMap, parseFilter, parseJSON, parsePreset } from '@directus/shared/utils';
|
||||
import { cloneDeep } from 'lodash';
|
||||
import hash from 'object-hash';
|
||||
import { getCache, setSystemCache } from '../cache';
|
||||
import { getCache, getSystemCache, setSystemCache } from '../cache';
|
||||
import getDatabase from '../database';
|
||||
import { appAccessMinimalPermissions } from '../database/system-data/app-access-permissions';
|
||||
import env from '../env';
|
||||
@@ -13,7 +13,7 @@ import { mergePermissionsForShare } from './merge-permissions-for-share';
|
||||
|
||||
export async function getPermissions(accountability: Accountability, schema: SchemaOverview) {
|
||||
const database = getDatabase();
|
||||
const { systemCache, cache } = getCache();
|
||||
const { cache } = getCache();
|
||||
|
||||
let permissions: Permission[] = [];
|
||||
|
||||
@@ -21,7 +21,7 @@ export async function getPermissions(accountability: Accountability, schema: Sch
|
||||
const cacheKey = `permissions-${hash({ user, role, app, admin, share_scope })}`;
|
||||
|
||||
if (env.CACHE_PERMISSIONS !== false) {
|
||||
const cachedPermissions = await systemCache.get(cacheKey);
|
||||
const cachedPermissions = await getSystemCache(cacheKey);
|
||||
|
||||
if (cachedPermissions) {
|
||||
if (!cachedPermissions.containDynamicData) {
|
||||
|
||||
@@ -3,7 +3,7 @@ import { Accountability, Filter, SchemaOverview } from '@directus/shared/types';
|
||||
import { parseJSON, toArray } from '@directus/shared/utils';
|
||||
import { Knex } from 'knex';
|
||||
import { mapValues } from 'lodash';
|
||||
import { getCache, setSystemCache } from '../cache';
|
||||
import { getSystemCache, setSystemCache } from '../cache';
|
||||
import { ALIAS_TYPES } from '../constants';
|
||||
import getDatabase from '../database';
|
||||
import { systemCollectionRows } from '../database/system-data/collections';
|
||||
@@ -20,7 +20,6 @@ export async function getSchema(options?: {
|
||||
}): Promise<SchemaOverview> {
|
||||
const database = options?.database || getDatabase();
|
||||
const schemaInspector = SchemaInspector(database);
|
||||
const { systemCache } = getCache();
|
||||
|
||||
let result: SchemaOverview;
|
||||
|
||||
@@ -28,7 +27,7 @@ export async function getSchema(options?: {
|
||||
let cachedSchema;
|
||||
|
||||
try {
|
||||
cachedSchema = (await systemCache.get('schema')) as SchemaOverview;
|
||||
cachedSchema = (await getSystemCache('schema')) as SchemaOverview;
|
||||
} catch (err: any) {
|
||||
logger.warn(err, `[schema-cache] Couldn't retrieve cache. ${err}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user