| 375 | } |
| 376 | |
| 377 | func (c *MemcachedClient) Add(ctx context.Context, key string, value []byte, ttl time.Duration) error { |
| 378 | return c.storeOperation(ctx, key, value, ttl, opAdd, func(ctx context.Context, key string, value []byte, ttl time.Duration) error { |
| 379 | select { |
| 380 | case <-ctx.Done(): |
| 381 | return ctx.Err() |
| 382 | default: |
| 383 | ttlSeconds, ok := toSeconds(ttl) |
| 384 | if !ok { |
| 385 | return fmt.Errorf("%w: for add operation on %s %s", ErrInvalidTTL, key, ttl) |
| 386 | } |
| 387 | |
| 388 | err := c.client.Add(&memcache.Item{ |
| 389 | Key: key, |
| 390 | Value: value, |
| 391 | Expiration: ttlSeconds, |
| 392 | }) |
| 393 | |
| 394 | if errors.Is(err, memcache.ErrNotStored) { |
| 395 | return fmt.Errorf("%w: for add operation on %s", ErrNotStored, key) |
| 396 | } |
| 397 | |
| 398 | return err |
| 399 | } |
| 400 | }) |
| 401 | } |
| 402 | |
| 403 | // toSeconds converts a time.Duration to seconds as an int32 and returns a boolean |
| 404 | // indicating if the value is valid to be used as a TTL. Durations might not be valid |