Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Nov 15, 2024
1 parent cb4fa88 commit 530ec18
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions lib/cache/memory-cache-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ const { Writable } = require('node:stream')
*/
class MemoryCacheStore {
#maxCount = Infinity

#maxEntrySize = Infinity

#entryCount = 0

/**
* @type {Map<string, Map<string, GetResult[]>>}
*/
#data = new Map()
#map = new Map()
#arr = []

/**
* @param {import('../../types/cache-interceptor.d.ts').default.MemoryCacheStoreOpts | undefined} [opts]
Expand Down Expand Up @@ -55,7 +53,7 @@ class MemoryCacheStore {
}

get isFull () {
return this.#entryCount >= this.#maxCount
return this.#arr.length >= this.#maxCount
}

/**
Expand Down Expand Up @@ -115,6 +113,7 @@ class MemoryCacheStore {
let value = findValue(key, values)
if (!value) {
value = { ...opts, body }
store.#arr.push(value)
values.push(value)
} else {
Object.assign(value, opts, { body })
Expand All @@ -129,7 +128,7 @@ class MemoryCacheStore {
* @param {CacheKey} key
*/
delete (key) {
this.#data.delete(`${key.origin}:${key.path}`)
this.#map.delete(`${key.origin}:${key.path}`)
}

/**
Expand All @@ -145,10 +144,10 @@ class MemoryCacheStore {

// https://www.rfc-editor.org/rfc/rfc9111.html#section-2-3
const topLevelKey = `${key.origin}:${key.path}`
let cachedPaths = this.#data.get(topLevelKey)
let cachedPaths = this.#map.get(topLevelKey)
if (!cachedPaths) {
cachedPaths = new Map()
this.#data.set(topLevelKey, cachedPaths)
this.#map.set(topLevelKey, cachedPaths)
}

let value = cachedPaths.get(key.method)
Expand All @@ -161,17 +160,22 @@ class MemoryCacheStore {
}

#prune () {
const now = Date.now()
for (const [key, cachedPaths] of this.#data) {
// TODO (perf): This could be implemented more efficiently...

const count = Math.max(0, this.#arr.length - this.#maxCount / 2)
for (const value of this.#arr.splice(0, count)) {
value.body = null
}

for (const [key, cachedPaths] of this.#map) {
for (const [method, prev] of cachedPaths) {
const next = prev.filter(({ deleteAt }) => deleteAt > now)
const next = prev.filter(({ body }) => body == null)
if (next.length === 0) {
cachedPaths.delete(method)
if (cachedPaths.size === 0) {
this.#data.delete(key)
this.#map.delete(key)
}
} else if (next.length !== prev.length) {
this.#entryCount -= prev.length - next.length
cachedPaths.set(method, next)
}
}
Expand Down

0 comments on commit 530ec18

Please sign in to comment.