精简缓存逻辑,精简文档 (#915)
https://github.com/HenryQW/RSSHub/issues/6 https://github.com/HenryQW/RSSHub/issues/7
This commit is contained in:
parent
7fc5425297
commit
465324b67d
|
|
@ -1494,15 +1494,13 @@ IATA 国际航空运输协会机场代码, 参见[维基百科 国际航空运
|
||||||
|
|
||||||
### 中国美术馆
|
### 中国美术馆
|
||||||
|
|
||||||
<route name="通知公告" author="HenryQW" example="/namoc/announcement" path="/namoc/announcement"/>
|
<route name="美术馆新闻" author="HenryQW" example="/namoc/announcement" path="/namoc/:type" :paramsDesc="['新闻类型, 可选如下']">
|
||||||
|
|
||||||
<route name="新闻" author="HenryQW" example="/namoc/news" path="/namoc/news"/>
|
| 通知公告 | 新闻 | 媒体联报 | 展览预告 | 焦点专题 |
|
||||||
|
| ------------ | ---- | -------- | ---------- | -------- |
|
||||||
|
| announcement | news | media | exhibition | specials |
|
||||||
|
|
||||||
<route name="媒体联报" author="HenryQW" example="/namoc/media" path="/namoc/media"/>
|
</route>
|
||||||
|
|
||||||
<route name="展览预告" author="HenryQW" example="/namoc/exhibition" path="/namoc/exhibition"/>
|
|
||||||
|
|
||||||
<route name="焦点专题" author="HenryQW" example="/namoc/specials" path="/namoc/specials"/>
|
|
||||||
|
|
||||||
### 国家地理
|
### 国家地理
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
};
|
||||||
|
|
@ -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 lru = require('lru-cache');
|
||||||
|
const common = require('./cache-common');
|
||||||
|
|
||||||
module.exports = function(options = {}) {
|
module.exports = function(options = {}) {
|
||||||
const {
|
const {
|
||||||
|
|
@ -29,31 +27,10 @@ module.exports = function(options = {}) {
|
||||||
return async function cache(ctx, next) {
|
return async function cache(ctx, next) {
|
||||||
const { url, path } = ctx.request;
|
const { url, path } = ctx.request;
|
||||||
const resolvedPrefix = typeof prefix === 'function' ? prefix.call(ctx, ctx) : prefix;
|
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';
|
const tkey = key + ':type';
|
||||||
let match = false;
|
|
||||||
let routeExpire = false;
|
|
||||||
|
|
||||||
for (let i = 0; i < routes.length; i++) {
|
let { match, routeExpire } = common.validityCheck(routes, exclude, path);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!match || (passParam && ctx.request.query[passParam])) {
|
if (!match || (passParam && ctx.request.query[passParam])) {
|
||||||
return await next();
|
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');
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -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 wrapper = require('co-redis');
|
||||||
const readall = require('readall');
|
|
||||||
const crypto = require('crypto');
|
|
||||||
const Redis = require('redis');
|
const Redis = require('redis');
|
||||||
|
const common = require('./cache-common');
|
||||||
|
|
||||||
module.exports = function(options = {}) {
|
module.exports = function(options = {}) {
|
||||||
let redisAvailable = false;
|
let redisAvailable = false;
|
||||||
|
|
@ -50,31 +48,10 @@ module.exports = function(options = {}) {
|
||||||
return async function cache(ctx, next) {
|
return async function cache(ctx, next) {
|
||||||
const { url, path } = ctx.request;
|
const { url, path } = ctx.request;
|
||||||
const resolvedPrefix = typeof prefix === 'function' ? prefix.call(ctx, ctx) : prefix;
|
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';
|
const tkey = key + ':type';
|
||||||
let match = false;
|
|
||||||
let routeExpire = false;
|
|
||||||
|
|
||||||
for (let i = 0; i < routes.length; i++) {
|
let { match, routeExpire } = common.validityCheck(routes, exclude, path);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!redisAvailable || !match || (passParam && ctx.request.query[passParam])) {
|
if (!redisAvailable || !match || (passParam && ctx.request.query[passParam])) {
|
||||||
return await next();
|
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');
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue