jellydator / ttlcache

An in-memory cache with item expiration and generics
MIT License
883 stars 115 forks source link

Add `Range` method #102

Closed gozeloglu closed 1 year ago

gozeloglu commented 1 year ago
swithek commented 1 year ago

@gozeloglu three PRs in a row, you're on fire!

As for testing, I think something like this should work:

c := prepCache("1", "2", "3", "4", "5")
var results []string
c.Range(func(item *Item) {
    results = append(results, item.Key())
    return item.Key() != "4"
})
assert.Equal(t, []string{"1", "2", "3", "4"}, results) // order is important
gozeloglu commented 1 year ago

@gozeloglu three PRs in a row, you're on fire!

As for testing, I think something like this should work:

c := prepCache("1", "2", "3", "4", "5")
var results []string
c.Range(func(item *Item) {
    results = append(results, item.Key())
    return item.Key() != "4"
})
assert.Equal(t, []string{"1", "2", "3", "4"}, results) // order is important

I added a simple test but I only checked the "5" because of map internal.

gozeloglu commented 1 year ago

Hi @swithek. Can you review the last changes?

swithek commented 1 year ago

@gozeloglu sorry for the delay. I'll review your changes sometime in the next few days.

swithek commented 1 year ago

I added a simple test but I only checked the "5" because of map internal.

My proposal in the comment above uses the LRU list instead of the map, so you should be able to get predictable results and check all items in the result slice. @gozeloglu

gozeloglu commented 1 year ago

I added a simple test but I only checked the "5" because of map internal.

My proposal in the comment above uses the LRU list instead of the map, so you should be able to get predictable results and check all items in the result slice. @gozeloglu

I updated the test. The keys are added to the front of the linked list. So, I checked in reverse order.