Skip to content

Commit e90e128

Browse files
authored
feat: sqlite cache store (#3657)
Signed-off-by: flakey5 <[email protected]>
1 parent 0d06552 commit e90e128

File tree

10 files changed

+717
-34
lines changed

10 files changed

+717
-34
lines changed

docs/docs/api/CacheStore.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,21 @@ The `MemoryCacheStore` stores the responses in-memory.
1313

1414
**Options**
1515

16-
- `maxEntries` - The maximum amount of responses to store. Default `Infinity`.
16+
- `maxCount` - The maximum amount of responses to store. Default `Infinity`.
1717
- `maxEntrySize` - The maximum size in bytes that a response's body can be. If a response's body is greater than or equal to this, the response will not be cached.
1818

19+
### `SqliteCacheStore`
20+
21+
The `SqliteCacheStore` stores the responses in a SQLite database.
22+
Under the hood, it uses Node.js' [`node:sqlite`](https://nodejs.org/api/sqlite.html) api.
23+
The `SqliteCacheStore` is only exposed if the `node:sqlite` api is present.
24+
25+
**Options**
26+
27+
- `location` - The location of the SQLite database to use. Default `:memory:`.
28+
- `maxCount` - The maximum number of entries to store in the database. Default `Infinity`.
29+
- `maxEntrySize` - The maximum size in bytes that a resposne's body can be. If a response's body is greater than or equal to this, the response will not be cached. Default `Infinity`.
30+
1931
## Defining a Custom Cache Store
2032

2133
The store must implement the following functions:

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ module.exports.cacheStores = {
4848
MemoryCacheStore: require('./lib/cache/memory-cache-store')
4949
}
5050

51+
try {
52+
const SqliteCacheStore = require('./lib/cache/sqlite-cache-store')
53+
module.exports.cacheStores.SqliteCacheStore = SqliteCacheStore
54+
} catch (err) {
55+
if (err.code !== 'ERR_UNKNOWN_BUILTIN_MODULE') {
56+
throw err
57+
}
58+
}
59+
5160
module.exports.buildConnector = buildConnector
5261
module.exports.errors = errors
5362
module.exports.util = {

lib/cache/memory-cache-store.js

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const { Writable } = require('node:stream')
4+
const { assertCacheKey, assertCacheValue } = require('../util/cache.js')
45

56
/**
67
* @typedef {import('../../types/cache-interceptor.d.ts').default.CacheKey} CacheKey
@@ -41,17 +42,6 @@ class MemoryCacheStore {
4142
this.#maxCount = opts.maxCount
4243
}
4344

44-
if (opts.maxSize !== undefined) {
45-
if (
46-
typeof opts.maxSize !== 'number' ||
47-
!Number.isInteger(opts.maxSize) ||
48-
opts.maxSize < 0
49-
) {
50-
throw new TypeError('MemoryCacheStore options.maxSize must be a non-negative integer')
51-
}
52-
this.#maxSize = opts.maxSize
53-
}
54-
5545
if (opts.maxEntrySize !== undefined) {
5646
if (
5747
typeof opts.maxEntrySize !== 'number' ||
@@ -70,9 +60,7 @@ class MemoryCacheStore {
7060
* @returns {import('../../types/cache-interceptor.d.ts').default.GetResult | undefined}
7161
*/
7262
get (key) {
73-
if (typeof key !== 'object') {
74-
throw new TypeError(`expected key to be object, got ${typeof key}`)
75-
}
63+
assertCacheKey(key)
7664

7765
const topLevelKey = `${key.origin}:${key.path}`
7866

@@ -103,12 +91,8 @@ class MemoryCacheStore {
10391
* @returns {Writable | undefined}
10492
*/
10593
createWriteStream (key, val) {
106-
if (typeof key !== 'object') {
107-
throw new TypeError(`expected key to be object, got ${typeof key}`)
108-
}
109-
if (typeof val !== 'object') {
110-
throw new TypeError(`expected value to be object, got ${typeof val}`)
111-
}
94+
assertCacheKey(key)
95+
assertCacheValue(val)
11296

11397
const topLevelKey = `${key.origin}:${key.path}`
11498

@@ -142,7 +126,7 @@ class MemoryCacheStore {
142126
store.#size += entry.size
143127
store.#count += 1
144128

145-
if (store.#size > store.#maxSize || store.#count > store.#maxCount) {
129+
if (store.#size > store.#maxEntrySize || store.#count > store.#maxCount) {
146130
for (const [key, entries] of store.#entries) {
147131
for (const entry of entries.splice(0, entries.length / 2)) {
148132
store.#size -= entry.size

0 commit comments

Comments
 (0)