|
1
|
import { Logger } from "@medusajs/framework/types" |
|
2
|
import { RedisCachingProvider } from "../redis-cache" |
|
3
|
|
|
4
|
type RedisHashValue = string | Buffer |
|
5
|
|
|
6
|
class FakePipeline { |
|
7
|
private readonly commands: Array<() => unknown> = [] |
|
8
|
|
|
9
|
constructor(private readonly redis: FakeRedisClient) {} |
|
10
|
|
|
11
|
smembers(key: string) { |
|
12
|
this.commands.push(() => this.redis.smembersSync(key)) |
|
13
|
return this |
|
14
|
} |
|
15
|
|
|
16
|
hget(key: string, field: string) { |
|
17
|
this.commands.push(() => this.redis.hgetSync(key, field)) |
|
18
|
return this |
|
19
|
} |
|
20
|
|
|
21
|
srem(key: string, member: string) { |
|
22
|
this.commands.push(() => this.redis.sremSync(key, member)) |
|
23
|
return this |
|
24
|
} |
|
25
|
|
|
26
|
unlink(...keys: string[]) { |
|
27
|
this.commands.push(() => this.redis.unlinkSync(...keys)) |
|
28
|
return this |
|
29
|
} |
|
30
|
|
|
31
|
exec = jest.fn(async () => { |
|
32
|
return this.commands.map((command) => [null, command()] as [null, unknown]) |
|
33
|
}) |
|
34
|
} |
|
35
|
|
|
36
|
class FakeRedisClient { |
|
37
|
public status = "ready" |
|
38
|
public readonly hashes = new Map<string, Map<string, RedisHashValue>>() |
|
39
|
public readonly strings = new Map<string, RedisHashValue>() |
|
40
|
public readonly sets = new Map<string, Set<string>>() |
|
41
|
|
|
42
|
public readonly keys = jest.fn(() => { |
|
43
|
throw new Error("KEYS should not be called") |
|
44
|
}) |
|
45
|
|
|
46
|
public readonly pipeline = jest.fn(() => new FakePipeline(this)) |
|
47
|
|
|
48
|
public readonly smembers = jest.fn(async (key: string) => { |
|
49
|
return this.smembersSync(key) |
|
50
|
}) |
|
51
|
|
|
52
|
public readonly unlink = jest.fn(async (...keys: string[]) => { |
|
53
|
return this.unlinkSync(...keys) |
|
54
|
}) |
|
55
|
|
|
56
|
setHash(key: string, fields: Record<string, RedisHashValue>) { |
|
57
|
this.hashes.set(key, new Map(Object.entries(fields))) |
|
58
|
} |
|
59
|
|
|
60
|
setMembers(key: string, members: string[]) { |
|
61
|
this.sets.set(key, new Set(members)) |
|
62
|
} |
|
63
|
|
|
64
|
smembersSync(key: string): string[] { |
|
65
|
return Array.from(this.sets.get(key) ?? []) |
|
66
|
} |
|
67
|
|
|
68
|
hgetSync(key: string, field: string): RedisHashValue | null { |
|
69
|
return this.hashes.get(key)?.get(field) ?? null |
|
70
|
} |
|
71
|
|
|
72
|
sremSync(key: string, member: string): number { |
|
73
|
const set = this.sets.get(key) |
|
74
|
if (!set?.has(member)) { |
|
75
|
return 0 |
|
76
|
} |
|
77
|
|
|
78
|
set.delete(member) |
|
79
|
|
|
80
|
if (!set.size) { |
|
81
|
this.sets.delete(key) |
|
82
|
} |
|
83
|
|
|
84
|
return 1 |
|
85
|
} |
|
86
|
|
|
87
|
unlinkSync(...keys: string[]): number { |
|
88
|
let deleted = 0 |
|
89
|
|
|
90
|
keys.forEach((key) => { |
|
91
|
const removed = |
|
92
|
this.hashes.delete(key) || |
|
93
|
this.strings.delete(key) || |
|
94
|
this.sets.delete(key) |
|
95
|
|
|
96
|
deleted += removed ? 1 : 0 |
|
97
|
}) |
|
98
|
|
|
99
|
return deleted |
|
100
|
} |
|
101
|
} |
|
102
|
|
|
103
|
const loggerMock = { |
|
104
|
warn: jest.fn(), |
|
105
|
info: jest.fn(), |
|
106
|
error: jest.fn(), |
|
107
|
debug: jest.fn(), |
|
108
|
} as unknown as Logger |
|
109
|
|
|
110
|
const createProvider = (redisClient: FakeRedisClient) => { |
|
111
|
return new RedisCachingProvider({ |
|
112
|
redisClient: redisClient as any, |
|
113
|
prefix: "mc:", |
|
114
|
hasher: (key) => `hash:${key}`, |
|
115
|
logger: loggerMock, |
|
116
|
}) |
|
117
|
} |
|
118
|
|
|
119
|
describe("RedisCachingProvider clear", () => { |
|
120
|
beforeEach(() => { |
|
121
|
jest.clearAllMocks() |
|
122
|
}) |
|
123
|
|
|
124
|
it("clears tagged entries without falling back to a global KEYS scan", async () => { |
|
125
|
const redisClient = new FakeRedisClient() |
|
126
|
const provider = createProvider(redisClient) |
|
127
|
|
|
128
|
redisClient.setHash("mc:entry-1", {}) |
|
129
|
redisClient.setMembers("mc:tags:entry-1", ["hash:tag-a"]) |
|
130
|
redisClient.setMembers("mc:tag:hash:tag-a", ["mc:entry-1"]) |
|
131
|
|
|
132
|
await provider.clear({ tags: ["tag-a"] }) |
|
133
|
|
|
134
|
expect(redisClient.keys).not.toHaveBeenCalled() |
|
135
|
expect(redisClient.hashes.has("mc:entry-1")).toBe(false) |
|
136
|
expect(redisClient.sets.has("mc:tags:entry-1")).toBe(false) |
|
137
|
expect(redisClient.sets.has("mc:tag:hash:tag-a")).toBe(false) |
|
138
|
}) |
|
139
|
|
|
140
|
it("only deletes auto-invalidated entries without using KEYS", async () => { |
|
141
|
const redisClient = new FakeRedisClient() |
|
142
|
const provider = createProvider(redisClient) |
|
143
|
|
|
144
|
redisClient.setHash("mc:entry-1", {}) |
|
145
|
redisClient.setHash("mc:entry-2", { |
|
146
|
options: JSON.stringify({ autoInvalidate: false }), |
|
147
|
}) |
|
148
|
redisClient.setMembers("mc:tags:entry-1", ["hash:tag-a"]) |
|
149
|
redisClient.setMembers("mc:tags:entry-2", ["hash:tag-a"]) |
|
150
|
redisClient.setMembers("mc:tag:hash:tag-a", ["mc:entry-1", "mc:entry-2"]) |
|
151
|
|
|
152
|
await provider.clear({ |
|
153
|
tags: ["tag-a"], |
|
154
|
options: { autoInvalidate: true }, |
|
155
|
}) |
|
156
|
|
|
157
|
expect(redisClient.keys).not.toHaveBeenCalled() |
|
158
|
expect(redisClient.hashes.has("mc:entry-1")).toBe(false) |
|
159
|
expect(redisClient.sets.has("mc:tags:entry-1")).toBe(false) |
|
160
|
expect(redisClient.hashes.has("mc:entry-2")).toBe(true) |
|
161
|
expect(redisClient.sets.get("mc:tags:entry-2")).toEqual( |
|
162
|
new Set(["hash:tag-a"]) |
|
163
|
) |
|
164
|
expect(redisClient.sets.get("mc:tag:hash:tag-a")).toEqual( |
|
165
|
new Set(["mc:entry-2"]) |
|
166
|
) |
|
167
|
}) |
|
168
|
|
|
169
|
it("clears by key using the reverse tag index", async () => { |
|
170
|
const redisClient = new FakeRedisClient() |
|
171
|
const provider = createProvider(redisClient) |
|
172
|
|
|
173
|
redisClient.setHash("mc:entry-1", {}) |
|
174
|
redisClient.setMembers("mc:tags:entry-1", ["hash:tag-a", "hash:tag-b"]) |
|
175
|
redisClient.setMembers("mc:tag:hash:tag-a", ["mc:entry-1", "mc:entry-2"]) |
|
176
|
redisClient.setMembers("mc:tag:hash:tag-b", ["mc:entry-1"]) |
|
177
|
|
|
178
|
await provider.clear({ key: "entry-1" }) |
|
179
|
|
|
180
|
expect(redisClient.hashes.has("mc:entry-1")).toBe(false) |
|
181
|
expect(redisClient.sets.has("mc:tags:entry-1")).toBe(false) |
|
182
|
expect(redisClient.sets.get("mc:tag:hash:tag-a")).toEqual( |
|
183
|
new Set(["mc:entry-2"]) |
|
184
|
) |
|
185
|
expect(redisClient.sets.has("mc:tag:hash:tag-b")).toBe(false) |
|
186
|
}) |
|
187
|
|
|
188
|
it("removes tag indexes before deleting entries", async () => { |
|
189
|
const redisClient = new FakeRedisClient() |
|
190
|
const provider = createProvider(redisClient) |
|
191
|
|
|
192
|
redisClient.setHash("mc:entry-1", {}) |
|
193
|
redisClient.setMembers("mc:tags:entry-1", ["hash:tag-a"]) |
|
194
|
redisClient.setMembers("mc:tag:hash:tag-a", ["mc:entry-1"]) |
|
195
|
|
|
196
|
const events: string[] = [] |
|
197
|
const originalSrem = redisClient.sremSync.bind(redisClient) |
|
198
|
const originalUnlink = redisClient.unlinkSync.bind(redisClient) |
|
199
|
|
|
200
|
redisClient.sremSync = ((key: string, member: string) => { |
|
201
|
events.push(`srem:${member}`) |
|
202
|
return originalSrem(key, member) |
|
203
|
}) as typeof redisClient.sremSync |
|
204
|
|
|
205
|
redisClient.unlinkSync = ((...keys: string[]) => { |
|
206
|
events.push(`unlink:${keys.join(",")}`) |
|
207
|
return originalUnlink(...keys) |
|
208
|
}) as typeof redisClient.unlinkSync |
|
209
|
|
|
210
|
await provider.clear({ key: "entry-1" }) |
|
211
|
|
|
212
|
const sremIdx = events.findIndex((event) => event === "srem:mc:entry-1") |
|
213
|
const reverseUnlinkIdx = events.findIndex((event) => |
|
214
|
event.includes("mc:tags:entry-1") |
|
215
|
) |
|
216
|
const entryUnlinkIdx = events.findIndex( |
|
217
|
(event) => event === "unlink:mc:entry-1" |
|
218
|
) |
|
219
|
|
|
220
|
expect(sremIdx).toBeGreaterThanOrEqual(0) |
|
221
|
expect(reverseUnlinkIdx).toBeGreaterThan(sremIdx) |
|
222
|
expect(entryUnlinkIdx).toBeGreaterThan(reverseUnlinkIdx) |
|
223
|
}) |
|
224
|
}) |