Skip to content

Commit

Permalink
fix: fix cache bytesLength indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Dec 8, 2022
1 parent a2d3a87 commit 91eeb24
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/main/js/cache.js
Expand Up @@ -71,17 +71,18 @@ export const withCache = (name, cb, ttl) => {

export const createCache = ({ttl, evictionTimeout = ttl, warmup, limit = Infinity}) => {
const store = new Map()
let totalByteLength = 0

const timer = setInterval(() => {
const now = Date.now()
store.forEach(({validTill, key}) => {
if (now > validTill) {
store.delete(key)
cache.del(key)
}
})
}, evictionTimeout)
let totalByteLength = 0

return {
const cache = {
add(key, value, _ttl) {
const byteLength = getByteLength(value)
if (totalByteLength + byteLength <= limit) {
Expand Down Expand Up @@ -117,4 +118,6 @@ export const createCache = ({ttl, evictionTimeout = ttl, warmup, limit = Infinit
warmup,
evictionTimeout
}

return cache
}

0 comments on commit 91eeb24

Please sign in to comment.