karlseguin / ccache

A golang LRU Cache for high concurrency
MIT License
1.28k stars 119 forks source link

Fetch returns expired items #4

Closed dvdplm closed 7 years ago

dvdplm commented 8 years ago

I would expect Fetch() to behave a little differently and not return stale items. Instead it seems like the fetch function argument is invoked only if the item is missing entirely from the cache. Would you be open to PRs changing that behavior, or provide a FetchAndRefresh() that runs the fetch function both if the item is expired and missing?

Essentially this:

func (c *Cache) Fetch(key string, duration time.Duration, fetch func() (interface{}, error)) (*Item, error) {
    item := c.Get(key)
    if item != nil && !item.Expired() {
        return item, nil
    }
    value, err := fetch()
    if err != nil {
        return nil, err
    }
    return c.set(key, value, duration), nil
}
karlseguin commented 8 years ago

I think changing the behavior of Fetch is warranted. Anyone who possibly wants a stale value can achieve it themselves. PR welcomed

dvdplm commented 8 years ago

See PR #5

utrack commented 7 years ago

I think it can be closed?

dvdplm commented 7 years ago

I agree.