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)
| 108 | // Add an item to the cache only if an item doesn't already exist for the given |
| 109 | // key, or if the existing item has expired. Returns an error otherwise. |
| 110 | func (c *cache) Add(k string, x interface{}, d time.Duration) error { |
| 111 | c.mu.Lock() |
| 112 | _, found := c.get(k) |
| 113 | if found { |
| 114 | c.mu.Unlock() |
| 115 | return fmt.Errorf("Item %s already exists", k) |
| 116 | } |
| 117 | c.set(k, x, d) |
| 118 | c.mu.Unlock() |
| 119 | return nil |
| 120 | } |
| 121 | |
| 122 | // Set a new value for the cache key only if it already exists, and the existing |
| 123 | // item hasn't expired. Returns an error otherwise. |