Increment an item of type int by n. Returns an error if the item's value is not an int, or if it was not found. If there is no error, the incremented value is returned.
(k string, n int)
| 275 | // not an int, or if it was not found. If there is no error, the incremented |
| 276 | // value is returned. |
| 277 | func (c *cache) IncrementInt(k string, n int) (int, error) { |
| 278 | c.mu.Lock() |
| 279 | v, found := c.items[k] |
| 280 | if !found || v.Expired() { |
| 281 | c.mu.Unlock() |
| 282 | return 0, fmt.Errorf("Item %s not found", k) |
| 283 | } |
| 284 | rv, ok := v.Object.(int) |
| 285 | if !ok { |
| 286 | c.mu.Unlock() |
| 287 | return 0, fmt.Errorf("The value for %s is not an int", k) |
| 288 | } |
| 289 | nv := rv + n |
| 290 | v.Object = nv |
| 291 | c.items[k] = v |
| 292 | c.mu.Unlock() |
| 293 | return nv, nil |
| 294 | } |
| 295 | |
| 296 | // Increment an item of type int8 by n. Returns an error if the item's value is |
| 297 | // not an int8, or if it was not found. If there is no error, the incremented |