'Inline' set in Set, and do time checks before the lock
This commit is contained in:
parent
76f1250a65
commit
d461c5d2dd
13
cache.go
13
cache.go
@ -49,8 +49,19 @@ type cache struct {
|
|||||||
// (DefaultExpiration), the cache's default expiration time is used. If it is -1
|
// (DefaultExpiration), the cache's default expiration time is used. If it is -1
|
||||||
// (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
|
||||||
|
var e int64
|
||||||
|
if d == DefaultExpiration {
|
||||||
|
d = c.defaultExpiration
|
||||||
|
}
|
||||||
|
if d > 0 {
|
||||||
|
e = time.Now().Add(d).UnixNano()
|
||||||
|
}
|
||||||
c.mu.Lock()
|
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
|
// 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()
|
||||||
|
Loading…
Reference in New Issue
Block a user