Skip to content

Commit

Permalink
refactor: sqlite versioning (#3870)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag authored Nov 22, 2024
1 parent 7283a54 commit 0ea7897
Showing 1 changed file with 14 additions and 12 deletions.
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);
`)

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

0 comments on commit 0ea7897

Please sign in to comment.