Skip to content

Commit

Permalink
fix(core): throw URL cache key (#10497)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyRL committed Aug 16, 2022
1 parent aef0b0a commit 4877301
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/middleware/cache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
15 changes: 15 additions & 0 deletions lib/v2/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
13 changes: 13 additions & 0 deletions test/middleware/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
});
});

0 comments on commit 4877301

Please sign in to comment.