Add a cache clear method to the UtilsService (#19990)

Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch>
This commit is contained in:
Rob Lee
2023-10-11 16:14:59 -04:00
committed by GitHub
parent 292e3b4742
commit 51a2fcbd53
4 changed files with 21 additions and 7 deletions

View File

@@ -0,0 +1,5 @@
---
"@directus/api": minor
---
Added `cacheClear` method to the `UtilsService`

View File

@@ -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();
})

View File

@@ -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<void> {
if (this.accountability?.admin !== true) {
throw new ForbiddenError();
}
return flushCaches(true);
}
}

View File

@@ -79,3 +79,4 @@
- koredeycode
- sajjadalis
- Nihcep
- nodeworks