From 529eb8f9491ae3c293707cd9a16be56065dd308f Mon Sep 17 00:00:00 2001 From: Ajay Date: Tue, 10 Mar 2026 15:01:32 -0400 Subject: [PATCH] Switch to simple lru cache --- package-lock.json | 26 ++++++++++++-------------- package.json | 2 +- src/utils/redis.ts | 15 ++++++--------- 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6a13dd9..bdb33bf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.1.0", "license": "AGPL-3.0-only", "dependencies": { + "@ajayyy/simple-lru-cache": "^1.1.2", "axios": "^1.13.6", "better-sqlite3": "^11.2.1", "cron": "^2.1.0", @@ -17,7 +18,6 @@ "express-rate-limit": "^6.7.0", "form-data": "^4.0.4", "lodash": "^4.17.23", - "lru-cache": "^10.2.0", "lz4-napi": "^2.2.0", "pg": "^8.8.0", "rate-limit-redis": "^3.0.1", @@ -51,6 +51,12 @@ "node": ">=18" } }, + "node_modules/@ajayyy/simple-lru-cache": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@ajayyy/simple-lru-cache/-/simple-lru-cache-1.1.2.tgz", + "integrity": "sha512-X1SRyVSK9xTyFQoZPf66IxtraRQbLkFWl93TRBtKft3V7NU8/KGuPWlIddilvxYwkrjSHKU6WYNN0mCgHBYYhg==", + "license": "MIT" + }, "node_modules/@ampproject/remapping": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.2.0.tgz", @@ -3859,14 +3865,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/lru-cache": { - "version": "10.2.0", - "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-10.2.0.tgz", - "integrity": "sha512-2bIM8x+VAf6JT4bKAljS1qUWgMsqZRPGJS6FSahIMPVvctcNhyVp7AJu7quxOW9jwkryBReKZY5tY5JYv2n/7Q==", - "engines": { - "node": "14 || >=16.14" - } - }, "node_modules/luxon": { "version": "1.28.1", "resolved": "https://registry.npmjs.org/luxon/-/luxon-1.28.1.tgz", @@ -6137,6 +6135,11 @@ } }, "dependencies": { + "@ajayyy/simple-lru-cache": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@ajayyy/simple-lru-cache/-/simple-lru-cache-1.1.2.tgz", + "integrity": "sha512-X1SRyVSK9xTyFQoZPf66IxtraRQbLkFWl93TRBtKft3V7NU8/KGuPWlIddilvxYwkrjSHKU6WYNN0mCgHBYYhg==" + }, "@ampproject/remapping": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.2.0.tgz", @@ -8878,11 +8881,6 @@ "is-unicode-supported": "^0.1.0" } }, - "lru-cache": { - "version": "10.2.0", - "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-10.2.0.tgz", - "integrity": "sha512-2bIM8x+VAf6JT4bKAljS1qUWgMsqZRPGJS6FSahIMPVvctcNhyVp7AJu7quxOW9jwkryBReKZY5tY5JYv2n/7Q==" - }, "luxon": { "version": "1.28.1", "resolved": "https://registry.npmjs.org/luxon/-/luxon-1.28.1.tgz", diff --git a/package.json b/package.json index 1d4b32e..04c84df 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "express-rate-limit": "^6.7.0", "form-data": "^4.0.4", "lodash": "^4.17.23", - "lru-cache": "^10.2.0", + "@ajayyy/simple-lru-cache": "^1.1.2", "lz4-napi": "^2.2.0", "pg": "^8.8.0", "rate-limit-redis": "^3.0.1", diff --git a/src/utils/redis.ts b/src/utils/redis.ts index 112361c..e1a528e 100644 --- a/src/utils/redis.ts +++ b/src/utils/redis.ts @@ -7,7 +7,7 @@ import { RedisReply } from "rate-limit-redis"; import { db } from "../databases/databases"; import { Postgres } from "../databases/Postgres"; import { compress, uncompress } from "lz4-napi"; -import { LRUCache } from "lru-cache"; +import { LRUCache } from "@ajayyy/simple-lru-cache/dist/index"; import { shouldClientCacheKey } from "./redisKeys"; import { ZMember } from "@redis/client/dist/lib/commands/generic-transformers"; @@ -79,15 +79,12 @@ const activeRequestPromises: Record> = {}; const resetKeys: Set = new Set(); const cache = config.redis.clientCacheSize ? new LRUCache({ maxSize: config.redis.clientCacheSize, - sizeCalculation: (value) => value.length, - ttl: 1000 * 60 * 30, - ttlResolution: 1000 * 60 * 15 + ttl: 1000 * 60 * 30 }) : null; // Used to cache ttl data const ttlCache = config.redis.clientCacheSize ? new LRUCache({ - max: config.redis.clientCacheSize / 10 / 4, // 4 byte integer per element - ttl: 1000 * 60 * 30, - ttlResolution: 1000 * 60 * 15 + maxElements: config.redis.clientCacheSize / 10 / 4, // 4 byte integer per element + ttl: 1000 * 60 * 30 }) : null; // For redis @@ -406,8 +403,8 @@ export function getRedisStats(): RedisStats { avgWriteTime: writeResponseTime.length > 0 ? writeResponseTime.reduce((a, b) => a + b, 0) / writeResponseTime.length : 0, memoryCacheHits: memoryCacheHits / (memoryCacheHits + memoryCacheMisses), memoryCacheTotalHits: memoryCacheHits / (memoryCacheHits + memoryCacheMisses + memoryCacheUncachedMisses), - memoryCacheLength: cache?.size ?? 0, - memoryCacheSize: cache?.calculatedSize ?? 0, + memoryCacheLength: cache?.currentElementCount ?? 0, + memoryCacheSize: cache?.currentSize ?? 0, lastInvalidation, lastInvalidationMessage };