Skip to content

Commit 06ba16e

Browse files
authored
feat: add redis driver identification (#201)
1 parent 87a42f0 commit 06ba16e

5 files changed

Lines changed: 91 additions & 1 deletion

File tree

examples/redis-minimal/cache-handler.mjs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import createLruHandler from "@fortedigital/nextjs-cache-handler/local-lru";
66
import createRedisHandler from "@fortedigital/nextjs-cache-handler/redis-strings";
77
import createCompositeHandler from "@fortedigital/nextjs-cache-handler/composite";
88
import { ioredisAdapter } from "@fortedigital/nextjs-cache-handler/helpers/ioredisAdapter";
9+
import { getClientInfoTag } from "@fortedigital/nextjs-cache-handler/helpers/getClientInfoTag";
910

1011
const isSingleConnectionModeEnabled = !!process.env.REDIS_SINGLE_CONNECTION;
1112
const redisType = process.env.REDIS_TYPE || "redis"; // "redis" or "ioredis"
@@ -17,7 +18,11 @@ async function setupRedisClient() {
1718
try {
1819
if (redisType === "ioredis") {
1920
console.info(`Using ioredis client...`);
20-
const ioredisClient = new Redis(process.env.REDIS_URL);
21+
const ioredisClient = new Redis(process.env.REDIS_URL, {
22+
// Set clientInfoTag for Redis driver identification
23+
// This helps identify the framework in CLIENT LIST output
24+
clientInfoTag: getClientInfoTag(),
25+
});
2126

2227
// Wait for connection to be ready
2328
console.info("Connecting ioredis client...");
@@ -35,6 +40,9 @@ async function setupRedisClient() {
3540
redisClient = createClient({
3641
url: process.env.REDIS_URL,
3742
pingInterval: 10000,
43+
// Set clientInfoTag for Redis driver identification
44+
// This helps identify the framework in CLIENT LIST output
45+
clientInfoTag: getClientInfoTag(),
3846
});
3947

4048
console.info("Connecting Redis client...");

packages/nextjs-cache-handler/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
"./helpers/ioredisAdapter": {
5555
"require": "./dist/helpers/ioredisAdapter.cjs",
5656
"import": "./dist/helpers/ioredisAdapter.js"
57+
},
58+
"./helpers/getClientInfoTag": {
59+
"require": "./dist/helpers/getClientInfoTag.cjs",
60+
"import": "./dist/helpers/getClientInfoTag.js"
5761
}
5862
},
5963
"typesVersions": {
@@ -87,6 +91,9 @@
8791
],
8892
"helpers/ioredisAdapter": [
8993
"dist/helpers/ioredisAdapter.d.ts"
94+
],
95+
"helpers/getClientInfoTag": [
96+
"dist/helpers/getClientInfoTag.d.ts"
9097
]
9198
}
9299
},
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { getClientInfoTag } from "./getClientInfoTag";
2+
3+
describe("getClientInfoTag", () => {
4+
it("should return a string starting with 'nextjs-cache-handler'", () => {
5+
const result = getClientInfoTag();
6+
7+
expect(result).toMatch(/^nextjs-cache-handler/);
8+
});
9+
10+
it("should return a string with version in format 'nextjs-cache-handler_vX.X.X'", () => {
11+
const result = getClientInfoTag();
12+
13+
// Should match pattern like "nextjs-cache-handler_v2.5.1"
14+
expect(result).toMatch(/^nextjs-cache-handler_v\d+\.\d+\.\d+$/);
15+
});
16+
17+
it("should include the correct package version", () => {
18+
const result = getClientInfoTag();
19+
20+
// The version should be 2.5.1 based on package.json
21+
expect(result).toBe("nextjs-cache-handler_v2.5.1");
22+
});
23+
24+
it("should return consistent results on multiple calls", () => {
25+
const result1 = getClientInfoTag();
26+
const result2 = getClientInfoTag();
27+
28+
expect(result1).toBe(result2);
29+
});
30+
});
31+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { readFileSync } from "node:fs";
2+
import { resolve } from "node:path";
3+
4+
function getPackageVersion(): string | undefined {
5+
try {
6+
const packageJsonPath = resolve(__dirname, '..', '..', 'package.json')
7+
const content = readFileSync(packageJsonPath, 'utf-8')
8+
const packageJson = JSON.parse(content) as { version: string }
9+
return packageJson.version
10+
} catch {
11+
return undefined
12+
}
13+
}
14+
15+
/**
16+
* Returns a client info tag string for Redis client identification.
17+
*
18+
* This function reads the version from `@fortedigital/nextjs-cache-handler/package.json`
19+
* and returns a formatted string like `nextjs-cache-handler_v2.5.1`.
20+
*
21+
* If the version cannot be read, it falls back to `nextjs-cache-handler`.
22+
*
23+
* @example
24+
* ```js
25+
* import { createClient } from 'redis';
26+
* import { getClientInfoTag } from '@fortedigital/nextjs-cache-handler/helpers/getClientInfoTag';
27+
*
28+
* const client = createClient({
29+
* url: 'redis://localhost:6379',
30+
* clientInfoTag: getClientInfoTag(),
31+
* });
32+
* ```
33+
*
34+
* @returns The client info tag string for Redis identification.
35+
*/
36+
export function getClientInfoTag(): string {
37+
const version = getPackageVersion();
38+
if (version) {
39+
return `nextjs-cache-handler_v${version}`;
40+
}
41+
return "nextjs-cache-handler";
42+
}
43+

packages/nextjs-cache-handler/tsup.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const tsup = defineConfig({
1010
"src/helpers/withAbortSignal.ts",
1111
"src/helpers/withAbortSignalProxy.ts",
1212
"src/helpers/ioredisAdapter.ts",
13+
"src/helpers/getClientInfoTag.ts",
1314
],
1415
splitting: false,
1516
outDir: "dist",

0 commit comments

Comments
 (0)