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