Skip to content

Commit 125846f

Browse files
committed
perf: store data as blobs in sql cache
1 parent 303b2ca commit 125846f

File tree

3 files changed

+13
-42
lines changed

3 files changed

+13
-42
lines changed

lib/cache/sqlite-cache-store.js

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { DatabaseSync } = require('node:sqlite')
44
const { Writable } = require('stream')
55
const { assertCacheKey, assertCacheValue } = require('../util/cache.js')
66

7-
const VERSION = 2
7+
const VERSION = 3
88

99
/**
1010
* @typedef {import('../../types/cache-interceptor.d.ts').default.CacheStore} CacheStore
@@ -17,7 +17,7 @@ const VERSION = 2
1717
* body: string
1818
* } & import('../../types/cache-interceptor.d.ts').default.CacheValue} SqliteStoreValue
1919
*/
20-
class SqliteCacheStore {
20+
module.exports = class SqliteCacheStore {
2121
#maxEntrySize = Infinity
2222
#maxCount = Infinity
2323

@@ -103,7 +103,7 @@ class SqliteCacheStore {
103103
method TEXT NOT NULL,
104104
105105
-- Data returned to the interceptor
106-
body TEXT NULL,
106+
body BUF NULL,
107107
deleteAt INTEGER NOT NULL,
108108
statusCode INTEGER NOT NULL,
109109
statusMessage TEXT NOT NULL,
@@ -222,7 +222,7 @@ class SqliteCacheStore {
222222
* @type {import('../../types/cache-interceptor.d.ts').default.GetResult}
223223
*/
224224
const result = {
225-
body: value.body ? parseBufferArray(JSON.parse(value.body)) : null,
225+
body: Buffer.from(value.body),
226226
statusCode: value.statusCode,
227227
statusMessage: value.statusMessage,
228228
headers: value.headers ? JSON.parse(value.headers) : undefined,
@@ -276,7 +276,7 @@ class SqliteCacheStore {
276276
if (existingValue) {
277277
// Updating an existing response, let's overwrite it
278278
store.#updateValueQuery.run(
279-
JSON.stringify(stringifyBufferArray(body)),
279+
Buffer.concat(body),
280280
value.deleteAt,
281281
value.statusCode,
282282
value.statusMessage,
@@ -294,7 +294,7 @@ class SqliteCacheStore {
294294
store.#insertValueQuery.run(
295295
url,
296296
key.method,
297-
JSON.stringify(stringifyBufferArray(body)),
297+
Buffer.concat(body),
298298
value.deleteAt,
299299
value.statusCode,
300300
value.statusMessage,
@@ -435,32 +435,3 @@ function headerValueEquals (lhs, rhs) {
435435

436436
return lhs === rhs
437437
}
438-
439-
/**
440-
* @param {Buffer[]} buffers
441-
* @returns {string[]}
442-
*/
443-
function stringifyBufferArray (buffers) {
444-
const output = new Array(buffers.length)
445-
for (let i = 0; i < buffers.length; i++) {
446-
output[i] = buffers[i].toString()
447-
}
448-
449-
return output
450-
}
451-
452-
/**
453-
* @param {string[]} strings
454-
* @returns {Buffer[]}
455-
*/
456-
function parseBufferArray (strings) {
457-
const output = new Array(strings.length)
458-
459-
for (let i = 0; i < strings.length; i++) {
460-
output[i] = Buffer.from(strings[i])
461-
}
462-
463-
return output
464-
}
465-
466-
module.exports = SqliteCacheStore

test/cache-interceptor/cache-store-test-utils.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function cacheStoreTests (CacheStore) {
5959
...requestValue,
6060
etag: undefined,
6161
cacheControlDirectives: {},
62-
body: requestBody
62+
body: Buffer.concat(requestBody.map(x => Buffer.from(x)))
6363
})
6464

6565
// Now let's write another request to the store
@@ -98,7 +98,7 @@ function cacheStoreTests (CacheStore) {
9898
...anotherValue,
9999
etag: undefined,
100100
cacheControlDirectives: {},
101-
body: anotherBody
101+
body: Buffer.concat(anotherBody.map(x => Buffer.from(x)))
102102
})
103103
})
104104

@@ -134,7 +134,7 @@ function cacheStoreTests (CacheStore) {
134134
...requestValue,
135135
etag: undefined,
136136
cacheControlDirectives: {},
137-
body: requestBody
137+
body: Buffer.concat(requestBody.map(x => Buffer.from(x)))
138138
})
139139
})
140140

@@ -209,7 +209,7 @@ function cacheStoreTests (CacheStore) {
209209
...responseValue,
210210
etag: undefined,
211211
cacheControlDirectives: {},
212-
body: requestBody
212+
body: Buffer.concat(requestBody.map(x => Buffer.from(x)))
213213
})
214214

215215
const nonMatchingRequest = {
@@ -253,14 +253,14 @@ async function readResponse ({ body: src, ...response }) {
253253
*/
254254
const body = []
255255
stream.on('data', chunk => {
256-
body.push(chunk.toString())
256+
body.push(Buffer.from(chunk))
257257
})
258258

259259
await once(stream, 'end')
260260

261261
return {
262262
...response,
263-
body
263+
body: Buffer.concat(body)
264264
}
265265
}
266266

test/cache-interceptor/sqlite-cache-store-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ test('SqliteCacheStore works nicely with multiple stores', async (t) => {
7070
...requestValue,
7171
etag: undefined,
7272
cacheControlDirectives: undefined,
73-
body: requestBody
73+
body: Buffer.concat(requestBody.map(x => Buffer.from(x)))
7474
})
7575

7676
// Make sure we got the expected response from store b

0 commit comments

Comments
 (0)