client

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2019 License: Apache-2.0 Imports: 12 Imported by: 1

README

Client

This package implements a minimalist client for working with redis servers.

Example

package main

import (
  "fmt"
  "strings"

  "github.com/bsm/redeo/client"
)

func main() {
	pool, _ := client.New(&pool.Options{
		InitialSize: 1,
	}, nil)
	defer pool.Close()

	cn, _ := pool.Get()
	defer pool.Put(cn)

	// Build pipeline
	cn.WriteCmdString("PING")
	cn.WriteCmdString("ECHO", "HEllO")
	cn.WriteCmdString("GET", "key")
	cn.WriteCmdString("SET", "key", "value")
	cn.WriteCmdString("DEL", "key")

	// Flush pipeline to socket
	if err := cn.Flush(); err != nil {
		cn.MarkFailed()
		panic(err)
	}

	// Consume responses
	for i := 0; i < 5; i++ {
		t, err := cn.PeekType()
		if err != nil {
			return
		}

		switch t {
		case resp.TypeInline:
			s, _ := cn.ReadInlineString()
			fmt.Println(s)
		case resp.TypeBulk:
			s, _ := cn.ReadBulkString()
			fmt.Println(s)
		case resp.TypeInt:
			n, _ := cn.ReadInt()
			fmt.Println(n)
		case resp.TypeNil:
			_ = cn.ReadNil()
			fmt.Println(nil)
		default:
			panic("unexpected response type")
		}
	}

}

Documentation

Overview

Package client implements a minimalist client for working with redis servers.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	net.Conn
	// contains filtered or unexported fields
}

func (*Client) Del

func (c *Client) Del(key string) ([]byte, error)

func (*Client) Get

func (c *Client) Get(key string) ([]byte, error)

func (*Client) Prefix

func (c *Client) Prefix(key string) ([]byte, error)

func (*Client) PrefixOnlyKey

func (c *Client) PrefixOnlyKey(key string) ([]byte, error)

func (*Client) RecvData

func (c *Client) RecvData() ([]byte, error)

func (*Client) Set

func (c *Client) Set(key string, value []byte) ([]byte, error)

type Conn added in v1.0.0

type Conn interface {
	resp.ResponseParser

	// MarkFailed marks the connection as failed which
	// will force it to be closed instead of being returned to the pool
	MarkFailed()

	// WriteCmd writes a full command as part of a pipeline. To execute the pipeline,
	// you must call Flush.
	WriteCmd(cmd string, args ...[]byte)
	// WriteCmdString writes a full command as part of a pipeline. To execute the pipeline,
	// you must call Flush.
	WriteCmdString(cmd string, args ...string)
	// WriteMultiBulkSize is a low-level method to write a multibulk size.
	// For normal operation, use WriteCmd or WriteCmdString.
	WriteMultiBulkSize(n int) error
	// WriteBulk is a low-level method to write a bulk.
	// For normal operation, use WriteCmd or WriteCmdString.
	WriteBulk(b []byte)
	// WriteBulkString is a low-level method to write a bulk.
	// For normal operation, use WriteCmd or WriteCmdString.
	WriteBulkString(s string)
	// CopyBulk is a low-level method to copy a large bulk of data directly to the writer.
	// For normal operation, use WriteCmd or WriteCmdString.
	CopyBulk(src io.Reader, n int64) error
	// Flush flushes the output buffer. Call this after you have completed your pipeline
	Flush() error

	// SetDeadline sets the read and write deadlines associated
	// with the connection. It is equivalent to calling both
	// SetReadDeadline and SetWriteDeadline.
	SetDeadline(time.Time) error
	// SetReadDeadline sets the deadline for future Read calls
	// and any currently-blocked Read call.
	// A zero value for t means Read will not time out.
	SetReadDeadline(time.Time) error
	// SetWriteDeadline sets the deadline for future Write calls
	// and any currently-blocked Write call.
	// Even if write times out, it may return n > 0, indicating that
	// some of the data was successfully written.
	// A zero value for t means Write will not time out.
	SetWriteDeadline(time.Time) error

	// UnreadBytes returns the number of unread bytes.
	UnreadBytes() int
	// UnflushedBytes returns the number of pending/unflushed bytes.
	UnflushedBytes() int

	// Close (force) closes the connection.
	Close() error
	// contains filtered or unexported methods
}

Conn wraps a single network connection and exposes common read/write methods.

func Wrap added in v1.0.0

func Wrap(cn net.Conn) Conn

Wrap wraps a single network connection.

type Pool added in v1.0.0

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

Pool is a minimalist redis client connection pool

Example
package main

import (
	"fmt"
	"github.com/markusleevip/taodb/client"
	"github.com/markusleevip/taodb/resp"
)

func main() {
	pool, _ := client.New(nil)
	defer pool.Close()

	cn, _ := pool.Get()
	defer pool.Put(cn)

	// Build pipeline
	cn.WriteCmdString("PING")
	cn.WriteCmdString("ECHO", "HEllO")
	cn.WriteCmdString("GET", "key")
	cn.WriteCmdString("SET", "key", "value")
	cn.WriteCmdString("GET", "key")

	// Flush pipeline to socket
	if err := cn.Flush(); err != nil {
		cn.MarkFailed()
		panic(err)
	}

	// Consume responses
	for i := 0; i < 5; i++ {
		t, err := cn.PeekType()
		if err != nil {
			return
		}

		switch t {
		case resp.TypeInline:
			s, _ := cn.ReadInlineString()
			fmt.Println(s)
		case resp.TypeBulk:
			s, _ := cn.ReadBulkString()
			fmt.Println(s)
		case resp.TypeInt:
			n, _ := cn.ReadInt()
			fmt.Println(n)
		case resp.TypeNil:
			_ = cn.ReadNil()
			fmt.Println(nil)
		case resp.TypeError:
			err, _ := cn.ReadError()
			fmt.Println(err)
		default:
			panic("unexpected response type")
		}
	}

}
Output:

PONG
HEllO
ERR not found key: 'key'
ok
value

func New

func New(dialer func() (net.Conn, error)) (*Pool, error)

New initializes a new pool with a custom dialer

func (*Pool) Close added in v1.0.0

func (p *Pool) Close() error

Close closes the client and all underlying connections

func (*Pool) Get added in v1.0.0

func (p *Pool) Get() (Conn, error)

Get returns a connection

func (*Pool) Put added in v1.0.0

func (p *Pool) Put(cn Conn)

Put allows to return a connection back to the pool. Call this method after every call/pipeline. Do not use the connection again after this method is triggered.

Jump to

Keyboard shortcuts

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