-
Notifications
You must be signed in to change notification settings - Fork 81
/
main.go
66 lines (57 loc) · 1.09 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
// An example of using Redka with transactions.
package main
import (
"log"
"log/slog"
_ "github.com/mattn/go-sqlite3"
"github.com/nalgeon/redka"
)
func main() {
db, err := redka.Open("data.db", nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()
{
// Writable transaction.
updCount := 0
err := db.Update(func(tx *redka.Tx) error {
err := tx.Str().Set("name", "alice")
if err != nil {
return err
}
updCount++
err = tx.Str().Set("age", 25)
if err != nil {
return err
}
updCount++
return nil
})
slog.Info("updated", "count", updCount, "err", err)
}
{
// Read-only transaction.
type person struct {
name string
age int
}
var p person
err := db.View(func(tx *redka.Tx) error {
name, err := tx.Str().Get("name")
if err != nil {
return err
}
p.name = name.String()
age, err := tx.Str().Get("age")
if err != nil {
return err
}
// Only use MustInt() if you are sure that
// the key exists and is an integer.
p.age = age.MustInt()
return nil
})
slog.Info("get", "person", p, "err", err)
}
}