Skip to content

Commit

Permalink
fix cache evict and ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
fschade committed Jun 10, 2021
1 parent a9f71e2 commit 300f057
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
28 changes: 18 additions & 10 deletions packages/web-pkg/src/cache/cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,32 +73,40 @@ describe('Cache', () => {

cache.set(3, 3, 10)
cache.set(4, 4, 20)
cache.set(5, 5, 0)
cache.set(6, 6, 0)

expect(cache.get(3)).toBe(3)
expect(cache.get(4)).toBe(4)
expect(cache.values().length).toBe(2)
expect(cache.keys().length).toBe(2)
expect(cache.entries().length).toBe(2)
expect(cache.get(5)).toBe(5)
expect(cache.get(6)).toBe(6)
expect(cache.values().length).toBe(4)
expect(cache.keys().length).toBe(4)
expect(cache.entries().length).toBe(4)

jest.setSystemTime(new Date().getTime() + 11)

expect(cache.get(3)).toBeFalsy()
expect(cache.get(4)).toBe(4)
expect(cache.values().length).toBe(1)
expect(cache.keys().length).toBe(1)
expect(cache.entries().length).toBe(1)
expect(cache.get(5)).toBe(5)
expect(cache.get(6)).toBe(6)
expect(cache.values().length).toBe(3)
expect(cache.keys().length).toBe(3)
expect(cache.entries().length).toBe(3)

jest.setSystemTime(new Date().getTime() + 10)

expect(cache.get(4)).toBeFalsy()
expect(cache.values().length).toBe(0)
expect(cache.keys().length).toBe(0)
expect(cache.entries().length).toBe(0)
expect(cache.get(5)).toBe(5)
expect(cache.get(6)).toBe(6)
expect(cache.values().length).toBe(2)
expect(cache.keys().length).toBe(2)
expect(cache.entries().length).toBe(2)
})

it('can handle capacity', () => {
const initialValues: number[] = [1, 2, 3, 4, 5]
const newValues: number[] = [6, 7, 8, 9, 10]
const newValues: number[] = [6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
const capacity = 5
const cache = newCache(initialValues, 0, capacity)

Expand Down
18 changes: 15 additions & 3 deletions packages/web-pkg/src/cache/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class Cache<K, V> {

public set(key: K, value: V, ttl?: number): V {
this.evict()
this.map.set(key, new CacheElement<V>(value, ttl || this.ttl))
this.map.set(key, new CacheElement<V>(value, isNaN(ttl) ? this.ttl : ttl))

return value
}
Expand All @@ -55,7 +55,7 @@ export default class Cache<K, V> {

public entries(): [K, V][] {
this.evict()
return Array.from(this.map).map(kv => [kv[0], kv[1].value])
return [...this.map.entries()].map(kv => [kv[0], kv[1].value])
}

public keys(): K[] {
Expand All @@ -70,9 +70,21 @@ export default class Cache<K, V> {

public evict(): void {
this.map.forEach((mv, mk) => {
if (mv.expired || (this.capacity && this.map.size > this.capacity)) {
if (mv.expired) {
this.delete(mk)
}
})

if (!this.capacity) {
return
}

for (const [k] of [...this.map.entries()]) {
if (this.map.size <= this.capacity) {
break
}

this.delete(k)
}
}
}

0 comments on commit 300f057

Please sign in to comment.