@@ -18,7 +18,8 @@ class MemoryCacheStore {
18
18
#maxEntrySize = Infinity
19
19
20
20
#size = 0
21
- #entries = [ ]
21
+ #count = 0
22
+ #entries = new Map ( )
22
23
23
24
/**
24
25
* @param {import('../../types/cache-interceptor.d.ts').default.MemoryCacheStoreOpts | undefined } [opts]
@@ -73,12 +74,12 @@ class MemoryCacheStore {
73
74
throw new TypeError ( `expected key to be object, got ${ typeof key } ` )
74
75
}
75
76
77
+ const topLevelKey = `${ key . origin } :${ key . path } `
78
+
76
79
const now = Date . now ( )
77
- return this . #entries. find ( ( entry ) => (
80
+ return this . #entries. get ( topLevelKey ) ?. find ( ( entry ) => (
78
81
entry . deleteAt > now &&
79
82
entry . method === key . method &&
80
- entry . origin === key . origin &&
81
- entry . path === key . path &&
82
83
( entry . vary == null || Object . keys ( entry . vary ) . every ( headerName => entry . vary [ headerName ] === key . headers ?. [ headerName ] ) )
83
84
) )
84
85
}
@@ -96,6 +97,8 @@ class MemoryCacheStore {
96
97
throw new TypeError ( `expected value to be object, got ${ typeof val } ` )
97
98
}
98
99
100
+ const topLevelKey = `${ key . origin } :${ key . path } `
101
+
99
102
const store = this
100
103
const entry = { ...key , ...val , body : [ ] , size : 0 }
101
104
@@ -116,13 +119,25 @@ class MemoryCacheStore {
116
119
callback ( null )
117
120
} ,
118
121
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 )
121
128
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
+ }
126
141
}
127
142
}
128
143
@@ -139,15 +154,13 @@ class MemoryCacheStore {
139
154
throw new TypeError ( `expected key to be object, got ${ typeof key } ` )
140
155
}
141
156
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
149
162
}
150
- this . #entries = arr
163
+ this . #entries. delete ( topLevelKey )
151
164
}
152
165
}
153
166
0 commit comments