Skip to content

Commit

Permalink
Merge pull request #2 from LOKE/feature/support_multiple_clients
Browse files Browse the repository at this point in the history
[ORD-1873] Enable use for Ioredis, redis@3, & redi@2
  • Loading branch information
althoff0 authored Aug 15, 2024
2 parents ae6c888 + 54949fa commit eb6f4a7
Show file tree
Hide file tree
Showing 5 changed files with 3,053 additions and 1,669 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on: push
jobs:
publish:
runs-on: ubuntu-latest

services:
redis:
image: redis
Expand All @@ -15,14 +15,14 @@ jobs:
--health-retries 5
ports:
- 6379:6379

steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 12
node-version: 18
- run: npm ci
- run: npm test
- uses: JS-DevTools/npm-publish@v1
with:
token: ${{ secrets.NPM_TOKEN }}
token: ${{ secrets.NPM_TOKEN }}
29 changes: 25 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { promisify } from "util";
import { Counter, Histogram, exponentialBuckets, Registry } from "prom-client";
import type { RedisClient } from "redis";

const queryCount = new Counter({
name: "cache_queries_total",
Expand Down Expand Up @@ -72,6 +71,25 @@ export interface Reviver {
(this: any, key: string, value: any): any;
}

export interface RedisClient {
get(
key: string,
cb?: (
err: Error | null | undefined,
reply: string | null | undefined
) => void
): void;
psetex(
key: string,
milliseconds: number,
value: string,
cb?: (err: Error | null | undefined) => void
): void;
del(
...args: [...key: string[], cb: (err: Error | null | undefined) => void]
): void;
}

export interface RedisCacheOptions {
/**
* a redis client, see https://www.npmjs.com/package/redis
Expand Down Expand Up @@ -102,9 +120,9 @@ export class RedisCache implements Cache {
private singleFlightGetCache: Map<string, Promise<string>>;
private singleFlightApplyCache: Map<string, Promise<string>>;

private _get: (key: string) => Promise<string | null>;
private _psetex: (key: string, ttl: number, value: string) => Promise<string>;
private _del: (key: string) => Promise<number>;
private _get: (key: string) => Promise<string | null | undefined>;
private _psetex: (key: string, ttl: number, value: string) => Promise<void>;
private _del: (key: string) => Promise<void>;

constructor(options: RedisCacheOptions) {
const { prefix, redisClient } = options;
Expand All @@ -114,6 +132,9 @@ export class RedisCache implements Cache {
this.singleFlightGetCache = new Map();
this.singleFlightApplyCache = new Map();

// Redis@3 & Redis@2 need to be promisified b/c they can't return promises (Redis@4 can)
// Ioredis already returns promises, so it returns the following warning when used
// (node:21449) [DEP0174] DeprecationWarning: Calling promisify on a function that returns a Promise is likely a mistake.
this._get = promisify(redisClient.get).bind(redisClient);
this._psetex = promisify(redisClient.psetex).bind(redisClient);
this._del = promisify(redisClient.del).bind(redisClient);
Expand Down
Loading

0 comments on commit eb6f4a7

Please sign in to comment.