Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: store data as blobs in sql cache #3885

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 6 additions & 35 deletions lib/cache/sqlite-cache-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { DatabaseSync } = require('node:sqlite')
const { Writable } = require('stream')
const { assertCacheKey, assertCacheValue } = require('../util/cache.js')

const VERSION = 2
const VERSION = 3

/**
* @typedef {import('../../types/cache-interceptor.d.ts').default.CacheStore} CacheStore
Expand All @@ -17,7 +17,7 @@ const VERSION = 2
* body: string
* } & import('../../types/cache-interceptor.d.ts').default.CacheValue} SqliteStoreValue
*/
class SqliteCacheStore {
module.exports = class SqliteCacheStore {
#maxEntrySize = Infinity
#maxCount = Infinity

Expand Down Expand Up @@ -103,7 +103,7 @@ class SqliteCacheStore {
method TEXT NOT NULL,

-- Data returned to the interceptor
body TEXT NULL,
body BUF NULL,
deleteAt INTEGER NOT NULL,
statusCode INTEGER NOT NULL,
statusMessage TEXT NOT NULL,
Expand Down Expand Up @@ -222,7 +222,7 @@ class SqliteCacheStore {
* @type {import('../../types/cache-interceptor.d.ts').default.GetResult}
*/
const result = {
body: value.body ? parseBufferArray(JSON.parse(value.body)) : null,
body: Buffer.from(value.body),
statusCode: value.statusCode,
statusMessage: value.statusMessage,
headers: value.headers ? JSON.parse(value.headers) : undefined,
Expand Down Expand Up @@ -276,7 +276,7 @@ class SqliteCacheStore {
if (existingValue) {
// Updating an existing response, let's overwrite it
store.#updateValueQuery.run(
JSON.stringify(stringifyBufferArray(body)),
Buffer.concat(body),
value.deleteAt,
value.statusCode,
value.statusMessage,
Expand All @@ -294,7 +294,7 @@ class SqliteCacheStore {
store.#insertValueQuery.run(
url,
key.method,
JSON.stringify(stringifyBufferArray(body)),
Buffer.concat(body),
value.deleteAt,
value.statusCode,
value.statusMessage,
Expand Down Expand Up @@ -435,32 +435,3 @@ function headerValueEquals (lhs, rhs) {

return lhs === rhs
}

/**
* @param {Buffer[]} buffers
* @returns {string[]}
*/
function stringifyBufferArray (buffers) {
const output = new Array(buffers.length)
for (let i = 0; i < buffers.length; i++) {
output[i] = buffers[i].toString()
}

return output
}

/**
* @param {string[]} strings
* @returns {Buffer[]}
*/
function parseBufferArray (strings) {
const output = new Array(strings.length)

for (let i = 0; i < strings.length; i++) {
output[i] = Buffer.from(strings[i])
}

return output
}

module.exports = SqliteCacheStore
12 changes: 6 additions & 6 deletions test/cache-interceptor/cache-store-test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function cacheStoreTests (CacheStore) {
...requestValue,
etag: undefined,
cacheControlDirectives: {},
body: requestBody
body: Buffer.concat(requestBody.map(x => Buffer.from(x)))
})

// Now let's write another request to the store
Expand Down Expand Up @@ -98,7 +98,7 @@ function cacheStoreTests (CacheStore) {
...anotherValue,
etag: undefined,
cacheControlDirectives: {},
body: anotherBody
body: Buffer.concat(anotherBody.map(x => Buffer.from(x)))
})
})

Expand Down Expand Up @@ -134,7 +134,7 @@ function cacheStoreTests (CacheStore) {
...requestValue,
etag: undefined,
cacheControlDirectives: {},
body: requestBody
body: Buffer.concat(requestBody.map(x => Buffer.from(x)))
})
})

Expand Down Expand Up @@ -209,7 +209,7 @@ function cacheStoreTests (CacheStore) {
...responseValue,
etag: undefined,
cacheControlDirectives: {},
body: requestBody
body: Buffer.concat(requestBody.map(x => Buffer.from(x)))
})

const nonMatchingRequest = {
Expand Down Expand Up @@ -253,14 +253,14 @@ async function readResponse ({ body: src, ...response }) {
*/
const body = []
stream.on('data', chunk => {
body.push(chunk.toString())
body.push(Buffer.from(chunk))
})

await once(stream, 'end')

return {
...response,
body
body: Buffer.concat(body)
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/cache-interceptor/sqlite-cache-store-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ test('SqliteCacheStore works nicely with multiple stores', async (t) => {
...requestValue,
etag: undefined,
cacheControlDirectives: undefined,
body: requestBody
body: Buffer.concat(requestBody.map(x => Buffer.from(x)))
})

// Make sure we got the expected response from store b
Expand All @@ -80,7 +80,7 @@ test('SqliteCacheStore works nicely with multiple stores', async (t) => {
...requestValue,
etag: undefined,
cacheControlDirectives: undefined,
body: requestBody
body: Buffer.concat(requestBody.map(x => Buffer.from(x)))
})
})

Expand Down
Loading