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

feat(cache): use vary headers to compare cached response with request headers #3

Closed
wants to merge 15 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix(cache): update size of cache after purging.
  • Loading branch information
IsakT committed Aug 22, 2024
commit 550decb21f0a4dfa53f5edbc12e57e73d40f151a
9 changes: 6 additions & 3 deletions lib/interceptor/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export class CacheStore {
#getQuery
#purgeQuery
#database
#sizeQuery
#size = 0
#maxSize = 128e9

Expand Down Expand Up @@ -152,6 +153,8 @@ export class CacheStore {
)

this.#purgeQuery = this.#database.prepare('DELETE FROM cacheInterceptor WHERE expires < ?')

this.#sizeQuery = this.#database.prepare('SELECT SUM(size) FROM cacheInterceptor')
}

set(key, { data, vary, size, expires }) {
Expand Down Expand Up @@ -199,9 +202,7 @@ export class CacheStore {
if (this.#size == null || this.#size > this.#maxSize) {
this.#purgeQuery.run(Date.now())

this.#size = Object.values(
this.#database.prepare('SELECT SUM(size) FROM cacheInterceptor').get(),
)[0]
this.#size = Object.values(this.#sizeQuery.get())[0]

// In the case where the cache is full but has no expired entries yet, delete 10% of the cache, ordered by
// the oldest entries according to the 'expires' column.
Expand All @@ -215,6 +216,8 @@ export class CacheStore {
LIMIT (SELECT ROUND(COUNT(*) * 0.10 + 1) FROM cacheInterceptor)
);
`)

this.#size = Object.values(this.#sizeQuery.get())[0]
}
IsakT marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Down