Skip to content

Commit eed110e

Browse files
committed
fixup
1 parent 53af515 commit eed110e

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

lib/cache/memory-cache-store.js

+31-18
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class MemoryCacheStore {
1818
#maxEntrySize = Infinity
1919

2020
#size = 0
21-
#entries = []
21+
#count = 0
22+
#entries = new Map()
2223

2324
/**
2425
* @param {import('../../types/cache-interceptor.d.ts').default.MemoryCacheStoreOpts | undefined} [opts]
@@ -73,12 +74,12 @@ class MemoryCacheStore {
7374
throw new TypeError(`expected key to be object, got ${typeof key}`)
7475
}
7576

77+
const topLevelKey = `${key.origin}:${key.path}`
78+
7679
const now = Date.now()
77-
return this.#entries.find((entry) => (
80+
return this.#entries.get(topLevelKey)?.find((entry) => (
7881
entry.deleteAt > now &&
7982
entry.method === key.method &&
80-
entry.origin === key.origin &&
81-
entry.path === key.path &&
8283
(entry.vary == null || Object.keys(entry.vary).every(headerName => entry.vary[headerName] === key.headers?.[headerName]))
8384
))
8485
}
@@ -96,6 +97,8 @@ class MemoryCacheStore {
9697
throw new TypeError(`expected value to be object, got ${typeof val}`)
9798
}
9899

100+
const topLevelKey = `${key.origin}:${key.path}`
101+
99102
const store = this
100103
const entry = { ...key, ...val, body: [], size: 0 }
101104

@@ -116,13 +119,25 @@ class MemoryCacheStore {
116119
callback(null)
117120
},
118121
final (callback) {
119-
store.#entries.push(entry)
120-
store.#size += entry.size
122+
let entries = store.#entries.get(topLevelKey)
123+
if (!entries) {
124+
entries = []
125+
store.#entries.set(topLevelKey, entries)
126+
}
127+
entries.push(entry)
121128

122-
while (store.#entries.length >= store.#maxCount || store.#size >= store.#maxSize) {
123-
const count = Math.max(0, store.#entries.length - store.#maxCount / 2)
124-
for (const entry of store.#entries.splice(0, count)) {
125-
store.#size -= entry.size
129+
store.#size += entry.size
130+
store.#count += 1
131+
132+
if (store.#size > store.#maxSize || store.#count > store.#maxCount) {
133+
for (const [key, entries] of store.#entries) {
134+
for (const entry of entries.splice(0, entries.length / 2)) {
135+
store.#size -= entry.size
136+
store.#count -= 1
137+
}
138+
if (entries.length === 0) {
139+
store.#entries.delete(key)
140+
}
126141
}
127142
}
128143

@@ -139,15 +154,13 @@ class MemoryCacheStore {
139154
throw new TypeError(`expected key to be object, got ${typeof key}`)
140155
}
141156

142-
const arr = []
143-
for (const entry of this.#entries) {
144-
if (entry.path === key.path && entry.origin === key.origin) {
145-
this.#size -= entry.size
146-
} else {
147-
arr.push(entry)
148-
}
157+
const topLevelKey = `${key.origin}:${key.path}`
158+
159+
for (const entry of this.#entries.get(topLevelKey) ?? []) {
160+
this.#size -= entry.size
161+
this.#count -= 1
149162
}
150-
this.#entries = arr
163+
this.#entries.delete(topLevelKey)
151164
}
152165
}
153166

0 commit comments

Comments
 (0)