fix(core): throw URL cache key (#10497)

This commit is contained in:
Tony
2022-08-16 01:38:05 -12:00
committed by GitHub
parent aef0b0a9b0
commit 4877301f23
3 changed files with 31 additions and 0 deletions

View File

@@ -56,6 +56,9 @@ module.exports = function (app) {
app.context.cache = {
...cacheModule,
tryGet: async (key, getValueFunc, maxAge = config.cache.contentExpire, refresh = true) => {
if (typeof key !== 'string') {
throw Error('Cache key must be a string');
}
let v = await get(key, refresh);
if (!v) {
v = await getValueFunc();

View File

@@ -75,6 +75,21 @@ module.exports = async (ctx) => {
link: `https://github.com/DIYgod/RSSHub/issues/0`,
author: `DIYgod0`,
});
} else if (ctx.params.id === 'cacheUrlKey') {
const description = await ctx.cache.tryGet(
new URL('https://rsshub.app'),
() => ({
text: `Cache${++cacheIndex}`,
}),
config.cache.routeExpire * 2
);
item.push({
title: 'Cache Title',
description: description.text,
pubDate: new Date(`2019-3-1`).toUTCString(),
link: `https://github.com/DIYgod/RSSHub/issues/0`,
author: `DIYgod0`,
});
} else if (ctx.params.id === 'complicated') {
item.push({
title: `Complicated Title`,

View File

@@ -190,4 +190,17 @@ describe('cache', () => {
expect(parsed1.items[0].content).toBe('Cache1');
expect(parsed2.items[0].content).toBe('Cache2');
});
it('throws URL key', async () => {
process.env.CACHE_TYPE = 'memory';
server = require('../../lib/index');
const request = supertest(server);
try {
const response = await request.get('/test/cacheUrlKey');
expect(response).toThrow(Error);
} catch (e) {
expect(e.message).toContain('Cache key must be a string');
}
});
});