MCPcopy
hub / github.com/spacecloud-io/space-cloud / CheckAndSet

Method CheckAndSet

runner/modules/pubsub/store.go:11–33  ·  view source on GitHub ↗

CheckAndSet sets the key value pair and returns if the key already existed or not

(ctx context.Context, key, value string, ttl time.Duration)

Source from the content-addressed store, hash-verified

9
10// CheckAndSet sets the key value pair and returns if the key already existed or not
11func (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
36func (m *Module) CheckIfKeyExists(ctx context.Context, key string) (bool, error) {

Callers 1

ScaleUpMethod · 0.80

Calls 3

getTopicNameMethod · 0.95
GetMethod · 0.45
SetMethod · 0.45

Tested by

no test coverage detected