From 3c8ccd948e14a4cfc6326eccbcc59d033797bad6 Mon Sep 17 00:00:00 2001 From: Tony Date: Wed, 6 Dec 2023 16:58:39 +0000 Subject: [PATCH] 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 --- lib/middleware/cache/index.js | 7 ++++--- lib/middleware/cache/redis.js | 6 +++--- package.json | 3 ++- pnpm-lock.yaml | 7 +++++++ test/middleware/cache.js | 15 ++++++++++++++- 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/middleware/cache/index.js b/lib/middleware/cache/index.js index 52c0b3aea..4c715b85a 100644 --- a/lib/middleware/cache/index.js +++ b/lib/middleware/cache/index.js @@ -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(); diff --git a/lib/middleware/cache/redis.js b/lib/middleware/cache/redis.js index 1e9889d0f..2234dd866 100644 --- a/lib/middleware/cache/redis.js +++ b/lib/middleware/cache/redis.js @@ -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 = { diff --git a/package.json b/package.json index 1b3829bf3..5a2213169 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 51a3d6f15..ad40cc193 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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'} diff --git a/test/middleware/cache.js b/test/middleware/cache.js index 88a138ba6..ddd572120 100644 --- a/test/middleware/cache.js +++ b/test/middleware/cache.js @@ -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(); + } + }); });