Skip to content

Commit d461c5d

Browse files
committed
'Inline' set in Set, and do time checks before the lock
1 parent 76f1250 commit d461c5d

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

cache.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,19 @@ type cache struct {
4949
// (DefaultExpiration), the cache's default expiration time is used. If it is -1
5050
// (NoExpiration), the item never expires.
5151
func (c *cache) Set(k string, x interface{}, d time.Duration) {
52+
// "Inlining" of set
53+
var e int64
54+
if d == DefaultExpiration {
55+
d = c.defaultExpiration
56+
}
57+
if d > 0 {
58+
e = time.Now().Add(d).UnixNano()
59+
}
5260
c.mu.Lock()
53-
c.set(k, x, d)
61+
c.items[k] = Item{
62+
Object: x,
63+
Expiration: e,
64+
}
5465
// TODO: Calls to mu.Unlock are currently not deferred because defer
5566
// adds ~200 ns (as of go1.)
5667
c.mu.Unlock()

0 commit comments

Comments
 (0)