Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 64 additions & 63 deletions packages/das/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/das/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"lint:fix": "eslint \"src/**/*.ts\" --fix"
},
"dependencies": {
"@keyv/redis": "^5.1.6",
"@nestjs/bullmq": "^11.0.4",
"@nestjs/cache-manager": "^3.1.0",
"@nestjs/common": "^10.0.0",
Expand All @@ -26,8 +27,8 @@
"@nestjs/swagger": "^7.4.2",
"@nestjs/typeorm": "^10.0.2",
"bullmq": "^5.74.1",
"cache-manager-redis-yet": "^5.1.5",
"jsonwebtoken": "^9.0.3",
"keyv": "^5.6.0",
"pg": "^8.16.3",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1",
Expand Down
45 changes: 31 additions & 14 deletions packages/das/src/cache/cache.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { CacheModule } from "@nestjs/cache-manager";
import { Global, Module } from "@nestjs/common";
import { CacheModule, CacheModuleOptions } from "@nestjs/cache-manager";
import { Global, Logger, Module } from "@nestjs/common";
import { APP_INTERCEPTOR } from "@nestjs/core";
import { redisStore } from "cache-manager-redis-yet";
import KeyvRedis from "@keyv/redis";
import { Keyv } from "keyv";
import { CustomCacheInterceptor } from "./custom-cache.interceptor";

// Production TTL for cached API responses (scoring cycles run far less often)
Expand All @@ -11,26 +12,42 @@ const DEV_CACHE_TTL_MS = 1;

const isProduction = process.env.NODE_ENV === "production";

const logger = new Logger("CustomCacheModule");

@Global()
@Module({
imports: [
CacheModule.registerAsync({
isGlobal: true,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
useFactory: async (): Promise<any> => {
useFactory: (): CacheModuleOptions => {
const ttl = isProduction ? CACHE_TTL_MS : DEV_CACHE_TTL_MS;

// @nestjs/cache-manager v3 (cache-manager v7 / Keyv) reads the store
// list from `stores` (plural — Keyv instances) and the entry TTL from
// the top-level `ttl`. A singular `store`, or a `ttl` nested inside a
// store adapter, is silently ignored: the cache then falls back to an
// in-memory store with no expiry. Keep both at the top level.
if (process.env.REDIS_HOST) {
return {
store: await redisStore({
socket: {
host: process.env.REDIS_HOST,
port: parseInt(process.env.REDIS_PORT ?? "6379"),
},
ttl,
}),
};
const host = process.env.REDIS_HOST;
const port = process.env.REDIS_PORT ?? "6379";

// `apicache` namespace keeps response-cache keys clear of the
// BullMQ (`bull:*`) keyspace on the shared Redis instance.
const store = new Keyv({
store: new KeyvRedis(`redis://${host}:${port}`),
namespace: "apicache",
});
// Surface — and swallow — Redis connection errors so a Redis blip
// degrades to an uncached request rather than an unhandled
// EventEmitter "error" that would crash the process.
store.on("error", (err) =>
logger.error(`Redis response cache error: ${String(err)}`),
);

return { ttl, stores: [store] };
}

// No Redis configured — fall back to the built-in in-memory store.
return { ttl };
},
}),
Expand Down
Loading