-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
137 lines (118 loc) · 3.06 KB
/
main.go
File metadata and controls
137 lines (118 loc) · 3.06 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"fmt"
"time"
"github.com/pkg/errors"
"go.knocknote.io/rapidash"
)
type UserLogin struct {
ID int64
UserID int64
UserSessionID int64
LoginParamID int64
Name string
CreatedAt time.Time
UpdatedAt time.Time
}
func (u *UserLogin) EncodeRapidash(enc rapidash.Encoder) error {
if u.ID != 0 {
enc.Int64("id", u.ID)
}
enc.Int64("user_id", u.UserID)
enc.Int64("user_session_id", u.UserSessionID)
enc.Int64("login_param_id", u.LoginParamID)
enc.String("name", u.Name)
enc.Time("created_at", u.CreatedAt)
enc.Time("updated_at", u.UpdatedAt)
return enc.Error()
}
func (u *UserLogin) DecodeRapidash(dec rapidash.Decoder) error {
u.ID = dec.Int64("id")
u.UserID = dec.Int64("user_id")
u.UserSessionID = dec.Int64("user_session_id")
u.LoginParamID = dec.Int64("login_param_id")
u.Name = dec.String("name")
u.CreatedAt = dec.Time("created_at")
u.UpdatedAt = dec.Time("updated_at")
return dec.Error()
}
type UserLoginSlice []*UserLogin
func (u *UserLoginSlice) EncodeRapidash(enc rapidash.Encoder) error {
for _, v := range *u {
if err := v.EncodeRapidash(enc.New()); err != nil {
return errors.WithStack(err)
}
}
return nil
}
func (u *UserLoginSlice) DecodeRapidash(dec rapidash.Decoder) error {
decLen := dec.Len()
*u = make([]*UserLogin, decLen)
for i := 0; i < decLen; i++ {
var v UserLogin
if err := v.DecodeRapidash(dec.At(i)); err != nil {
return errors.WithStack(err)
}
(*u)[i] = &v
}
return nil
}
func (u *UserLogin) Struct() *rapidash.Struct {
return rapidash.NewStruct("user_logins").
FieldInt64("id").
FieldInt64("user_id").
FieldInt64("user_session_id").
FieldInt64("login_param_id").
FieldString("name").
FieldTime("created_at").
FieldTime("updated_at")
}
func main() {
cache, err := rapidash.New(
rapidash.ServerAddrs([]string{"localhost:11211"}),
rapidash.Timeout(3*time.Second),
)
if err != nil {
panic(err)
}
tx, err := cache.Begin()
defer func() {
if err := tx.Commit(); err != nil {
fmt.Printf("err:%v\n", err)
}
}()
if err != nil {
panic(err)
}
if err := tx.Create("key", rapidash.Int(1)); err != nil {
panic(err)
}
var v int
if err := tx.Find("key", rapidash.IntPtr(&v)); err != nil {
panic(err)
}
fmt.Printf("v:%v\n", v)
userLogin := UserLogin{
ID: 1,
UserID: 1,
Name: "bob",
}
if err := tx.Create("user_login", new(UserLogin).Struct().Cast(&userLogin)); err != nil {
panic(err)
}
var newUserLogin UserLogin
if err := tx.Find("user_login", new(UserLogin).Struct().Cast(&newUserLogin)); err != nil {
panic(err)
}
fmt.Printf("newUserLogin:%v\n", newUserLogin.Name)
var userLoginSlice UserLoginSlice
userLoginSlice = append(userLoginSlice, &userLogin)
if err := tx.Create("user_login_slice", rapidash.Structs(&userLoginSlice, new(UserLogin).Struct())); err != nil {
panic(err)
}
var newUserLoginSlice UserLoginSlice
if err := tx.Find("user_login_slice", rapidash.Structs(&newUserLoginSlice, new(UserLogin).Struct())); err != nil {
panic(err)
}
fmt.Printf("newUserLoginSlice[0]:%v\n", newUserLoginSlice[0].Name)
}