Update cache.go

This commit is contained in:
GITSRC 2020-04-16 17:29:30 +08:00 committed by GitHub
parent 46f4078530
commit 6919aa7277
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 11 deletions

View File

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