perf(core): replace md5 cache key with xxhash64 (#13974)

* perf(core): replace md5 cache key with xxhash64

* test: add test case for cache TTL key
This commit is contained in:
Tony 2023-12-06 16:58:39 +00:00 committed by GitHub
parent 360fc48d90
commit 3c8ccd948e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 8 deletions

View File

@ -1,4 +1,4 @@
const md5 = require('@/utils/md5');
const xxhash = require('xxhash-wasm');
const config = require('@/config').value;
const logger = require('@/utils/logger');
const { RequestInProgressError } = require('@/errors');
@ -81,8 +81,9 @@ module.exports = function (app) {
};
return async (ctx, next) => {
const key = 'koa-redis-cache:' + md5(ctx.request.path);
const controlKey = 'path-requested:' + md5(ctx.request.path);
const { h64ToString } = await xxhash();
const key = 'rsshub:koa-redis-cache:' + h64ToString(ctx.request.path);
const controlKey = 'rsshub:path-requested:' + h64ToString(ctx.request.path);
if (!status.available) {
return next();

View File

@ -19,10 +19,10 @@ redisClient.on('connect', () => {
});
const getCacheTtlKey = (key) => {
if (key.startsWith('cacheTtl:')) {
throw Error('"cacheTtl:" prefix is reserved for the internal usage, please change your cache key'); // blocking any attempt to get/set the cacheTtl
if (key.startsWith('rsshub:cacheTtl:')) {
throw Error('"rsshub:cacheTtl:" prefix is reserved for the internal usage, please change your cache key'); // blocking any attempt to get/set the cacheTtl
}
return `cacheTtl:${key}`;
return `rsshub:cacheTtl:${key}`;
};
module.exports = {

View File

@ -146,7 +146,8 @@
"tiny-async-pool": "2.1.0",
"tough-cookie": "4.1.3",
"twitter-api-v2": "1.15.2",
"winston": "3.11.0"
"winston": "3.11.0",
"xxhash-wasm": "1.0.2"
},
"devDependencies": {
"@microsoft/eslint-formatter-sarif": "3.0.0",

View File

@ -197,6 +197,9 @@ dependencies:
winston:
specifier: 3.11.0
version: 3.11.0
xxhash-wasm:
specifier: 1.0.2
version: 1.0.2
devDependencies:
'@microsoft/eslint-formatter-sarif':
@ -8083,6 +8086,10 @@ packages:
resolution: {integrity: sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw==}
dev: false
/xxhash-wasm@1.0.2:
resolution: {integrity: sha512-ibF0Or+FivM9lNrg+HGJfVX8WJqgo+kCLDc4vx6xMeTce7Aj+DLttKbxxRR/gNLSAelRc1omAPlJ77N/Jem07A==}
dev: false
/y18n@5.0.8:
resolution: {integrity: sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==}
engines: {node: '>=10'}

View File

@ -13,7 +13,7 @@ beforeAll(() => {
afterEach(() => {
delete process.env.CACHE_TYPE;
jest.resetModules();
server.close();
server?.close();
});
afterAll(() => {
@ -203,4 +203,17 @@ describe('cache', () => {
expect(e.message).toContain('Cache key must be a string');
}
});
it('throws TTL key', async () => {
process.env.CACHE_TYPE = 'redis';
const app = require('../../lib/app');
try {
await app.context.cache.get('rsshub:cacheTtl:mock');
} catch (e) {
expect(e.message).toContain('reserved for the internal usage');
} finally {
await app.context.cache.clients.redisClient.quit();
}
});
});