-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
123 lines (100 loc) · 2.67 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"context"
"fmt"
"os"
"sync"
"github.com/redis/go-redis/v9"
"goa.design/pulse/rmap"
)
func main() {
ctx := context.Background()
// Create Redis client
rdb := redis.NewClient(&redis.Options{Addr: "localhost:6379", Password: os.Getenv("REDIS_PASSWORD")})
// Make sure Redis is up and running and we can connect to it
if err := rdb.Ping(ctx).Err(); err != nil {
panic(err)
}
// Join replicated map
m, err := rmap.Join(ctx, "basics", rdb)
if err != nil {
panic(err)
}
defer m.Close()
// Reset the map
if err := m.Reset(ctx); err != nil {
panic(err)
}
// Write a string
if _, err := m.Set(ctx, "stringval", "bar"); err != nil {
panic(err)
}
// Set returns the previous value
old, err := m.Set(ctx, "stringval", "baz")
if err != nil {
panic(err)
}
fmt.Printf("%q: old value for stringval: %v\n", m.Name, old)
// Delete a key
prev, err := m.Delete(ctx, "stringval")
if err != nil {
panic(err)
}
fmt.Printf("%q: deleted stringval: %v\n", m.Name, prev)
// Append to a list
if _, err := m.AppendValues(ctx, "listval", "first", "second"); err != nil {
panic(err)
}
// Append returns the new values of the list
vals, err := m.AppendValues(ctx, "listval", "third")
if err != nil {
panic(err)
}
fmt.Printf("%q: new values for listval: %v\n", m.Name, vals)
// Remove elements from a list and return the new content
vals, removed, err := m.RemoveValues(ctx, "listval", "first")
if err != nil {
panic(err)
}
fmt.Printf("%q: new values for listval: %v, removed: %v\n", m.Name, vals, removed)
// Increment an integer, starts at 0
if _, err := m.Inc(ctx, "intval", 1); err != nil {
panic(err)
}
// Increment returns the new value
val, err := m.Inc(ctx, "intval", 1)
if err != nil {
panic(err)
}
fmt.Printf("%q: new value for intval: %v\n", m.Name, val)
// Print a single value
st, ok := m.Get("stringval")
fmt.Printf("%q: stringval: %v, ok: %v\n", m.Name, st, ok)
// Print all keys
keys := m.Keys()
fmt.Printf("%q: keys: %v\n", m.Name, keys)
// Print the number of items in the map
ln := m.Len()
fmt.Printf("%q: len: %v\n", m.Name, ln)
// Print the final map
content := m.Map()
fmt.Printf("%q: final content: %v\n", m.Name, content)
// Subscribe to changes and make sure to unsubscribe when done
c := m.Subscribe()
defer m.Unsubscribe(c)
// Start a goroutine to listen for updates and stop when it gets one
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
<-c
}()
// Send updates
if _, err := m.Set(ctx, "foo", "bar"); err != nil {
panic(err)
}
// Wait for the updates to be received
fmt.Println("waiting for updates...")
wg.Wait()
fmt.Printf("Got update, %q: %v\n", m.Name, m.Map())
}