Increment an item of type float32 or float64 by n. Returns an error if the item's value is not floating point, if it was not found, or if it is not possible to increment it by n. Pass a negative number to decrement the value. To retrieve the incremented value, use one of the specialized methods, e.g
(k string, n float64)
| 251 | // value. To retrieve the incremented value, use one of the specialized methods, |
| 252 | // e.g. IncrementFloat64. |
| 253 | func (c *cache) IncrementFloat(k string, n float64) error { |
| 254 | c.mu.Lock() |
| 255 | v, found := c.items[k] |
| 256 | if !found || v.Expired() { |
| 257 | c.mu.Unlock() |
| 258 | return fmt.Errorf("Item %s not found", k) |
| 259 | } |
| 260 | switch v.Object.(type) { |
| 261 | case float32: |
| 262 | v.Object = v.Object.(float32) + float32(n) |
| 263 | case float64: |
| 264 | v.Object = v.Object.(float64) + n |
| 265 | default: |
| 266 | c.mu.Unlock() |
| 267 | return fmt.Errorf("The value for %s does not have type float32 or float64", k) |
| 268 | } |
| 269 | c.items[k] = v |
| 270 | c.mu.Unlock() |
| 271 | return nil |
| 272 | } |
| 273 | |
| 274 | // Increment an item of type int by n. Returns an error if the item's value is |
| 275 | // not an int, or if it was not found. If there is no error, the incremented |