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

refactor: sqlite versioning #3870

Merged
merged 1 commit into from
Nov 22, 2024
Merged
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
26 changes: 14 additions & 12 deletions lib/cache/sqlite-cache-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const { DatabaseSync } = require('node:sqlite')
const { Writable } = require('stream')
const { assertCacheKey, assertCacheValue } = require('../util/cache.js')

const VERSION = 1

/**
* @typedef {import('../../types/cache-interceptor.d.ts').default.CacheStore} CacheStore
* @implements {CacheStore}
Expand Down Expand Up @@ -94,7 +96,7 @@ class SqliteCacheStore {
this.#db = new DatabaseSync(opts?.location ?? ':memory:')

this.#db.exec(`
CREATE TABLE IF NOT EXISTS cacheInterceptorV1 (
CREATE TABLE IF NOT EXISTS cacheInterceptorV${VERSION} (
-- Data specific to us
id INTEGER PRIMARY KEY AUTOINCREMENT,
url TEXT NOT NULL,
Expand All @@ -112,9 +114,9 @@ class SqliteCacheStore {
staleAt INTEGER NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV1_url ON cacheInterceptorV1(url);
CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV1_method ON cacheInterceptorV1(method);
CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV1_deleteAt ON cacheInterceptorV1(deleteAt);
CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV${VERSION}_url ON cacheInterceptorV${VERSION}(url);
CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV${VERSION}_method ON cacheInterceptorV${VERSION}(method);
CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV${VERSION}_deleteAt ON cacheInterceptorV${VERSION}(deleteAt);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do? What's the idx_ prefix?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do?

As in CREATE INDEX ...? It creates map of a column to an id in the table. Helps speed up reads when we do a SELECT ... WHERE ... but at the cost of some write performance (since it writes to the table and the indices)

What's the idx_ prefix?

Short for index, it's just a naming convention

`)

this.#getValuesQuery = this.#db.prepare(`
Expand All @@ -129,7 +131,7 @@ class SqliteCacheStore {
vary,
cachedAt,
staleAt
FROM cacheInterceptorV1
FROM cacheInterceptorV${VERSION}
WHERE
url = ?
AND method = ?
Expand All @@ -138,7 +140,7 @@ class SqliteCacheStore {
`)

this.#updateValueQuery = this.#db.prepare(`
UPDATE cacheInterceptorV1 SET
UPDATE cacheInterceptorV${VERSION} SET
body = ?,
deleteAt = ?,
statusCode = ?,
Expand All @@ -153,7 +155,7 @@ class SqliteCacheStore {
`)

this.#insertValueQuery = this.#db.prepare(`
INSERT INTO cacheInterceptorV1 (
INSERT INTO cacheInterceptorV${VERSION} (
url,
method,
body,
Expand All @@ -170,25 +172,25 @@ class SqliteCacheStore {
`)

this.#deleteByUrlQuery = this.#db.prepare(
'DELETE FROM cacheInterceptorV1 WHERE url = ?'
`DELETE FROM cacheInterceptorV${VERSION} WHERE url = ?`
)

this.#countEntriesQuery = this.#db.prepare(
'SELECT COUNT(*) AS total FROM cacheInterceptorV1'
`SELECT COUNT(*) AS total FROM cacheInterceptorV${VERSION}`
)

this.#deleteExpiredValuesQuery = this.#db.prepare(
'DELETE FROM cacheInterceptorV1 WHERE deleteAt <= ?'
`DELETE FROM cacheInterceptorV${VERSION} WHERE deleteAt <= ?`
)

this.#deleteOldValuesQuery = this.#maxCount === Infinity
? null
: this.#db.prepare(`
DELETE FROM cacheInterceptorV1
DELETE FROM cacheInterceptorV${VERSION}
WHERE id IN (
SELECT
id
FROM cacheInterceptorV1
FROM cacheInterceptorV${VERSION}
ORDER BY cachedAt DESC
LIMIT ?
)
Expand Down
Loading