Skip to content
Draft
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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,26 @@ const redisCacheHandler = createRedisHandler({

**Note:** Redis Cluster support is currently experimental and may have limitations or unexpected bugs. Use it with caution.

### Using ioredis

If you prefer using `ioredis` instead of `@redis/client`, you can use the `ioredisAdapter` helper.

`npm i ioredis`

```js
import Redis from "ioredis";
import createRedisHandler from "@fortedigital/nextjs-cache-handler/redis-strings";
import { ioredisAdapter } from "@fortedigital/nextjs-cache-handler/helpers/ioredisAdapter";

const client = new Redis(process.env.REDIS_URL);
const redisClient = ioredisAdapter(client);

const redisHandler = createRedisHandler({
client: redisClient,
keyPrefix: "my-app:",
});
```

---

### `local-lru`
Expand Down
11 changes: 11 additions & 0 deletions packages/nextjs-cache-handler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
"./helpers/withAbortSignalProxy": {
"require": "./dist/helpers/withAbortSignalProxy.cjs",
"import": "./dist/helpers/withAbortSignalProxy.js"
},
"./helpers/ioredisAdapter": {
"require": "./dist/helpers/ioredisAdapter.cjs",
"import": "./dist/helpers/ioredisAdapter.js"
}
},
"typesVersions": {
Expand Down Expand Up @@ -106,6 +110,7 @@
"eslint": "^8.57.1",
"eslint-config-prettier": "^9.1.2",
"globals": "^16.5.0",
"ioredis": "^5.8.2",
"jest": "^30.2.0",
"prettier": "^3.7.4",
"prettier-plugin-packagejson": "2.5.20",
Expand All @@ -118,8 +123,14 @@
},
"peerDependencies": {
"@redis/client": ">= 5.5.6",
"ioredis": ">= 5.0.0",
"next": ">=15.2.4"
},
"peerDependenciesMeta": {
"ioredis": {
"optional": true
}
},
"distTags": [
"next15"
],
Expand Down
137 changes: 137 additions & 0 deletions packages/nextjs-cache-handler/src/helpers/ioredisAdapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import type { RedisClientType } from "@redis/client";
import type { Redis } from "ioredis";

/**
* Adapter to make an ioredis client compatible with the interface expected by createRedisHandler.
*
* @param client - The ioredis client instance.
* @returns A proxy that implements the subset of RedisClientType used by this library.
*/
export function ioredisAdapter(client: Redis): RedisClientType {
return new Proxy(client, {
get(target, prop, receiver) {
if (prop === "isReady") {
return target.status === "ready";
}

if (prop === "hScan") {
return async (
key: string,
cursor: string,
options?: { COUNT?: number },
) => {
let result: [string, string[]];

if (options?.COUNT) {
result = await target.hscan(key, cursor, "COUNT", options.COUNT);
} else {
result = await target.hscan(key, cursor);
}

const [newCursor, items] = result;

const entries = [];
for (let i = 0; i < items.length; i += 2) {
entries.push({ field: items[i], value: items[i + 1] });
}

return {
cursor: newCursor,
entries,
};
};
}

if (prop === "hDel") {
return async (key: string, fields: string | string[]) => {
const args = Array.isArray(fields) ? fields : [fields];
if (args.length === 0) {
return 0;
}
return target.hdel(key, ...args);
};
}

if (prop === "unlink") {
return async (keys: string | string[]) => {
const args = Array.isArray(keys) ? keys : [keys];
if (args.length === 0) {
return 0;
}
return target.unlink(...args);
};
}

if (prop === "set") {
return async (key: string, value: string, options?: any) => {
const args: (string | number)[] = [key, value];
if (options) {
if (options.EXAT) {
args.push("EXAT", options.EXAT);
} else if (options.PXAT) {
args.push("PXAT", options.PXAT);
} else if (options.EX) {
args.push("EX", options.EX);
} else if (options.PX) {
args.push("PX", options.PX);
} else if (options.KEEPTTL) {
args.push("KEEPTTL");
}
// Add other options if necessary
}
// Cast to a generic signature to avoid overload mismatch issues with dynamic args
const setFn = target.set as unknown as (
key: string,
value: string | number,
...args: (string | number)[]
) => Promise<string | null>;

return setFn(
args[0] as string,
args[1] as string | number,
...args.slice(2),
);
};
}

if (prop === "hmGet") {
return async (key: string, fields: string | string[]) => {
const args = Array.isArray(fields) ? fields : [fields];
if (args.length === 0) {
return [];
}
return target.hmget(key, ...args);
};
}

// Handle camelCase to lowercase mapping for other methods
if (typeof prop === "string") {
// Special case for expireAt -> expireat
if (prop === "expireAt") {
return target.expireat.bind(target);
}

// hSet -> hset
if (prop === "hSet") {
return target.hset.bind(target);
}

// hExists -> hexists
if (prop === "hExists") {
return target.hexists.bind(target);
}

// Default fallback to lowercase if exists
const lowerProp = prop.toLowerCase();
if (
lowerProp in target &&
typeof (target as any)[lowerProp] === "function"
) {
return (target as any)[lowerProp].bind(target);
}
}

return Reflect.get(target, prop, receiver);
},
}) as unknown as RedisClientType;
}
1 change: 1 addition & 0 deletions packages/nextjs-cache-handler/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const tsup = defineConfig({
"src/helpers/redisClusterAdapter.ts",
"src/helpers/withAbortSignal.ts",
"src/helpers/withAbortSignalProxy.ts",
"src/helpers/ioredisAdapter.ts",
],
splitting: false,
outDir: "dist",
Expand Down
Loading