-
Notifications
You must be signed in to change notification settings - Fork 1
/
mkv.go
61 lines (50 loc) · 1016 Bytes
/
mkv.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
package kv
import (
"errors"
"github.com/bradfitz/gomemcache/memcache"
)
var ErrNoClient error = errors.New("no memcache client")
var mClient *memcache.Client
//memcache 的配置
type MConfig struct {
Addrs []string `toml:"addrs" json:"addrs"`
PoolSize int `toml:"pool_size" json:"pool_size"`
}
func (c *MConfig) GetAddrs() []string {
if c != nil {
return c.Addrs
}
return []string{}
}
func (c *MConfig) GetPoolSize() int {
if c != nil {
return c.PoolSize
}
return 5
}
func InitMcClient(c *MConfig) {
mClient = memcache.New(c.GetAddrs()...)
mClient.MaxIdleConns = c.GetPoolSize()
return
}
func GetMcClient() *memcache.Client {
return mClient
}
func Get(key string) ([]byte, error) {
client := GetMcClient()
if client == nil {
return nil, ErrNoClient
}
item, err := client.Get(key)
if err != nil {
return nil, err
}
return item.Value, nil
}
func Del(key string) error {
client := GetMcClient()
if client == nil {
return ErrNoClient
}
return client.Delete(key)
}