-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
63 lines (55 loc) · 1.2 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
package mc
// https://docs.memcached.org/protocols/meta/
import (
"encoding/binary"
"time"
)
var crlf = []byte("\r\n")
var endian = binary.BigEndian
const (
compressed = 2
serialized = 4
)
func New(opts *Options) (*Client, error) {
opts.setDefaults()
if len(opts.Addrs) == 0 {
return nil, ErrNoServers
}
var (
cli = &Client{
opts: opts,
pool: pool{
idle: make(map[string]chan *conn),
dialTimeout: opts.DialTimeout,
connMaxLifetime: opts.ConnMaxLifetime,
},
encodeKey: binaryEncodeKey,
}
)
if opts.DisableBinaryEncodedKeys {
cli.encodeKey = func(s string) (string, error) {
if !checkKey(s) {
return "", ErrMalformedKey
}
return s, nil
}
}
for _, s := range opts.Addrs {
cli.pool.idle[s] = make(chan *conn, opts.MaxIdleConnsPerAddr)
}
return cli, nil
}
type Client struct {
pool pool
opts *Options
encodeKey func(string) (string, error)
}
func (c *Client) PurgeNamespace(ns string) error {
if _, err := c.nsVersion(ns, 1); err != nil {
return err
}
return nil
}
func (c *Client) nsVersion(ns string, delta uint64) (uint64, error) {
return c.Inc("namespace::"+ns, delta, 0, WithInitialValue(uint64(time.Now().UnixNano())))
}