From 465324b67dbad550c991b2d0180a1ad90c313d5d Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Thu, 18 Oct 2018 02:44:45 +0100 Subject: [PATCH] =?UTF-8?q?=E7=B2=BE=E7=AE=80=E7=BC=93=E5=AD=98=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E7=B2=BE=E7=AE=80=E6=96=87=E6=A1=A3=20(#915)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/HenryQW/RSSHub/issues/6 https://github.com/HenryQW/RSSHub/issues/7 --- docs/README.md | 12 +++---- middleware/cache-common.js | 65 ++++++++++++++++++++++++++++++++++++++ middleware/lru-cache.js | 60 +++-------------------------------- middleware/redis-cache.js | 60 +++-------------------------------- 4 files changed, 78 insertions(+), 119 deletions(-) create mode 100644 middleware/cache-common.js diff --git a/docs/README.md b/docs/README.md index c5809b9e8c..d20df3ca1a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1494,15 +1494,13 @@ IATA 国际航空运输协会机场代码, 参见[维基百科 国际航空运 ### 中国美术馆 - + - +| 通知公告 | 新闻 | 媒体联报 | 展览预告 | 焦点专题 | +| ------------ | ---- | -------- | ---------- | -------- | +| announcement | news | media | exhibition | specials | - - - - - + ### 国家地理 diff --git a/middleware/cache-common.js b/middleware/cache-common.js new file mode 100644 index 0000000000..b0eacde65c --- /dev/null +++ b/middleware/cache-common.js @@ -0,0 +1,65 @@ +const pathToRegExp = require('path-to-regexp'); +const readall = require('readall'); +const crypto = require('crypto'); + +const paired = (route, path) => { + const options = { + sensitive: true, + strict: true, + }; + + return pathToRegExp(route, [], options).exec(path); +}; + +// eslint-disable-next-line no-unused-vars +const read = (stream) => + new Promise((resolve, reject) => { + readall(stream, (err, data) => { + if (err) { + reject(err); + } else { + resolve(data); + } + }); + }); + +const md5 = (str) => + crypto + .createHash('md5') + .update(str) + .digest('hex'); + +const validityCheck = (routes, exclude, path) => { + let match = false; + let routeExpire = false; + + for (let i = 0; i < routes.length; i++) { + let 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 }; +}; + +module.exports = { + paired, + read, + md5, + validityCheck, +}; diff --git a/middleware/lru-cache.js b/middleware/lru-cache.js index 1937a3e51c..2b10636fa3 100644 --- a/middleware/lru-cache.js +++ b/middleware/lru-cache.js @@ -1,9 +1,7 @@ -// baed on https://github.com/coderhaoxin/koa-redis-cache +// based on https://github.com/coderhaoxin/koa-redis-cache -const pathToRegExp = require('path-to-regexp'); -const readall = require('readall'); -const crypto = require('crypto'); const lru = require('lru-cache'); +const common = require('./cache-common'); module.exports = function(options = {}) { const { @@ -29,31 +27,10 @@ module.exports = function(options = {}) { return async function cache(ctx, next) { const { url, path } = ctx.request; const resolvedPrefix = typeof prefix === 'function' ? prefix.call(ctx, ctx) : prefix; - const key = resolvedPrefix + md5(ignoreQuery ? path : url); + const key = resolvedPrefix + common.md5(ignoreQuery ? path : url); const tkey = key + ':type'; - let match = false; - let routeExpire = false; - for (let i = 0; i < routes.length; i++) { - let 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; - } - } + let { match, routeExpire } = common.validityCheck(routes, exclude, path); if (!match || (passParam && ctx.request.query[passParam])) { return await next(); @@ -134,32 +111,3 @@ module.exports = function(options = {}) { } } }; - -function paired(route, path) { - const options = { - sensitive: true, - strict: true, - }; - - return pathToRegExp(route, [], options).exec(path); -} - -// eslint-disable-next-line no-unused-vars -function read(stream) { - return new Promise((resolve, reject) => { - readall(stream, (err, data) => { - if (err) { - reject(err); - } else { - resolve(data); - } - }); - }); -} - -function md5(str) { - return crypto - .createHash('md5') - .update(str) - .digest('hex'); -} diff --git a/middleware/redis-cache.js b/middleware/redis-cache.js index 91c2858513..059540805c 100644 --- a/middleware/redis-cache.js +++ b/middleware/redis-cache.js @@ -1,10 +1,8 @@ -// baed on https://github.com/coderhaoxin/koa-redis-cache +// based on https://github.com/coderhaoxin/koa-redis-cache -const pathToRegExp = require('path-to-regexp'); const wrapper = require('co-redis'); -const readall = require('readall'); -const crypto = require('crypto'); const Redis = require('redis'); +const common = require('./cache-common'); module.exports = function(options = {}) { let redisAvailable = false; @@ -50,31 +48,10 @@ module.exports = function(options = {}) { return async function cache(ctx, next) { const { url, path } = ctx.request; const resolvedPrefix = typeof prefix === 'function' ? prefix.call(ctx, ctx) : prefix; - const key = resolvedPrefix + md5(ignoreQuery ? path : url); + const key = resolvedPrefix + common.md5(ignoreQuery ? path : url); const tkey = key + ':type'; - let match = false; - let routeExpire = false; - for (let i = 0; i < routes.length; i++) { - let 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; - } - } + let { match, routeExpire } = common.validityCheck(routes, exclude, path); if (!redisAvailable || !match || (passParam && ctx.request.query[passParam])) { return await next(); @@ -155,32 +132,3 @@ module.exports = function(options = {}) { } } }; - -function paired(route, path) { - const options = { - sensitive: true, - strict: true, - }; - - return pathToRegExp(route, [], options).exec(path); -} - -// eslint-disable-next-line no-unused-vars -function read(stream) { - return new Promise((resolve, reject) => { - readall(stream, (err, data) => { - if (err) { - reject(err); - } else { - resolve(data); - } - }); - }); -} - -function md5(str) { - return crypto - .createHash('md5') - .update(str) - .digest('hex'); -}