'Inline' set in Set, and do time checks before the lock

This commit is contained in:
Patrick Mylund Nielsen 2015-12-01 11:08:43 -05:00
parent 76f1250a65
commit d461c5d2dd
1 changed files with 12 additions and 1 deletions

View File

@ -49,8 +49,19 @@ type cache struct {
// (DefaultExpiration), the cache's default expiration time is used. If it is -1
// (NoExpiration), the item never expires.
func (c *cache) Set(k string, x interface{}, d time.Duration) {
// "Inlining" of set
var e int64
if d == DefaultExpiration {
d = c.defaultExpiration
}
if d > 0 {
e = time.Now().Add(d).UnixNano()
}
c.mu.Lock()
c.set(k, x, d)
c.items[k] = Item{
Object: x,
Expiration: e,
}
// TODO: Calls to mu.Unlock are currently not deferred because defer
// adds ~200 ns (as of go1.)
c.mu.Unlock()