Avoid more time.Now() calls

This commit is contained in:
Patrick Mylund Nielsen 2017-04-18 22:45:37 -04:00
parent cd479c6eeb
commit c63dcbab96
1 changed files with 25 additions and 7 deletions

View File

@ -177,8 +177,10 @@ func (c *cache) Get(k string) (interface{}, bool) {
} }
return nil, false return nil, false
} }
var now int64
if item.Expiration > 0 { if item.Expiration > 0 {
if time.Now().UnixNano() > item.Expiration { now = time.Now().UnixNano()
if now > item.Expiration {
if c.maxItems > 0 { if c.maxItems > 0 {
c.mu.Unlock() c.mu.Unlock()
} else { } else {
@ -188,7 +190,10 @@ func (c *cache) Get(k string) (interface{}, bool) {
} }
} }
if c.maxItems > 0 { if c.maxItems > 0 {
item.Accessed = time.Now().UnixNano() if now == 0 {
now = time.Now().UnixNano()
}
item.Accessed = now
c.items[k] = item c.items[k] = item
c.mu.Unlock() c.mu.Unlock()
} else { } else {
@ -205,13 +210,18 @@ func (c *cache) get(k string) (interface{}, bool) {
return nil, false return nil, false
} }
// "Inlining" of Expired // "Inlining" of Expired
var now int64
if item.Expiration > 0 { if item.Expiration > 0 {
if time.Now().UnixNano() > item.Expiration { now = time.Now().UnixNano()
if now > item.Expiration {
return nil, false return nil, false
} }
} }
if c.maxItems > 0 { if c.maxItems > 0 {
item.Accessed = time.Now().UnixNano() if now == 0 {
now = time.Now().UnixNano()
}
item.Accessed = now
c.items[k] = item c.items[k] = item
} }
return item.Object, true return item.Object, true
@ -239,8 +249,10 @@ func (c *cache) GetWithExpiration(k string) (interface{}, time.Time, bool) {
} }
return nil, time.Time{}, false return nil, time.Time{}, false
} }
var now int64
if item.Expiration > 0 { if item.Expiration > 0 {
if time.Now().UnixNano() > item.Expiration { now = time.Now().UnixNano()
if now > item.Expiration {
if c.maxItems > 0 { if c.maxItems > 0 {
c.mu.Unlock() c.mu.Unlock()
} else { } else {
@ -249,7 +261,10 @@ func (c *cache) GetWithExpiration(k string) (interface{}, time.Time, bool) {
return nil, time.Time{}, false return nil, time.Time{}, false
} }
if c.maxItems > 0 { if c.maxItems > 0 {
item.Accessed = time.Now().UnixNano() if now == 0 {
now = time.Now().UnixNano()
}
item.Accessed = now
c.items[k] = item c.items[k] = item
c.mu.Unlock() c.mu.Unlock()
} else { } else {
@ -258,7 +273,10 @@ func (c *cache) GetWithExpiration(k string) (interface{}, time.Time, bool) {
return item.Object, time.Unix(0, item.Expiration), true return item.Object, time.Unix(0, item.Expiration), true
} }
if c.maxItems > 0 { if c.maxItems > 0 {
item.Accessed = time.Now().UnixNano() if now == 0 {
now = time.Now().UnixNano()
}
item.Accessed = now
c.items[k] = item c.items[k] = item
c.mu.Unlock() c.mu.Unlock()
} else { } else {