-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.go
168 lines (136 loc) · 3.17 KB
/
client.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package memcache
import (
"context"
"net"
"time"
"github.com/go-kiss/net/pool"
)
// Client memcache client
type Client struct {
addr string
pool pool.Pooler
}
// New init client
func New(addr string, initialCap int, maxCap int) (*Client, error) {
opts := pool.Options{
Dialer: func(ctx context.Context) (pool.Closer, error) {
var d net.Dialer
nc, err := d.DialContext(ctx, "tcp", addr)
if err != nil {
return nil, err
}
c := NewConn(nc)
return &pooledConn{nc: nc, c: c}, nil
},
PoolSize: maxCap,
MinIdleConns: initialCap,
IdleTimeout: time.Minute,
}
return &Client{pool: pool.New(opts)}, nil
}
type pooledConn struct {
nc net.Conn
c *Conn
}
func (pc *pooledConn) Close() error {
return pc.nc.Close()
}
// PoolStats 返回连接池状态
func (c *Client) PoolStats() *pool.Stats {
return c.pool.Stats()
}
func (c *Client) do(ctx context.Context, fn func(c *Conn) error) error {
mc, err := c.pool.Get(ctx)
if err != nil {
return err
}
pc := mc.C.(*pooledConn)
if d, ok := ctx.Deadline(); ok {
pc.nc.SetDeadline(d)
} else {
pc.nc.SetDeadline(time.Time{})
}
err = fn(pc.c)
defer c.put(mc, err)
return err
}
func (c *Client) put(pc *pool.Conn, err error) {
if IsResumableErr(err) {
c.pool.Put(pc)
return
}
// TODO 复用 bufio 对象
c.pool.Remove(pc)
}
// Add only set new key
func (c *Client) Add(ctx context.Context, item *Item) error {
return c.do(ctx, func(c *Conn) error {
return c.Add(item)
})
}
// CompareAndSwap cas set
func (c *Client) CompareAndSwap(ctx context.Context, item *Item) error {
return c.do(ctx, func(c *Conn) error {
return c.CompareAndSwap(item)
})
}
// Decrement decr key
func (c *Client) Decrement(ctx context.Context, key string, delta uint64) (d uint64, err error) {
err = c.do(ctx, func(c *Conn) error {
d, err = c.Decrement(key, delta)
return err
})
return
}
// Delete delete key
func (c *Client) Delete(ctx context.Context, key string) error {
return c.do(ctx, func(c *Conn) error {
return c.Delete(key)
})
}
// Get get one key
func (c *Client) Get(ctx context.Context, key string) (i *Item, err error) {
err = c.do(ctx, func(c *Conn) error {
i, err = c.Get(key)
return err
})
return
}
// GetMulti get multi keys
func (c *Client) GetMulti(ctx context.Context, keys []string) (is map[string]*Item, err error) {
err = c.do(ctx, func(c *Conn) error {
is, err = c.GetMulti(keys)
return err
})
return
}
// Increment incr key
func (c *Client) Increment(ctx context.Context, key string, delta uint64) (d uint64, err error) {
err = c.do(ctx, func(c *Conn) error {
d, err = c.Increment(key, delta)
return err
})
return
}
// Replace set old key
func (c *Client) Replace(ctx context.Context, item *Item) error {
return c.do(ctx, func(c *Conn) error {
return c.Replace(item)
})
}
// Set set key
func (c *Client) Set(ctx context.Context, item *Item) error {
return c.do(ctx, func(c *Conn) error {
return c.Set(item)
})
}
// Touch change ttl
func (c *Client) Touch(ctx context.Context, key string, seconds int32) error {
return c.do(ctx, func(c *Conn) error {
return c.Touch(key, seconds)
})
}
// Close close all connection
func (c *Client) Close() {
c.pool.Close()
}