Help us shape the future of Go! Take the 2026 Go Developer Survey

protocol

package module
v0.0.0-...-8cdae60 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2016 License: MIT Imports: 6 Imported by: 1

README

redis-protocol

Redis protocol parser in golang

Document

Getting Started

package main

import (
    "bufio"
    "fmt"
    "github.com/quorzz/redis-protocol"
    "net"
    "time"
)

// Just for example, ignore all the possible errors
func main() {

    conn, _ := net.DialTimeout("tcp", "127.0.0.1:6379", time.Duration(10)*time.Second)

    raw, _ := protocol.PackCommand("hmset", "users:1", "name", "lily")
    conn.Write(raw)

    reader := bufio.NewReader(conn)
    message, _ := protocol.UnpackFromReader(reader)

    fmt.Println(message.String()) // OK <nil>
    fmt.Println(message.Bool())   // true <nil>



    // There is also anthor usage more flexibily :

    protocol.NewWriter(conn).WriteCommand("hgetall", "users:1")

    msg, _ := protocol.NewReader(conn).ReadMessage()

    fmt.Println(msg.StringMap()) // map[name:lily] <nil>

    fmt.Println(msg.Strings()) // ["name", "lily"] <nil>



    // If you want to use slice or map as parameters :

    args := protocol.NormalizeArgs(map[string]string{"name": "bob"})

    protocol.PackCommand("hmset", "users:1", args)


    args2 := protocol.NormalizeArgs("lpush", "list:users", []string{"no.1", "no.2", "no,3"})

    protocol.NewWriter(conn).WriteCommand(args2)

}

Some useful methods of protocol.Message:

  • Message.Bool()
  • Message.Int()
  • Messsage.Int64()
  • Message.String()
  • Message.Strings()
  • Message.Bytes()
  • Message.StringMap()
  • Some more soon ......

License

The MIT License (MIT) Copyright (c) 2016 quorzz

Documentation

Index

Constants

View Source
const (
	MessageError  = '-'
	MessageStatus = '+'
	MessageInt    = ':'
	MessageBulk   = '$'
	MessageMutli  = '*'
)

Variables

This section is empty.

Functions

func NormalizeArgs

func NormalizeArgs(args ...interface{}) []interface{}

func PackCommand

func PackCommand(args ...interface{}) ([]byte, error)

Types

type Message

type Message struct {
	Type    byte
	Error   error
	Status  string
	Integer int64
	Bulk    []byte
	Multi   []*Message
}

func UnpackFromReader

func UnpackFromReader(r *bufio.Reader) (*Message, error)

func (*Message) Bool

func (Message *Message) Bool() (bool, error)

func (*Message) Bytes

func (Message *Message) Bytes() ([]byte, error)

func (*Message) HasError

func (Message *Message) HasError() bool

func (*Message) Int

func (Message *Message) Int() (int, error)

func (*Message) Int64

func (Message *Message) Int64() (int64, error)

func (*Message) String

func (Message *Message) String() (string, error)

func (*Message) StringMap

func (Message *Message) StringMap() (map[string]string, error)

func (*Message) Strings

func (Message *Message) Strings() ([]string, error)

type Reader

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

func NewReader

func NewReader(r io.Reader) *Reader

func (*Reader) ReadMessage

func (r *Reader) ReadMessage() (*Message, error)

type Writer

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

func NewWriter

func NewWriter(w io.Writer) *Writer

func (*Writer) WriteCommand

func (w *Writer) WriteCommand(args ...interface{}) error

Jump to

Keyboard shortcuts

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