karlseguin / ccache

A golang LRU Cache for high concurrency
MIT License
1.27k stars 118 forks source link

ttl not working #83

Closed HoganEdwardChu closed 9 months ago

HoganEdwardChu commented 9 months ago

Sometimes cache give old result even though ttl expires.

How can we solve this situation? Could you give me some help? We do default cache configuration @karlseguin

karlseguin commented 9 months ago

Might help with more info, but, by default, cache.Get is designed to return expired items.

https://github.com/karlseguin/ccache#get

By returning expired items, CCache lets you decide if you want to serve stale content or not. For example, you might decide to serve up slightly stale content (< 30 seconds old) while re-fetching newer data in the background. You might also decide to serve up infinitely stale content if you're unable to get new data from your source.

So you can use the Expired() or TTL() (which would be negative).

HoganEdwardChu commented 9 months ago

thanks for reply @karlseguin

currently we do like this


func GetValueByCache(cache *ccache.Cache, orderNumber int) (..., bool) {
    value := cache.Get(orderNumber)

    if value != nil  {
        return value, true
    }
    return nil, false
}

and if this return boolean is false we do cache set. Set but what you mean is this right?


func GetValueByCache(cache *ccache.Cache, orderNumber int) (..., bool) {
    value := cache.Get(orderNumber)

    if value != nil && !value.Expired() {
        return value, true
    }
    return nil, false
}

-> And this modification makes after expired, cache will not give old value.

karlseguin commented 9 months ago

Yes

HoganEdwardChu commented 9 months ago

Thanks~