Switch to simple lru cache

This commit is contained in:
Ajay
2026-03-10 15:01:32 -04:00
parent 1e440ff053
commit 529eb8f949
3 changed files with 19 additions and 24 deletions

26
package-lock.json generated
View File

@@ -9,6 +9,7 @@
"version": "0.1.0", "version": "0.1.0",
"license": "AGPL-3.0-only", "license": "AGPL-3.0-only",
"dependencies": { "dependencies": {
"@ajayyy/simple-lru-cache": "^1.1.2",
"axios": "^1.13.6", "axios": "^1.13.6",
"better-sqlite3": "^11.2.1", "better-sqlite3": "^11.2.1",
"cron": "^2.1.0", "cron": "^2.1.0",
@@ -17,7 +18,6 @@
"express-rate-limit": "^6.7.0", "express-rate-limit": "^6.7.0",
"form-data": "^4.0.4", "form-data": "^4.0.4",
"lodash": "^4.17.23", "lodash": "^4.17.23",
"lru-cache": "^10.2.0",
"lz4-napi": "^2.2.0", "lz4-napi": "^2.2.0",
"pg": "^8.8.0", "pg": "^8.8.0",
"rate-limit-redis": "^3.0.1", "rate-limit-redis": "^3.0.1",
@@ -51,6 +51,12 @@
"node": ">=18" "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": { "node_modules/@ampproject/remapping": {
"version": "2.2.0", "version": "2.2.0",
"resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.2.0.tgz", "resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.2.0.tgz",
@@ -3859,14 +3865,6 @@
"url": "https://github.com/sponsors/sindresorhus" "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": { "node_modules/luxon": {
"version": "1.28.1", "version": "1.28.1",
"resolved": "https://registry.npmjs.org/luxon/-/luxon-1.28.1.tgz", "resolved": "https://registry.npmjs.org/luxon/-/luxon-1.28.1.tgz",
@@ -6137,6 +6135,11 @@
} }
}, },
"dependencies": { "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": { "@ampproject/remapping": {
"version": "2.2.0", "version": "2.2.0",
"resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.2.0.tgz", "resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.2.0.tgz",
@@ -8878,11 +8881,6 @@
"is-unicode-supported": "^0.1.0" "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": { "luxon": {
"version": "1.28.1", "version": "1.28.1",
"resolved": "https://registry.npmjs.org/luxon/-/luxon-1.28.1.tgz", "resolved": "https://registry.npmjs.org/luxon/-/luxon-1.28.1.tgz",

View File

@@ -27,7 +27,7 @@
"express-rate-limit": "^6.7.0", "express-rate-limit": "^6.7.0",
"form-data": "^4.0.4", "form-data": "^4.0.4",
"lodash": "^4.17.23", "lodash": "^4.17.23",
"lru-cache": "^10.2.0", "@ajayyy/simple-lru-cache": "^1.1.2",
"lz4-napi": "^2.2.0", "lz4-napi": "^2.2.0",
"pg": "^8.8.0", "pg": "^8.8.0",
"rate-limit-redis": "^3.0.1", "rate-limit-redis": "^3.0.1",

View File

@@ -7,7 +7,7 @@ import { RedisReply } from "rate-limit-redis";
import { db } from "../databases/databases"; import { db } from "../databases/databases";
import { Postgres } from "../databases/Postgres"; import { Postgres } from "../databases/Postgres";
import { compress, uncompress } from "lz4-napi"; 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 { shouldClientCacheKey } from "./redisKeys";
import { ZMember } from "@redis/client/dist/lib/commands/generic-transformers"; import { ZMember } from "@redis/client/dist/lib/commands/generic-transformers";
@@ -79,15 +79,12 @@ const activeRequestPromises: Record<string, Promise<string>> = {};
const resetKeys: Set<RedisCommandArgument> = new Set(); const resetKeys: Set<RedisCommandArgument> = new Set();
const cache = config.redis.clientCacheSize ? new LRUCache<RedisCommandArgument, string>({ const cache = config.redis.clientCacheSize ? new LRUCache<RedisCommandArgument, string>({
maxSize: config.redis.clientCacheSize, maxSize: config.redis.clientCacheSize,
sizeCalculation: (value) => value.length, ttl: 1000 * 60 * 30
ttl: 1000 * 60 * 30,
ttlResolution: 1000 * 60 * 15
}) : null; }) : null;
// Used to cache ttl data // Used to cache ttl data
const ttlCache = config.redis.clientCacheSize ? new LRUCache<RedisCommandArgument, number>({ const ttlCache = config.redis.clientCacheSize ? new LRUCache<RedisCommandArgument, number>({
max: config.redis.clientCacheSize / 10 / 4, // 4 byte integer per element maxElements: config.redis.clientCacheSize / 10 / 4, // 4 byte integer per element
ttl: 1000 * 60 * 30, ttl: 1000 * 60 * 30
ttlResolution: 1000 * 60 * 15
}) : null; }) : null;
// For redis // For redis
@@ -406,8 +403,8 @@ export function getRedisStats(): RedisStats {
avgWriteTime: writeResponseTime.length > 0 ? writeResponseTime.reduce((a, b) => a + b, 0) / writeResponseTime.length : 0, avgWriteTime: writeResponseTime.length > 0 ? writeResponseTime.reduce((a, b) => a + b, 0) / writeResponseTime.length : 0,
memoryCacheHits: memoryCacheHits / (memoryCacheHits + memoryCacheMisses), memoryCacheHits: memoryCacheHits / (memoryCacheHits + memoryCacheMisses),
memoryCacheTotalHits: memoryCacheHits / (memoryCacheHits + memoryCacheMisses + memoryCacheUncachedMisses), memoryCacheTotalHits: memoryCacheHits / (memoryCacheHits + memoryCacheMisses + memoryCacheUncachedMisses),
memoryCacheLength: cache?.size ?? 0, memoryCacheLength: cache?.currentElementCount ?? 0,
memoryCacheSize: cache?.calculatedSize ?? 0, memoryCacheSize: cache?.currentSize ?? 0,
lastInvalidation, lastInvalidation,
lastInvalidationMessage lastInvalidationMessage
}; };