Add an item to the cache, replacing any existing item. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.
(k string, x interface{}, d time.Duration)
| 49 | // (DefaultExpiration), the cache's default expiration time is used. If it is -1 |
| 50 | // (NoExpiration), the item never expires. |
| 51 | 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 | } |
| 60 | c.mu.Lock() |
| 61 | c.items[k] = Item{ |
| 62 | Object: x, |
| 63 | Expiration: e, |
| 64 | } |
| 65 | // TODO: Calls to mu.Unlock are currently not deferred because defer |
| 66 | // adds ~200 ns (as of go1.) |
| 67 | c.mu.Unlock() |
| 68 | } |
| 69 | |
| 70 | func (c *cache) set(k string, x interface{}, d time.Duration) { |
| 71 | var e int64 |