mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-11 07:40:26 +08:00
fix(core): throw URL cache key (#10497)
This commit is contained in:
3
lib/middleware/cache/index.js
vendored
3
lib/middleware/cache/index.js
vendored
@@ -56,6 +56,9 @@ module.exports = function (app) {
|
|||||||
app.context.cache = {
|
app.context.cache = {
|
||||||
...cacheModule,
|
...cacheModule,
|
||||||
tryGet: async (key, getValueFunc, maxAge = config.cache.contentExpire, refresh = true) => {
|
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);
|
let v = await get(key, refresh);
|
||||||
if (!v) {
|
if (!v) {
|
||||||
v = await getValueFunc();
|
v = await getValueFunc();
|
||||||
|
|||||||
@@ -75,6 +75,21 @@ module.exports = async (ctx) => {
|
|||||||
link: `https://github.com/DIYgod/RSSHub/issues/0`,
|
link: `https://github.com/DIYgod/RSSHub/issues/0`,
|
||||||
author: `DIYgod0`,
|
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') {
|
} else if (ctx.params.id === 'complicated') {
|
||||||
item.push({
|
item.push({
|
||||||
title: `Complicated Title`,
|
title: `Complicated Title`,
|
||||||
|
|||||||
@@ -190,4 +190,17 @@ describe('cache', () => {
|
|||||||
expect(parsed1.items[0].content).toBe('Cache1');
|
expect(parsed1.items[0].content).toBe('Cache1');
|
||||||
expect(parsed2.items[0].content).toBe('Cache2');
|
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');
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user