#165·go-cache

Question About Expired

Author: xiaoliwangCreated Mar 6, 2023Updated Mar 6, 2023
go
func (c *cache) Get(k string) (interface{}, bool) {
    c.mu.RLock()
    // "Inlining" of get and Expired
    item, found := c.items[k]
    if !found {
        c.mu.RUnlock()
        return nil, false
    }
    if item.Expiration > 0 {
        if time.Now().UnixNano() > item.Expiration {
            c.mu.RUnlock()
            return nil, false
        }
    }
    c.mu.RUnlock()
    return item.Object, true
}

Why use item.Expiration > 0 && time.Now().UnixNano() > item.Expiration here, not to use item.Expired()