redisgo

package module
v0.0.0-...-ec3d81c Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2020 License: Apache-2.0 Imports: 9 Imported by: 0

README

redisgo

redisgo is a Go Client for Redis (http://redis.io).

Some features include

  • minimal, high performance
  • based on redis protocol 2.0, support for all redis types and commands
  • connection pooling support
  • thread safe (goroutine safe)

Start

redisgo use redisgo.NewConnector(redisgo.Config{...}) to create connection with Redis server. You can use redisgo.Config to set host, port, pool size, timeout, etc.

package main

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

func main() {
	conn, err := redisgo.NewConnector(redisgo.Config{
		Host:    "127.0.0.1",
		Port:    6379,
		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 Redis operations go with redisgo.Connector.Cmd(), it accepts variable arguments. The first argument of Cmd() is the Redis 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("incrby", "key-incrby", 1)
conn.Cmd("hset", "key-hash", "field-1", "value-1")

Response

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

Response Status

the element of redisgo.Result.Status is the response code. all of the codes include:

  • ResultOK
  • ResultError
  • ResultNotFound
  • ResultBadArgument
  • ResultNoAuth
  • ResultServerError
  • ResultNetworkException
  • ResultTimeout
  • ResultUnknown
  • alias of func redisgo.Result.OK() bool
  • alias of func redisgo.Result.NotFound() bool

Examples:

if rs := conn.Cmd("set", "key", "value"); rs.OK() {
	// ...
}
if rs := conn.Cmd("set", "key", "value"); rs.Status == redisgo.ResultOK {
	// ...
}
Response Data

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

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

Examples:

// example 1
if rs := conn.Cmd("incrby", "key-incrby", 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

Performance

test environment
  • CPU: Intel i7-7700 CPU @ 3.60GHz (4 cores, 8 threads)
  • OS: CentOS 7.7.1908 x86_64
  • redis: version 5.0.7 (disable save the DB on disk)
  • data keys: 40 bytes each
  • data values: 1024 bytes each
typical performance in client num with 1, 10 and 50:

typical-benchmark

typical-benchmark

Refer URLs

Licensing

Licensed under the Apache License, Version 2.0

Documentation

Index

Constants

View Source
const (
	ResultOK uint8
	ResultError
	ResultNotFound
	ResultBadArgument
	ResultNoAuth
	ResultServerError
	ResultNetworkException
	ResultTimeout
	ResultUnknown
)

Variables

This section is empty.

Functions

This section is empty.

Types

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"`

	// A path of a UNIX socket file. Leave blank if using host and port
	Socket string `json:"socket"`

	// The connection timeout to a redis host (seconds)
	Timeout int `json:"timeout"`

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

type Connector

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

func NewConnector

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

func (*Connector) Close

func (c *Connector) Close()

func (*Connector) Cmd

func (c *Connector) Cmd(cmd string, args ...interface{}) *Result

type Result

type Result struct {
	Status uint8

	Items []*Result
	// contains filtered or unexported fields
}

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) Int16

func (r *Result) Int16() int16

func (*Result) Int32

func (r *Result) Int32() int32

func (*Result) Int64

func (r *Result) Int64() int64

func (*Result) Int8

func (r *Result) Int8() int8

func (*Result) JsonDecode

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

func (*Result) KvEach

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

func (*Result) KvLen

func (r *Result) KvLen() int

func (*Result) KvList

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

func (*Result) List

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

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) Uint16

func (r *Result) Uint16() uint16

func (*Result) Uint32

func (r *Result) Uint32() uint32

func (*Result) Uint64

func (r *Result) Uint64() uint64

func (*Result) Uint8

func (r *Result) Uint8() uint8

type ResultBytes

type ResultBytes []byte

Universal Bytes

func (ResultBytes) Bool

func (rb ResultBytes) Bool() bool

func (ResultBytes) Bytes

func (rb ResultBytes) Bytes() []byte

func (ResultBytes) Float32

func (rb ResultBytes) Float32() float32

float

func (ResultBytes) Float64

func (rb ResultBytes) Float64() float64

func (ResultBytes) Int

func (rb ResultBytes) Int() int

int

func (ResultBytes) Int16

func (rb ResultBytes) Int16() int16

func (ResultBytes) Int32

func (rb ResultBytes) Int32() int32

func (ResultBytes) Int64

func (rb ResultBytes) Int64() int64

func (ResultBytes) Int8

func (rb ResultBytes) Int8() int8

func (ResultBytes) JsonDecode

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

func (ResultBytes) String

func (rb ResultBytes) String() string

func (ResultBytes) Uint

func (rb ResultBytes) Uint() uint

unsigned int

func (ResultBytes) Uint16

func (rb ResultBytes) Uint16() uint16

func (ResultBytes) Uint32

func (rb ResultBytes) Uint32() uint32

func (ResultBytes) Uint64

func (rb ResultBytes) Uint64() uint64

func (ResultBytes) Uint8

func (rb ResultBytes) Uint8() uint8

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