-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
153 lines (120 loc) · 3.31 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/QuangTung97/go-memcache/memcache"
"github.com/QuangTung97/memproxy"
"github.com/QuangTung97/memproxy/item"
"github.com/jmoiron/sqlx"
_ "github.com/go-sql-driver/mysql"
)
type service struct {
db *sqlx.DB
memcache memproxy.Memcache
}
var dropTableSQL = `
DROP TABLE IF EXISTS customer
`
var createTableSQL = `
CREATE TABLE customer (
id INT PRIMARY KEY,
username VARCHAR(100) NOT NULL
)
`
// Customer ...
type Customer struct {
ID int64 `db:"id" json:"id"`
Username string `db:"username" json:"username"`
}
// Marshal ...
func (c Customer) Marshal() ([]byte, error) {
return json.Marshal(c)
}
// GetKey ...
func (c Customer) GetKey() CustomerKey {
return CustomerKey{ID: c.ID}
}
func unmarshalCustomer(data []byte) (Customer, error) {
var c Customer
err := json.Unmarshal(data, &c)
return c, err
}
// CustomerKey ...
type CustomerKey struct {
ID int64
}
func (k CustomerKey) String() string {
return fmt.Sprintf("customers:%d", k.ID)
}
func (s *service) getCustomersFromDB(ctx context.Context, keys []CustomerKey) ([]Customer, error) {
ids := make([]int64, 0, len(keys))
for _, k := range keys {
ids = append(ids, k.ID)
}
fmt.Println("Multi Get from Database with IDs =", ids)
query, args, err := sqlx.In(`SELECT id, username FROM customer WHERE id IN (?)`, ids)
if err != nil {
return nil, err
}
var customers []Customer
err = s.db.SelectContext(ctx, &customers, query, args...)
return customers, err
}
func (s *service) newCustomerItem(pipe memproxy.Pipeline) *item.Item[Customer, CustomerKey] {
return item.New[Customer, CustomerKey](
pipe,
unmarshalCustomer,
item.NewMultiGetFiller[Customer, CustomerKey](
s.getCustomersFromDB,
Customer.GetKey,
),
)
}
func main() {
client, err := memcache.New("localhost:11211", 3)
if err != nil {
panic(err)
}
mc := memproxy.NewPlainMemcache(client)
db := sqlx.MustConnect("mysql", "root:1@tcp(localhost:3306)/memtest?")
db.MustExec(dropTableSQL)
db.MustExec(createTableSQL)
db.MustExec(`
INSERT INTO customer (id, username)
VALUES (11, "user01"), (12, "user02")
`)
svc := &service{
db: db,
memcache: mc,
}
pipe := mc.Pipeline(context.Background())
customerItem := svc.newCustomerItem(pipe)
fn1 := customerItem.Get(context.Background(), CustomerKey{ID: 11})
fn2 := customerItem.Get(context.Background(), CustomerKey{ID: 12})
// not found
fn3 := customerItem.Get(context.Background(), CustomerKey{ID: 13})
c1, err := fn1()
fmt.Println("CUSTOMER 01:", c1, err)
c2, err := fn2()
fmt.Println("CUSTOMER 02:", c2, err)
c3, err := fn3()
fmt.Println("CUSTOMER 03:", c3, err)
// should use defer pipe.Finish()
pipe.Finish()
// The 3 keys: customers:11, customers:12, customers:13 will exist in the memcached server
// Can check using: telnet localhost 11211
// get customers:11
// =============================================
// Do Get Again
// =============================================
pipe = mc.Pipeline(context.Background())
customerItem = svc.newCustomerItem(pipe)
fn1 = customerItem.Get(context.Background(), CustomerKey{ID: 11})
fn2 = customerItem.Get(context.Background(), CustomerKey{ID: 12})
c1, err = fn1()
fmt.Println("CUSTOMER 01 AGAIN:", c1, err)
c2, err = fn2()
fmt.Println("CUSTOMER 02 AGAIN:", c2, err)
pipe.Finish()
}