Skip to content
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
2 changes: 1 addition & 1 deletion lib/cache/sqlite-cache-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ module.exports = class SqliteCacheStore {
existingValue.id
)
} else {
this.#prune()
// New response, let's insert it
this.#insertValueQuery.run(
url,
Expand All @@ -301,6 +300,7 @@ module.exports = class SqliteCacheStore {
value.cachedAt,
value.staleAt
)
this.#prune()
}
}

Expand Down
43 changes: 39 additions & 4 deletions test/cache-interceptor/sqlite-cache-store-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ test('SqliteCacheStore works nicely with multiple stores', { skip: runtimeFeatur
test('SqliteCacheStore maxEntries', { skip: runtimeFeatures.has('sqlite') === false }, async () => {
const SqliteCacheStore = require('../../lib/cache/sqlite-cache-store.js')

const store = new SqliteCacheStore({
maxCount: 10
})
const maxCount = 10
const store = new SqliteCacheStore({ maxCount })

for (let i = 0; i < 20; i++) {
/**
Expand Down Expand Up @@ -109,7 +108,43 @@ test('SqliteCacheStore maxEntries', { skip: runtimeFeatures.has('sqlite') === fa
writeBody(writable, body)
}

strictEqual(store.size <= 11, true)
strictEqual(store.size <= maxCount, true)
})

test('SqliteCacheStore enforces maxCount after insert', { skip: runtimeFeatures.has('sqlite') === false }, () => {
const SqliteCacheStore = require('../../lib/cache/sqlite-cache-store.js')

const store = new SqliteCacheStore({ maxCount: 1 })

store.set(
{ origin: 'localhost', path: '/a', method: 'GET', headers: {} },
{
statusCode: 200,
statusMessage: '',
headers: {},
cachedAt: Date.now(),
staleAt: Date.now() + 10_000,
deleteAt: Date.now() + 20_000,
body: Buffer.from('a')
}
)

store.set(
{ origin: 'localhost', path: '/b', method: 'GET', headers: {} },
{
statusCode: 200,
statusMessage: '',
headers: {},
cachedAt: Date.now() + 1,
staleAt: Date.now() + 10_001,
deleteAt: Date.now() + 20_001,
body: Buffer.from('b')
}
)

strictEqual(store.size, 1)
strictEqual(store.get({ origin: 'localhost', path: '/a', method: 'GET', headers: {} }), undefined)
notEqual(store.get({ origin: 'localhost', path: '/b', method: 'GET', headers: {} }), undefined)
})

test('SqliteCacheStore two writes', { skip: runtimeFeatures.has('sqlite') === false }, async () => {
Expand Down
Loading