因為beego中cache子產品中使用了ssdb,是以準備學習下ssdb
(1)ssdb簡介
(2)ssdb的基本操作
(3)gossdb怎麼使用?
1、ssdb簡介
SSDB 是一個 C/C++ 語言開發的高性能 NoSQL 資料庫, 支援 KV, list, map(hash), zset(sorted set),qlist(隊列) 等資料結構, 用來替代或者與 Redis 配合存儲十億級别清單的資料.
SSDB 是穩定的, 生産環境使用的, 已經在許多網際網路公司得到廣泛使用, 如奇虎 360, TOPGAME.
特性
替代 Redis 資料庫, Redis 的 100 倍容量
LevelDB 網絡支援, 使用 C/C++ 開發
Redis API 相容, 支援 Redis 用戶端
适合存儲集合資料, 如kv, list, hashtable, zset,hset,qlist...
用戶端 API 支援的語言包括: C++, PHP, Python, Java, Go
持久化的隊列服務
主從複制, 負載均衡
既然有redis這種NoSQL資料庫,為什麼需要ssdb?
我在網上得到的結論是Redis是記憶體型,記憶體成本太高,SSDB針對這個弱點,使用硬碟存儲,使用Google高性能的存儲引擎LevelDB。
2、ssdb的基本操作
指令
3、gossdb包怎麼使用?
源碼,打開源碼檔案,代碼大概在190行左右,不是很多。這個包也不複雜。
結構體Client
1type Client struct {
2 sock *net.TCPConn
3 recv_buf bytes.Buffer
4}
相關方法:
1)建立Client
func Connect(ip string, port int) (*Client, error)
1func Connect(ip string, port int) (*Client, error) {
2 addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", ip, port))
3 if err != nil {
4 return nil, err
5 }
6 sock, err := net.DialTCP("tcp", nil, addr)
7 if err != nil {
8 return nil, err
9 }
10 var c Client
11 c.sock = sock
12 return &c, nil
13}
2)發送指令到ssdb伺服器
func (c *Client) Do(args ...interface{}) ([]string, error)
1func (c *Client) Do(args ...interface{}) ([]string, error) {
2 err := c.send(args)
3 if err != nil {
4 return nil, err
5 }
6 resp, err := c.recv()
7 return resp, err
8}
3)設定值
func (c *Client) Set(key string, val string) (interface{}, error)
1func (c *Client) Set(key string, val string) (interface{}, error) {
2 resp, err := c.Do("set", key, val)
3 if err != nil {
4 return nil, err
5 }
6 if len(resp) == 2 && resp[0] == "ok" {
7 return true, nil
8 }
9 return nil, fmt.Errorf("bad response")
10}
4)擷取值
1func (c *Client) Get(key string) (interface{}, error) {
2 resp, err := c.Do("get", key)
3 if err != nil {
4 return nil, err
5 }
6 if len(resp) == 2 && resp[0] == "ok" {
7 return resp[1], nil
8 }
9 if resp[0] == "not_found" {
10 return nil, nil
11 }
12 return nil, fmt.Errorf("bad response")
13}
5)func (c *Client) Del(key string) (interface{}, error)
删除值
1func (c *Client) Del(key string) (interface{}, error) {
2 resp, err := c.Do("del", key)
3 if err != nil {
4 return nil, err
5 }
6
7 //response looks like this: [ok 1]
8 if len(resp) > 0 && resp[0] == "ok" {
9 return true, nil
10 }
11 return nil, fmt.Errorf("bad response:resp:%v:", resp)
12}
6)func (c *Client) Send(args ...interface{}) error
發送指令到ssdb伺服器
1func (c *Client) Send(args ...interface{}) error {
2 return c.send(args);
3}
4
5func (c *Client) send(args []interface{}) error {
6 var buf bytes.Buffer
7 for _, arg := range args {
8 var s string
9 switch arg := arg.(type) {
10 case string:
11 s = arg
12 case []byte:
13 s = string(arg)
14 case []string:
15 for _, s := range arg {
16 buf.WriteString(fmt.Sprintf("%d", len(s)))
17 buf.WriteByte('\n')
18 buf.WriteString(s)
19 buf.WriteByte('\n')
20 }
21 continue
22 case int:
23 s = fmt.Sprintf("%d", arg)
24 case int64:
25 s = fmt.Sprintf("%d", arg)
26 case float64:
27 s = fmt.Sprintf("%f", arg)
28 case bool:
29 if arg {
30 s = "1"
31 } else {
32 s = "0"
33 }
34 case nil:
35 s = ""
36 default:
37 return fmt.Errorf("bad arguments")
38 }
39 buf.WriteString(fmt.Sprintf("%d", len(s)))
40 buf.WriteByte('\n')
41 buf.WriteString(s)
42 buf.WriteByte('\n')
43 }
44 buf.WriteByte('\n')
45 _, err := c.sock.Write(buf.Bytes())
46 return err
47}
7)func (c *Client) Recv() ([]string, error)
接收ssdb的資訊
1func (c *Client) Recv() ([]string, error) {
2 return c.recv();
3}
4
5func (c *Client) recv() ([]string, error) {
6 var tmp [8192]byte
7 for {
8 resp := c.parse()
9 if resp == nil || len(resp) > 0 {
10 return resp, nil
11 }
12 n, err := c.sock.Read(tmp[0:])
13 if err != nil {
14 return nil, err
15 }
16 c.recv_buf.Write(tmp[0:n])
17 }
18}
8)func (c *Client) Close() error
關閉連接配接
1func (c *Client) Close() error {
2 return c.sock.Close()
3}
解析指令
1func (c *Client) parse() []string {
2 resp := []string{}
3 buf := c.recv_buf.Bytes()
4 var idx, offset int
5 idx = 0
6 offset = 0
7
8 for {
9 idx = bytes.IndexByte(buf[offset:], '\n')
10 if idx == -1 {
11 break
12 }
13 p := buf[offset : offset+idx]
14 offset += idx + 1
15 //fmt.Printf("> [%s]\n", p);
16 if len(p) == 0 || (len(p) == 1 && p[0] == '\r') {
17 if len(resp) == 0 {
18 continue
19 } else {
20 var new_buf bytes.Buffer
21 new_buf.Write(buf[offset:])
22 c.recv_buf = new_buf
23 return resp
24 }
25 }
26
27 size, err := strconv.Atoi(string(p))
28 if err != nil || size < 0 {
29 return nil
30 }
31 if offset+size >= c.recv_buf.Len() {
32 break
33 }
34
35 v := buf[offset : offset+size]
36 resp = append(resp, string(v))
37 offset += size + 1
38 }
39
40 //fmt.Printf("buf.size: %d packet not ready...\n", len(buf))
41 return []string{}
42}
原文釋出時間為:2019-1-6
本文作者:Golang語言社群
本文來自雲栖社群合作夥伴“
Golang語言社群”,了解相關資訊可以關注“Golangweb”微信公衆号