Skip to content

Commit

Permalink
'Inline' set in Set, and do time checks before the lock
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickmn committed Dec 1, 2015
1 parent 76f1250 commit d461c5d
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion cache.go
Expand Up @@ -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()
Expand Down

0 comments on commit d461c5d

Please sign in to comment.