refactor: remove Content-Type cache

This commit is contained in:
DIYgod
2019-11-07 19:49:02 +08:00
parent ccffe38014
commit af51ca0337
5 changed files with 13 additions and 76 deletions
+11 -67
View File
@@ -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;
};
};
+1 -3
View File
@@ -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: [
+1
View File
@@ -66,6 +66,7 @@ module.exports = async (ctx) => {
ctx.set({
'Content-Type': 'text/html; charset=UTF-8',
'Cache-Control': 'no-cache',
});
ctx.body = `<script>window.location = '/weibo/timeline/${uid}'</script>`;
}
-1
View File
@@ -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",
-5
View File
@@ -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"