From af51ca0337d884de8ae44caa95125f0ad3eb4675 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 7 Nov 2019 19:49:02 +0800 Subject: [PATCH] refactor: remove Content-Type cache --- lib/middleware/cache.js | 78 +++++------------------------------- lib/routes/index.js | 4 +- lib/routes/weibo/timeline.js | 1 + package.json | 1 - yarn.lock | 5 --- 5 files changed, 13 insertions(+), 76 deletions(-) diff --git a/lib/middleware/cache.js b/lib/middleware/cache.js index 2b4a99fb23..b1afffb491 100644 --- a/lib/middleware/cache.js +++ b/lib/middleware/cache.js @@ -4,12 +4,11 @@ const Lru = require('lru-cache'); const md5 = require('@/utils/md5'); const config = require('@/config').value; const logger = require('@/utils/logger'); -const pathToRegExp = require('path-to-regexp'); module.exports = function(app, options = {}) { let available = false; - const { prefix = 'koa-redis-cache:', expire = config.cache.routeExpire, routes = ['(.*)'], exclude = ['/'], passParam = '', maxLength = Infinity, ignoreQuery = true } = options; + const { prefix = 'koa-redis-cache:', expire = config.cache.routeExpire, passParam = '', maxLength = Infinity, ignoreQuery = true } = options; const globalCache = { get: null, @@ -148,27 +147,19 @@ module.exports = function(app, options = {}) { return v; }; - async function getCache(ctx, key, tkey) { + async function getCache(ctx, key) { const value = await globalCache.get(key); - let type; let ok = false; if (value) { ctx.response.status = 200; - type = (await globalCache.get(tkey)) || 'text/html'; - // can happen if user specified return_buffers: true in redis options - if (Buffer.isBuffer(type)) { - type = type.toString(); - } if (config.cache.type === 'redis') { ctx.response.set({ 'X-Koa-Redis-Cache': 'true', - 'Content-Type': type, }); } else if (config.cache.type === 'memory') { ctx.response.set({ 'X-Koa-Memory-Cache': 'true', - 'Content-Type': type, }); } try { @@ -182,18 +173,11 @@ module.exports = function(app, options = {}) { return ok; } - async function setCache(ctx, key, tkey, expire) { - ctx.state.data.lastBuildDate = new Date().toUTCString(); - const body = JSON.stringify(ctx.state.data); - - // if (Buffer.byteLength(body) > maxLength) { - // return; - // } - await globalCache.set(key, body, expire); - - const type = ctx.response.headers['content-type']; - if (type) { - await globalCache.set(tkey, type, expire); + async function setCache(ctx, key, expire) { + if (ctx.response.get('Cache-Control') !== 'no-cache' && ctx.state && ctx.state.data) { + ctx.state.data.lastBuildDate = new Date().toUTCString(); + const body = JSON.stringify(ctx.state.data); + await globalCache.set(key, body, expire); } } @@ -201,53 +185,14 @@ module.exports = function(app, options = {}) { const { url, path } = ctx.request; const resolvedPrefix = typeof prefix === 'function' ? prefix.call(ctx, ctx) : prefix; const key = resolvedPrefix + md5(ignoreQuery ? path : url); - const tkey = key + ':type'; - const validityCheck = (routes, exclude, path) => { - let match = false; - const routeExpire = false; - - const paired = (route, path) => { - const options = { - sensitive: true, - strict: true, - }; - return pathToRegExp(route, [], options).exec(path); - }; - - for (let i = 0; i < routes.length; i++) { - const route = routes[i]; - // if (typeof routes[i] === 'object') { - // route = routes[i].path; - // routeExpire = routes[i].expire; - // } - if (paired(route, path)) { - match = true; - break; - } - } - - for (let j = 0; j < exclude.length; j++) { - if (paired(exclude[j], path)) { - match = false; - break; - } - } - - return { match, routeExpire }; - }; - - const validity = validityCheck(routes, exclude, path); - const match = validity.match; - let routeExpire = validity.routeExpire; - - if (!available || !match || (passParam && ctx.request.query[passParam])) { + if (!available || (passParam && ctx.request.query[passParam])) { return await next(); } let ok = false; try { - ok = await getCache(ctx, key, tkey); + ok = await getCache(ctx, key); } catch (e) { ok = false; } @@ -258,11 +203,10 @@ module.exports = function(app, options = {}) { await next(); try { - const trueExpire = routeExpire || expire; - await setCache(ctx, key, tkey, trueExpire); + const trueExpire = expire; + setCache(ctx, key, trueExpire); } catch (e) { // } - routeExpire = false; }; }; diff --git a/lib/routes/index.js b/lib/routes/index.js index 6fcbd166ee..e55db0c95e 100644 --- a/lib/routes/index.js +++ b/lib/routes/index.js @@ -13,6 +13,7 @@ try { module.exports = async (ctx) => { ctx.set({ 'Content-Type': 'text/html; charset=UTF-8', + 'Cache-Control': 'no-cache', }); const routes = Object.keys(ctx.debug.routes).sort((a, b) => ctx.debug.routes[b] - ctx.debug.routes[a]); @@ -63,9 +64,6 @@ module.exports = async (ctx) => { const stats = await pidusage(process.pid); - ctx.set({ - 'Cache-Control': 'no-cache', - }); ctx.body = art(path.resolve(__dirname, '../views/welcome.art'), { showDebug, debug: [ diff --git a/lib/routes/weibo/timeline.js b/lib/routes/weibo/timeline.js index 2a7c12722a..d812209067 100644 --- a/lib/routes/weibo/timeline.js +++ b/lib/routes/weibo/timeline.js @@ -66,6 +66,7 @@ module.exports = async (ctx) => { ctx.set({ 'Content-Type': 'text/html; charset=UTF-8', + 'Cache-Control': 'no-cache', }); ctx.body = ``; } diff --git a/package.json b/package.json index 332c3c917c..86b4acbb07 100644 --- a/package.json +++ b/package.json @@ -88,7 +88,6 @@ "mailparser": "2.7.6", "markdown-it": "10.0.0", "module-alias": "2.2.2", - "path-to-regexp": "3.1.0", "pidusage": "2.0.17", "plist": "3.0.1", "puppeteer": "2.0.0", diff --git a/yarn.lock b/yarn.lock index ef82cc8f0d..2387522919 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8203,11 +8203,6 @@ path-to-regexp@0.1.7: resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c" integrity sha1-32BBeABfUi8V60SQ5yR6G/qmf4w= -path-to-regexp@3.1.0: - version "3.1.0" - resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-3.1.0.tgz#f45a9cc4dc6331ae8f131e0ce4fde8607f802367" - integrity sha512-PtHLisEvUOepjc+sStXxJ/pDV/s5UBTOKWJY2SOz3e6E/iN/jLknY9WL72kTwRrwXDUbZTEAtSnJbz2fF127DA== - path-to-regexp@^1.1.1: version "1.7.0" resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-1.7.0.tgz#59fde0f435badacba103a84e9d3bc64e96b9937d"