CheckAndSet sets the key value pair and returns if the key already existed or not
(ctx context.Context, key, value string, ttl time.Duration)
| 9 | |
| 10 | // CheckAndSet sets the key value pair and returns if the key already existed or not |
| 11 | func (m *Module) CheckAndSet(ctx context.Context, key, value string, ttl time.Duration) (bool, error) { |
| 12 | // Flag to check if the value already existed |
| 13 | exists := true |
| 14 | |
| 15 | // See if the key exists |
| 16 | key = m.getTopicName(key) |
| 17 | err := m.client.Get(ctx, key).Err() |
| 18 | if err != nil && err != redis.Nil { |
| 19 | return exists, err |
| 20 | } |
| 21 | |
| 22 | // Mark key as exists |
| 23 | if err == redis.Nil { |
| 24 | exists = false |
| 25 | } |
| 26 | |
| 27 | // Set the key value pair |
| 28 | if err := m.client.Set(ctx, key, value, ttl).Err(); err != nil { |
| 29 | return exists, err |
| 30 | } |
| 31 | |
| 32 | return exists, nil |
| 33 | } |
| 34 | |
| 35 | // CheckIfKeyExists checks if the key exists |
| 36 | func (m *Module) CheckIfKeyExists(ctx context.Context, key string) (bool, error) { |
no test coverage detected