Skip to content

Commit

Permalink
perf: store data as blobs in sql cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Nov 26, 2024
1 parent 303b2ca commit 7e326f2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 37 deletions.
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
4 changes: 2 additions & 2 deletions test/cache-interceptor/cache-store-test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,14 @@ async function readResponse ({ body: src, ...response }) {
*/
const body = []
stream.on('data', chunk => {
body.push(chunk.toString())
body.push(chunk)
})

await once(stream, 'end')

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

Expand Down

0 comments on commit 7e326f2

Please sign in to comment.