ssdbgo

package module
v0.0.0-...-85e083f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

README

ssdbgo

ssdbgo is a Go Client for SSDB (http://ssdb.io).

Some features include

  • minimal, high performance
  • support for all SSDB types and commands
  • connection pooling support
  • thread safe (goroutine safe)

Start

  • ssdbgo use ssdbgo.NewConnector(ssdbgo.Config{...}) to create connection with SSDB server. You can use ssdbgo.Config to set host, port, pool size, timeout, etc.
package main

import (
	"github.com/lynkdb/ssdbgo"
)

func main() {

	conn, err := ssdbgo.NewConnector(ssdbgo.Config{
		Host:    "127.0.0.1",
		Port:    6380,
		Timeout: 3, // timeout in second, default to 10
		MaxConn: 1, // max connection number, default to 1
		// Auth:    "foobared",
	})
	if err != nil {
		return
	}

	conn.Cmd("set", "key", "value")
	conn.Close()
}

Request: all SSDB operations go with ssdbgo.Connector.Cmd(), it accepts variable arguments. The first argument of Cmd() is the SSDB command, for example "get", "set", etc. The rest arguments(maybe none) are the arguments of that command.

Examples:

conn.Cmd("set", "key", "value")
conn.Cmd("incr", "key-incr", 1)
conn.Cmd("hset", "name-hash", "key-1", "value-1")

Response

the ssdbgo.Connector.Cmd() method will return an Object of ssdbgo.Result

Response Status

The element of ssdbgo.Result.Status is the response code, "ok" means the current command are valid results. The response code may be "not_found" if you are calling "get" on an non-exist key. all of the codes include:

const (
	ResultOK          = "ok"
	ResultNotFound    = "not_found"
	ResultError       = "error"
	ResultFail        = "fail"
	ResultClientError = "client_error"
)
Response Data

use the following method to get a dynamic data type what you want to need.

  • ssdbgo.Result.Bytes() []byte
  • ssdbgo.Result.String() string
  • ssdbgo.Result.Bool() bool
  • ssdbgo.Result.Int() int
  • ssdbgo.Result.Int8() int8
  • ssdbgo.Result.Int16() int16
  • ssdbgo.Result.Int32() int32
  • ssdbgo.Result.Int64() int64
  • ssdbgo.Result.Uint() uint
  • ssdbgo.Result.Uint8() uint8
  • ssdbgo.Result.Uint16() uint16
  • ssdbgo.Result.Uint32() uint32
  • ssdbgo.Result.Uint64() uint64
  • ssdbgo.Result.Float32() float32
  • ssdbgo.Result.Float64() float64
  • ssdbgo.Result.List() []ssdbgo.ResultBytes
  • ssdbgo.Result.KvEach(fn func(key, value ssdbgo.ResultBytes)) int
  • ssdbgo.Result.KvLen() int
  • ssdbgo.Result.KvList() []*ssdbgo.ResultEntry
  • ssdbgo.Result.JsonDecode(obj interface{}) error

Examples:

// example 1
if rs := conn.Cmd("incr", "key-incr", 1); rs.OK() {
	fmt.Println("return int", rs.Int())
}

// example 2
var rsobject struct {
	Name string `json:"name"`
}
if rs := conn.Cmd("get", "key-json"); rs.OK() {
	if err := rs.JsonDecode(&rsobject); err == nil {
		fmt.Println("return json.name", rsobject.Name)
	}
}

the more examples of result.APIs can visit: example/example.go

Refer URLs

Licensing

Licensed under the Apache License, Version 2.0

Documentation

Index

Constants

View Source
const (
	ResultOK          = "ok"
	ResultNotFound    = "not_found"
	ResultError       = "error"
	ResultFail        = "fail"
	ResultClientError = "client_error"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Batch

type Batch struct {
	// contains filtered or unexported fields
}

func (*Batch) Cmd

func (b *Batch) Cmd(args ...interface{})

func (*Batch) Exec

func (b *Batch) Exec() ([]*Result, error)

type Client

type Client struct {
	// contains filtered or unexported fields
}

func Connect

func Connect(ip string, port int) (*Client, error)

func (*Client) Close

func (c *Client) Close() error

Close The Client Connection

func (*Client) Cmd

func (c *Client) Cmd(args ...interface{}) *Result

type Config

type Config struct {

	// Database server hostname or IP. Leave blank if using unix sockets
	Host string `json:"host"`

	// Database server port. Leave blank if using unix sockets
	Port uint16 `json:"port"`

	// Password for authentication
	Auth string `json:"auth"`

	// The connection timeout to a ssdb server (seconds)
	Timeout int `json:"timeout"`

	// Maximum number of connections
	MaxConn int `json:"maxconn"`

	// Retry number of Cmd(), default: 1
	CmdRetryNum int `json:"cmd_retry_num"`
}

type Connector

type Connector struct {
	// contains filtered or unexported fields
}

func NewConnector

func NewConnector(cfg Config) (*Connector, error)

func (*Connector) Batch

func (cr *Connector) Batch() *Batch

Batch is in DEVELOPMENT PREVIEW, DO NOT USE IT IN PRODUCTION

This feature is implemented in client side, the ssdb-server does not support batch command execution. The server will execute each command as if they are separated.

func (*Connector) Close

func (cr *Connector) Close()

func (*Connector) Cmd

func (cr *Connector) Cmd(args ...interface{}) *Result

type Result

type Result struct {
	Status string
	Items  []ResultBytes
}

func (*Result) Bool

func (r *Result) Bool() bool

func (*Result) Bytes

func (r *Result) Bytes() []byte

func (*Result) Float32

func (r *Result) Float32() float32

func (*Result) Float64

func (r *Result) Float64() float64

func (*Result) Int

func (r *Result) Int() int

func (*Result) Int8

func (r *Result) Int8() int8

func (*Result) Int16

func (r *Result) Int16() int16

func (*Result) Int32

func (r *Result) Int32() int32

func (*Result) Int64

func (r *Result) Int64() int64

func (*Result) JsonDecode

func (r *Result) JsonDecode(v interface{}) error

Json returns the map that marshals from the reply bytes as json in response .

func (*Result) KvEach

func (r *Result) KvEach(fn func(key, value ResultBytes)) int

func (*Result) KvLen

func (r *Result) KvLen() int

func (*Result) KvList

func (rs *Result) KvList() []*ResultEntry

func (*Result) List

func (r *Result) List() []ResultBytes

func (*Result) NotFound

func (r *Result) NotFound() bool

func (*Result) OK

func (r *Result) OK() bool

func (*Result) String

func (r *Result) String() string

func (*Result) Uint

func (r *Result) Uint() uint

func (*Result) Uint8

func (r *Result) Uint8() uint8

func (*Result) Uint16

func (r *Result) Uint16() uint16

func (*Result) Uint32

func (r *Result) Uint32() uint32

func (*Result) Uint64

func (r *Result) Uint64() uint64

type ResultBytes

type ResultBytes []byte

Universal Bytes

func (ResultBytes) Bool

func (rd ResultBytes) Bool() bool

func (ResultBytes) Bytes

func (rd ResultBytes) Bytes() []byte

func (ResultBytes) Float32

func (rd ResultBytes) Float32() float32

float

func (ResultBytes) Float64

func (rd ResultBytes) Float64() float64

func (ResultBytes) Int

func (rd ResultBytes) Int() int

int

func (ResultBytes) Int8

func (rd ResultBytes) Int8() int8

func (ResultBytes) Int16

func (rd ResultBytes) Int16() int16

func (ResultBytes) Int32

func (rd ResultBytes) Int32() int32

func (ResultBytes) Int64

func (rd ResultBytes) Int64() int64

func (ResultBytes) JsonDecode

func (rd ResultBytes) JsonDecode(v interface{}) error

func (ResultBytes) String

func (rd ResultBytes) String() string

func (ResultBytes) Uint

func (rd ResultBytes) Uint() uint

unsigned int

func (ResultBytes) Uint8

func (rd ResultBytes) Uint8() uint8

func (ResultBytes) Uint16

func (rd ResultBytes) Uint16() uint16

func (ResultBytes) Uint32

func (rd ResultBytes) Uint32() uint32

func (ResultBytes) Uint64

func (rd ResultBytes) Uint64() uint64

type ResultEntry

type ResultEntry struct {
	Key   ResultBytes
	Value ResultBytes
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL