test: cache object

This commit is contained in:
DIYgod
2019-06-13 16:01:12 +08:00
parent 95c1290e2d
commit 58be5caf98
2 changed files with 17 additions and 14 deletions

View File

@@ -188,12 +188,9 @@ module.exports = function(app, options = {}) {
ctx.state.data.lastBuildDate = new Date().toUTCString(); ctx.state.data.lastBuildDate = new Date().toUTCString();
const body = JSON.stringify(ctx.state.data); const body = JSON.stringify(ctx.state.data);
if (ctx.request.method !== 'GET' || !body) { // if (Buffer.byteLength(body) > maxLength) {
return; // return;
} // }
if (Buffer.byteLength(body) > maxLength) {
return;
}
await globalCache.set(key, body, expire); await globalCache.set(key, body, expire);
const type = ctx.response.headers['content-type']; const type = ctx.response.headers['content-type'];
@@ -210,7 +207,7 @@ module.exports = function(app, options = {}) {
const validityCheck = (routes, exclude, path) => { const validityCheck = (routes, exclude, path) => {
let match = false; let match = false;
let routeExpire = false; const routeExpire = false;
const paired = (route, path) => { const paired = (route, path) => {
const options = { const options = {
@@ -221,11 +218,11 @@ module.exports = function(app, options = {}) {
}; };
for (let i = 0; i < routes.length; i++) { for (let i = 0; i < routes.length; i++) {
let route = routes[i]; const route = routes[i];
if (typeof routes[i] === 'object') { // if (typeof routes[i] === 'object') {
route = routes[i].path; // route = routes[i].path;
routeExpire = routes[i].expire; // routeExpire = routes[i].expire;
} // }
if (paired(route, path)) { if (paired(route, path)) {
match = true; match = true;
break; break;

View File

@@ -15,10 +15,16 @@ module.exports = async (ctx) => {
author: `DIYgod0`, author: `DIYgod0`,
}); });
} else if (ctx.params.id === 'cache') { } else if (ctx.params.id === 'cache') {
const description = await ctx.cache.tryGet('test', () => `Cache${++cacheIndex}`, config.cache.routeExpire * 2); const description = await ctx.cache.tryGet(
'test',
() => ({
text: `Cache${++cacheIndex}`,
}),
config.cache.routeExpire * 2
);
item.push({ item.push({
title: 'Cache Title', title: 'Cache Title',
description: description, description: description.text,
pubDate: new Date(`2018-3-1`).toUTCString(), pubDate: new Date(`2018-3-1`).toUTCString(),
link: `https://github.com/DIYgod/RSSHub/issues/0`, link: `https://github.com/DIYgod/RSSHub/issues/0`,
author: `DIYgod0`, author: `DIYgod0`,