UNPKG

915 BJavaScriptView Raw
1module.exports = ({
2 router,
3 cache,
4 asyncMiddleware,
5 getConfig,
6 handleResponse,
7}) => {
8
9 /**
10 * @swagger
11 * /cache/clear:
12 * get:
13 * tags:
14 * - cache
15 * summary: Clear cache
16 * description: Clears the LRU cache of API responses
17 * produces:
18 * - text/plain
19 * responses:
20 * 200:
21 * description: Result
22 */
23 router.get(
24 '/cache/clear.:ext?',
25 asyncMiddleware(async (req, res) => {
26 const config = await getConfig();
27
28 if (!config.cache.enabled) {
29 handleResponse(req, res, 'Cache disabled');
30 return;
31 }
32
33 const items = [];
34
35 cache.forEach((value, key) => {
36 if (key.indexOf(req.session.slug) === 0) {
37 items.push(key);
38 }
39 });
40
41 items.forEach(key => cache.del(key));
42
43 handleResponse(req, res, `${items.length} items removed from cache`);
44 })
45 );
46
47};