MCPcopy
hub / github.com/grafana/dskit / Add

Method Add

cache/memcached_client.go:377–401  ·  view source on GitHub ↗
(ctx context.Context, key string, value []byte, ttl time.Duration)

Source from the content-addressed store, hash-verified

375}
376
377func (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

Callers

nothing calls this directly

Calls 7

storeOperationMethod · 0.95
toSecondsFunction · 0.85
ErrorfMethod · 0.80
IsMethod · 0.80
DoneMethod · 0.65
AddMethod · 0.65
ErrMethod · 0.45

Tested by

no test coverage detected