From 51a2fcbd53108f7aebd876fc05bbc01b19430dc5 Mon Sep 17 00:00:00 2001 From: Rob Lee Date: Wed, 11 Oct 2023 16:14:59 -0400 Subject: [PATCH] Add a cache clear method to the UtilsService (#19990) Co-authored-by: Pascal Jufer --- .changeset/ten-icons-kiss.md | 5 +++++ api/src/controllers/utils.ts | 12 ++++++------ api/src/services/utils.ts | 10 +++++++++- contributors.yml | 1 + 4 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 .changeset/ten-icons-kiss.md diff --git a/.changeset/ten-icons-kiss.md b/.changeset/ten-icons-kiss.md new file mode 100644 index 0000000000..9e823da1a1 --- /dev/null +++ b/.changeset/ten-icons-kiss.md @@ -0,0 +1,5 @@ +--- +"@directus/api": minor +--- + +Added `cacheClear` method to the `UtilsService` diff --git a/api/src/controllers/utils.ts b/api/src/controllers/utils.ts index 327b2bd886..6d5adfc8c9 100644 --- a/api/src/controllers/utils.ts +++ b/api/src/controllers/utils.ts @@ -4,8 +4,7 @@ import { Router } from 'express'; import Joi from 'joi'; import fs from 'node:fs'; import { createRequire } from 'node:module'; -import { flushCaches } from '../cache.js'; -import { ForbiddenError, InvalidPayloadError, InvalidQueryError, UnsupportedMediaTypeError } from '../errors/index.js'; +import { InvalidPayloadError, InvalidQueryError, UnsupportedMediaTypeError } from '../errors/index.js'; import collectionExists from '../middleware/collection-exists.js'; import { respond } from '../middleware/respond.js'; import type { ImportWorkerData } from '../services/import-export/import-worker.js'; @@ -194,11 +193,12 @@ router.post( router.post( '/cache/clear', asyncHandler(async (req, res) => { - if (req.accountability?.admin !== true) { - throw new ForbiddenError(); - } + const service = new UtilsService({ + accountability: req.accountability, + schema: req.schema, + }); - await flushCaches(true); + await service.clearCache(); res.status(200).end(); }) diff --git a/api/src/services/utils.ts b/api/src/services/utils.ts index bfae4f17ea..036a5da4ef 100644 --- a/api/src/services/utils.ts +++ b/api/src/services/utils.ts @@ -1,11 +1,11 @@ import type { Accountability, SchemaOverview } from '@directus/types'; import type { Knex } from 'knex'; +import { flushCaches, getCache } from '../cache.js'; import getDatabase from '../database/index.js'; import { systemCollectionRows } from '../database/system-data/collections/index.js'; import emitter from '../emitter.js'; import { ForbiddenError, InvalidPayloadError } from '../errors/index.js'; import type { AbstractServiceOptions, PrimaryKey } from '../types/index.js'; -import { getCache } from '../cache.js'; import { shouldClearCache } from '../utils/should-clear-cache.js'; export class UtilsService { @@ -147,4 +147,12 @@ export class UtilsService { } ); } + + async clearCache(): Promise { + if (this.accountability?.admin !== true) { + throw new ForbiddenError(); + } + + return flushCaches(true); + } } diff --git a/contributors.yml b/contributors.yml index 2cb00edc06..adbb4e85bf 100644 --- a/contributors.yml +++ b/contributors.yml @@ -79,3 +79,4 @@ - koredeycode - sajjadalis - Nihcep +- nodeworks