diff --git a/src/hooks/helpers/path.js b/src/hooks/helpers/path.js index 38b4318..7a3ee74 100644 --- a/src/hooks/helpers/path.js +++ b/src/hooks/helpers/path.js @@ -15,12 +15,13 @@ function parseNestedPath(path, params) { function parsePath(hook, config = {removePathFromCacheKey: false, parseNestedRoutes: false}) { const q = hook.params.query || {}; + const paginate = hook.params.paginate === false ? 'off' : 'on'; // if `.paginate` is underfined it means pagination hook is enabled, that's why we are using strict check with false const remove = config.removePathFromCacheKey; const parseNestedRoutes = config.parseNestedRoutes; - let path = remove && hook.id ? '' : `${hook.path}`; + let path = remove && hook.id ? `paginate:${paginate}:` : `paginate:${paginate}:${hook.path}`; if (!remove && parseNestedRoutes) { - path = parseNestedPath(path, hook.params); + path = `paginate:${paginate}:${parseNestedPath(path, hook.params)}`; } if (hook.id) { diff --git a/src/routes/cache.js b/src/routes/cache.js index 264ed31..792f84a 100644 --- a/src/routes/cache.js +++ b/src/routes/cache.js @@ -37,37 +37,53 @@ function routes(app) { } // Gets the value of a key in the redis client - client.get(`${target}`, (err, reply) => { - if (err) { - res.status(HTTP_SERVER_ERROR).json({ - message: 'something went wrong' + err.message - }); - } else { - // If the key existed - if (reply) { - // Clear existing cached key - h.clearSingle(target).then(r => { - res.status(HTTP_OK).json({ - message: `cache cleared for key (${hasQueryString ? - 'with' : 'without'} params): ${target}`, - status: HTTP_OK - }); + const clearSingleKey = (key) => new Promise((resolve) => { + client.get(key, (err, reply) => { + if (err) { + res.status(HTTP_SERVER_ERROR).json({ + message: 'something went wrong' + err.message }); } else { - /** - * Empty reply means the key does not exist. - * Must use HTTP_OK with express as HTTP's RFC stats 204 should not - * provide a body, message would then be lost. - */ - res.status(HTTP_OK).json({ - message: `cache already cleared for key (${hasQueryString ? - 'with' : 'without'} params): ${target}`, - status: HTTP_NO_CONTENT - }); + // If the key existed + if (reply) { + // Clear existing cached key + h.clearSingle(key) + .then(r => { + resolve({ + message: `cache cleared for key (${hasQueryString ? 'with' : 'without'} params): ${key}`, + status: HTTP_OK + }); + }); + } else { + /** + * Empty reply means the key does not exist. + * Must use HTTP_OK with express as HTTP's RFC stats 204 should not + * provide a body, message would then be lost. + */ + resolve({ + message: `cache already cleared for key (${hasQueryString ? 'with' : 'without'} params): ${key}`, + status: HTTP_NO_CONTENT + }); + } } - - } + }); }); + + Promise.all([ + clearSingleKey(`paginate:on:${target}`), + clearSingleKey(`paginate:off:${target}`), + ]) + .then(([withPagResult, witoutPagResult]) => { + if (withPagResult.status === HTTP_OK) { + res.status(HTTP_OK).json(withPagResult); + } else if (witoutPagResult.status === HTTP_OK) { + res.status(HTTP_OK).json(witoutPagResult); + } else if (withPagResult.status !== HTTP_OK) { + res.status(HTTP_OK).json(withPagResult); + } else { + res.status(HTTP_OK).json(witoutPagResult); + } + }); } else { res.status(HTTP_NOT_FOUND).end(); }