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

fix(LruObject): do not bumb entry if there is only one entry #33

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions src/LruMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export class LruMap {
}

bumpLru(item) {
if (this.last === item) {
return // Item is already the last one, no need to bump
}

const last = this.last
const next = item.next
const prev = item.prev
Expand Down
4 changes: 4 additions & 0 deletions src/LruObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export class LruObject {
}

bumpLru(item) {
if (this.last === item) {
return // Item is already the last one, no need to bump
}

const last = this.last
const next = item.next
const prev = item.prev
Expand Down
28 changes: 28 additions & 0 deletions test/LruMap.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,34 @@ describe('LruMap', function () {
expect(item2Pre).toBe(items[2])
expect(item2Post).toBe(items[2])
})

it('does not overwrite cache.first', async () => {
cache = new LruMap(5, 500)

const key = '10.0.0.1'
const value = 100

cache.set(key, value)
expect(cache.first).not.toBeNull()

cache.get(key)
expect(cache.first).not.toBeNull()
})

it('does not cause TypeError when reaching the cache limit', async () => {
const maxCacheSize = 3
cache = new LruMap(maxCacheSize, 500)

const key = '10.0.0.1'
const value = 100

cache.set(key, value)
cache.get(key)

for (let i = 0; i < maxCacheSize; i++) {
cache.set(i, i)
}
})
})

describe('getMany', () => {
Expand Down
28 changes: 28 additions & 0 deletions test/LruObject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,34 @@ describe('LruObject', function () {
expect(item2Pre).toBe(items[2])
expect(item2Post).toBe(items[2])
})

it('does not overwrite cache.first', async () => {
cache = new LruObject(5, 500)

const key = '10.0.0.1'
const value = 100

cache.set(key, value)
expect(cache.first).not.toBeNull()

cache.get(key)
expect(cache.first).not.toBeNull()
})

it('does not cause TypeError when reaching the cache limit', async () => {
const maxCacheSize = 3
cache = new LruObject(maxCacheSize, 500)

const key = '10.0.0.1'
const value = 100

cache.set(key, value)
cache.get(key)

for (let i = 0; i < maxCacheSize; i++) {
cache.set(i, i)
}
})
})

describe('getMany', () => {
Expand Down
Loading