From d461c5d2dd1e09fecf8be90a136289fadebbeef1 Mon Sep 17 00:00:00 2001 From: Patrick Mylund Nielsen Date: Tue, 1 Dec 2015 11:08:43 -0500 Subject: [PATCH] 'Inline' set in Set, and do time checks before the lock --- cache.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/cache.go b/cache.go index d58c603..ff2c1e2 100644 --- a/cache.go +++ b/cache.go @@ -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()