mc

package module
v0.0.0-...-51c9346 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2020 License: MIT Imports: 13 Imported by: 1

README

gomemcached

Memcached protocol implementation for memcached server. You can use it to create a memcached server easily.

GoDoc travis Go Report Card Coverage Status

import this lib:

go get -u github.com/rpcxio/gomemcached

And register handlers.

    addr = "127.0.0.1:" + strconv.Itoa(port)
	mockServer = NewServer(addr)
	mockServer.RegisterFunc("get", DefaultGet)
	mockServer.RegisterFunc("gets", DefaultGet)
	mockServer.RegisterFunc("set", DefaultSet)
	mockServer.RegisterFunc("delete", DefaultDelete)
	mockServer.RegisterFunc("incr", DefaultIncr)
	mockServer.RegisterFunc("flush_all", DefaultFlushAll)
	mockServer.RegisterFunc("version", DefaultVersion)
	mockServer.Start()

This project refers to the below projects:

I added more implementation and fix some issues, for example, panic on reading long value. I also add Context in handlers so that we can pass more info to handlers.

Documentation

Overview

Package mc implements memcached text protocol: https://github.com/memcached/memcached/blob/master/doc/protocol.txt. binary protocol () has not been implemented.

Index

Constants

View Source
const (
	// ReaderBuffsize is used for bufio reader.
	ReaderBuffsize = 16 * 1024
	// WriterBuffsize is used for bufio writer.
	WriterBuffsize = 16 * 1024
)
View Source
const RealtimeMaxDelta = 60 * 60 * 24 * 30

RealtimeMaxDelta is max delta time.

Variables

View Source
var (
	RespOK        = "OK"
	RespEnd       = "END"
	RespStored    = "STORED"
	RespNotStored = "NOT_STORED"
	RespExists    = "EXISTS"
	RespDeleted   = "DELETED"
	RespTouched   = "TOUCHED"
	RespNotFound  = "NOT_FOUND"
	RespErr       = "ERROR "
	RespClientErr = "CLIENT_ERROR "
	RespServerErr = "SERVER_ERROR "
)

Functions

This section is empty.

Types

type Error

type Error struct {
	Description string
}

Error is memcached protocol error.

func NewError

func NewError(description string) Error

NewError creates a new error.

func (Error) Error

func (e Error) Error() string

type HandlerFunc

type HandlerFunc func(ctx context.Context, req *Request, res *Response) error

HandlerFunc is a function to handle a request and returns a response.

type RemoteConnKey

type RemoteConnKey struct{}

RemoteConnKey is used as key in context.

type Request

type Request struct {
	// Command is memcached command name, see https://github.com/memcached/memcached/wiki/Commands
	Command string
	Key     string
	Keys    []string
	Flags   string
	Exptime int64 //in second
	Data    []byte
	Value   int64
	Cas     string
	Noreply bool
}

Request is a generic memcached request. Some fields are meaningless for some special commands and they are zero values. Exptime will always be 0 or epoch (in seconds)

func ReadRequest

func ReadRequest(r *bufio.Reader) (req *Request, err error)

ReadRequest reads a request from reader

type Response

type Response struct {
	Response string
	Values   []Value
}

Response is a memcached response.

func (Response) String

func (r Response) String() string

String converts Response to string to send over wire.

type Server

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

Server implements memcached server.

func NewServer

func NewServer(addr string) *Server

NewServer creates a memcached server.

func (*Server) RegisterFunc

func (s *Server) RegisterFunc(cmd string, fn HandlerFunc) error

RegisterFunc registers a handler to handle this command.

func (*Server) Serve

func (s *Server) Serve(ln net.Listener) error

Serve accepts incoming connections on the Listener ln, creating a new service goroutine for each. The service goroutines read requests and then call registered handlers to reply to them.

func (*Server) Start

func (s *Server) Start() error

Start starts a memcached server in a goroutine. It listens on the TCP network address s.Addr and then calls Serve to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.

func (*Server) Stop

func (s *Server) Stop() error

Stop stops this memcached sever.

type Value

type Value struct {
	Key, Flags string
	//Exptime time.Time
	Data []byte
	Cas  string
}

Value is data in responses.

Jump to

Keyboard shortcuts

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