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

Remove cache flushing on startup #18238

Merged
merged 5 commits into from Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions api/src/app.ts
Expand Up @@ -7,7 +7,6 @@ import { createRequire } from 'node:module';
import path from 'path';
import qs from 'qs';
import { registerAuthProviders } from './auth.js';
import { flushCaches } from './cache.js';
import activityRouter from './controllers/activity.js';
import assetsRouter from './controllers/assets.js';
import authRouter from './controllers/auth.js';
Expand Down Expand Up @@ -91,8 +90,6 @@ export default async function createApp(): Promise<express.Application> {
logger.warn(`Database migrations have not all been run`);
}

await flushCaches();

await registerAuthProviders();

const extensionManager = getExtensionManager();
Expand Down
12 changes: 12 additions & 0 deletions api/src/database/migrations/run.ts
Expand Up @@ -5,6 +5,7 @@ import { orderBy } from 'lodash-es';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import path from 'path';
import { flushCaches } from '../../cache.js';
import env from '../../env.js';
import logger from '../../logger.js';
import type { Migration } from '../../types/index.js';
Expand Down Expand Up @@ -77,6 +78,8 @@ export default async function run(database: Knex, direction: 'up' | 'down' | 'la

await up(database);
await database.insert({ version: nextVersion.version, name: nextVersion.name }).into('directus_migrations');

await flushCaches(true);
}

async function down() {
Expand All @@ -100,11 +103,16 @@ export default async function run(database: Knex, direction: 'up' | 'down' | 'la

await down(database);
await database('directus_migrations').delete().where({ version: migration.version });

await flushCaches(true);
}

async function latest() {
let needsCacheFlush = false;

for (const migration of migrations) {
if (migration.completed === false) {
needsCacheFlush = true;
const { up } = await import(`file://${migration.file}`);

if (log) {
Expand All @@ -115,5 +123,9 @@ export default async function run(database: Knex, direction: 'up' | 'down' | 'la
await database.insert({ version: migration.version, name: migration.name }).into('directus_migrations');
}
}

if (needsCacheFlush) {
await flushCaches(true);
}
}
}
2 changes: 2 additions & 0 deletions api/src/utils/get-cache-key.ts
Expand Up @@ -2,12 +2,14 @@ import type { Request } from 'express';
import hash from 'object-hash';
import url from 'url';
import { getGraphqlQueryAndVariables } from './get-graphql-query-and-variables.js';
import { version } from './package.js';

export function getCacheKey(req: Request): string {
const path = url.parse(req.originalUrl).pathname;
const isGraphQl = path?.startsWith('/graphql');

const info = {
version,
user: req.accountability?.user || null,
path,
query: isGraphQl ? getGraphqlQueryAndVariables(req) : req.sanitizedQuery,
Expand Down