Add an item to the cache only if an item doesn't already exist for the given key, or if the existing item has expired. Returns an error otherwise.
(k string, x interface{}, d time.Duration)
| 90 | // Add an item to the cache only if an item doesn't already exist for the given |
| 91 | // key, or if the existing item has expired. Returns an error otherwise. |
| 92 | func (c *cache) Add(k string, x interface{}, d time.Duration) error { |
| 93 | c.mu.Lock() |
| 94 | _, found := c.get(k) |
| 95 | if found { |
| 96 | c.mu.Unlock() |
| 97 | return fmt.Errorf("Item %s already exists", k) |
| 98 | } |
| 99 | c.set(k, x, d) |
| 100 | c.mu.Unlock() |
| 101 | return nil |
| 102 | } |
| 103 | |
| 104 | // Set a new value for the cache key only if it already exists, and the existing |
| 105 | // item hasn't expired. Returns an error otherwise. |