'Inline' Get and Expired

This commit is contained in:
Patrick Mylund Nielsen 2015-11-30 13:50:17 -05:00
parent 4e0d34ef00
commit 31c7be0bed
1 changed files with 12 additions and 3 deletions

View File

@ -26,7 +26,11 @@ func (item Item) expired(now time.Time) bool {
// Returns true if the item has expired. // Returns true if the item has expired.
func (item Item) Expired() bool { func (item Item) Expired() bool {
return item.expired(time.Now()) // "Inlining" of expired
if item.Expiration == emptyTime {
return false
}
return item.Expiration.Before(time.Now())
} }
const ( const (
@ -108,9 +112,14 @@ func (c *cache) Replace(k string, x interface{}, d time.Duration) error {
// whether the key was found. // whether the key was found.
func (c *cache) Get(k string) (interface{}, bool) { func (c *cache) Get(k string) (interface{}, bool) {
c.mu.RLock() c.mu.RLock()
x, found := c.get(k) // "Inlining" of get
item, found := c.items[k]
if !found || item.Expired() {
c.mu.RUnlock() c.mu.RUnlock()
return x, found return nil, false
}
c.mu.RUnlock()
return item.Object, found
} }
func (c *cache) get(k string) (interface{}, bool) { func (c *cache) get(k string) (interface{}, bool) {