Skip to content

Commit 7171364

Browse files
committed
use BLAKE3 for redis cache key hash
1 parent 1f2ca14 commit 7171364

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

middleware/cache.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const Redis = require('ioredis');
2-
const crypto = require('crypto');
2+
const blake3 = require('blake3');
33
const { logger } = require('./logger');
44

55
let redis;
@@ -26,12 +26,12 @@ redis.on('reconnecting', () => {
2626
});
2727

2828
/**
29-
* Hash func for redis keys
29+
* BLAKE3 hash func for redis keys
3030
*
3131
* @param {String} str String to hash
3232
* @returns {String} Hashed result
3333
*/
34-
const hash = (str) => crypto.createHash('sha1').update(str).digest('hex');
34+
const hash = (str) => blake3.createHash().update(str).digest('hex');
3535

3636
/**
3737
* Redis cache middleware
@@ -42,7 +42,7 @@ const hash = (str) => crypto.createHash('sha1').update(str).digest('hex');
4242
*/
4343
module.exports.middleware = async (ctx, next) => {
4444
const { url, method } = ctx.request;
45-
const key = `spacex-cache:${hash(method)}:${hash(url)}:${hash(JSON.stringify(ctx.request.body))}`;
45+
const key = `spacex-cache:${hash(`${method}${url}${JSON.stringify(ctx.request.body)}`)}`;
4646

4747
if (!redisAvailable) {
4848
ctx.response.set('spacex-api-cache-online', 'false');

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
},
2929
"homepage": "https://github.com/r-spacex/SpaceX-API",
3030
"dependencies": {
31+
"blake3": "^2.1.4",
3132
"casbin": "^5.0.4",
3233
"cheerio": "^1.0.0-rc.3",
3334
"cron": "^1.8.2",

0 commit comments

Comments
 (0)