memcache

package
v0.0.0-...-61472b1 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound not found
	ErrNotFound = errors.New("gomemcache: key not found")
	// ErrExists exists
	ErrExists = errors.New("gomemcache: key exists")
	// ErrNotStored not stored
	ErrNotStored = errors.New("gomemcache: key not stored")

	// ErrPoolExhausted is returned from a pool connection method (Store, Get,
	// Delete, IncrDecr, Err) when the maximum number of database connections
	// in the pool has been reached.
	ErrPoolExhausted = errors.New("gomemcache: connection pool exhausted")
	// ErrPoolClosed pool closed
	ErrPoolClosed = errors.New("gomemcache: connection pool closed")
	// ErrConnClosed conn closed
	ErrConnClosed = errors.New("gomemcache: connection closed")
)

Functions

This section is empty.

Types

type Conn

type Conn interface {
	// Close closes the connection.
	Close() error

	// Err returns a non-nil value if the connection is broken. The returned
	// value is either the first non-nil value returned from the underlying
	// network connection or a protocol parsing error. Applications should
	// close broken connections.
	Err() error

	// Store sends a command to the server for store data.
	// cmd: set, add, replace, append, prepend, cas
	Store(cmd, key string, value []byte, flags uint32, timeout int32, cas uint64) error

	// Get sends a command to the server for gets data.
	// cmd: get, gets
	Get(cmd string, key string) (*Reply, error)

	// Gets sends a command to the server for gets data.
	// cmd: get, gets
	Gets(cmd string, keys ...string) ([]*Reply, error)

	// Touch update the expiration time on an existing key.
	Touch(key string, timeout int32) error

	// Store sends a command to the server for delete data.
	Delete(key string) (err error)

	// IncrDecr sends a command to the server for incr/decr data.
	// cmd: incr, decr
	IncrDecr(cmd string, key string, delta uint64) (uint64, error)
}

Conn represents a connection to a Memcache server. Command Reference: https://github.com/memcached/memcached/wiki/Commands

func Dial

func Dial(network, address string, options ...DialOption) (Conn, error)

Dial connects to the Memcache server at the given network and address using the specified options.

func NewConn

func NewConn(netConn net.Conn, readTimeout, writeTimeout time.Duration) Conn

NewConn returns a new gomemcache connection for the given net connection.

type DialOption

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

DialOption specifies an option for dialing a Memcache server.

func DialConnectTimeout

func DialConnectTimeout(d time.Duration) DialOption

DialConnectTimeout specifies the timeout for connecting to the Memcache server.

func DialNetDial

func DialNetDial(dial func(network, addr string) (net.Conn, error)) DialOption

DialNetDial specifies a custom dial function for creating TCP connections. If this option is left out, then net.Dial is used. DialNetDial overrides DialConnectTimeout.

func DialReadTimeout

func DialReadTimeout(d time.Duration) DialOption

DialReadTimeout specifies the timeout for reading a single command reply.

func DialWriteTimeout

func DialWriteTimeout(d time.Duration) DialOption

DialWriteTimeout specifies the timeout for writing a single command.

type Error

type Error string

Error represents an error returned in a command reply.

func (Error) Error

func (err Error) Error() string

type Pool

type Pool struct {

	// Dial is an application supplied function for creating and configuring a
	// connection.
	//
	// The connection returned from Dial must not be in a special state
	// (subscribed to pubsub channel, transaction started, ...).
	Dial func() (Conn, error)

	// TestOnBorrow is an optional application supplied function for checking
	// the health of an idle connection before the connection is used again by
	// the application. Argument t is the time that the connection was returned
	// to the pool. If the function returns an error, then the connection is
	// closed.
	TestOnBorrow func(c Conn, t time.Time) error

	// Maximum number of idle connections in the pool.
	MaxIdle int

	// Maximum number of connections allocated by the pool at a given time.
	// When zero, there is no limit on the number of connections in the pool.
	MaxActive int

	// Close connections after remaining idle for this duration. If the value
	// is zero, then idle connections are not closed. Applications should set
	// the timeout to a value less than the server's timeout.
	IdleTimeout time.Duration

	// If Wait is true and the pool is at the MaxActive limit, then Get() waits
	// for a connection to be returned to the pool before returning.
	Wait bool
	// contains filtered or unexported fields
}

Pool maintains a pool of connections. The application calls the Get method to get a connection from the pool and the connection's Close method to return the connection's resources to the pool.

The following example shows how to use a pool in a web application. The application creates a pool at application startup and makes it available to request handlers using a global variable.

func newPool(server string) *memcache.Pool {
    return &memcache.Pool{
        MaxIdle: 3,
        IdleTimeout: 240 * time.Second,
        Dial: func () (memcache.Conn, error) {
            c, err := memcache.Dial("tcp", server)
            if err != nil {
                return nil, err
            }
            return c, err
        },
        TestOnBorrow: func(c memcache.Conn, t time.Time) error {
            _, err := c.Get("get", "PING")
            return err
        },
    }
}

var (
    pool *memcache.Pool
    memcacheServer = flag.String("memcacheServer", ":11211", "")
)

func main() {
    flag.Parse()
    pool = newPool(*memcacheServer)
    ...
}

A request handler gets a connection from the pool and closes the connection when the handler is done:

func serveHome(w http.ResponseWriter, r *http.Request) {
    conn := pool.Get()
    defer conn.Close()
    ....
}

func NewPool deprecated

func NewPool(newFn func() (Conn, error), maxIdle int) *Pool

NewPool creates a new pool.

Deprecated: Initialize the Pool directory as shown in the example.

func (*Pool) ActiveCount

func (p *Pool) ActiveCount() int

ActiveCount returns the number of active connections in the pool.

func (*Pool) Close

func (p *Pool) Close() error

Close releases the resources used by the pool.

func (*Pool) Get

func (p *Pool) Get() Conn

Get gets a connection. The application must close the returned connection. This method always returns a valid connection so that applications can defer error handling to the first use of the connection. If there is an error getting an underlying connection, then the connection Err, Do, Send, Flush and Receive methods return that error.

type Reply

type Reply struct {
	// Key is the Item's key (250 bytes maximum).
	Key string

	// Value is the Item's value.
	Value []byte

	// Flags are server-opaque flags whose semantics are entirely
	// up to the app.
	Flags uint32

	// Expiration is the cache expiration time, in seconds: either a relative
	// time from now (up to 1 month), or an absolute Unix epoch time.
	// Zero means the Item has no expiration time.
	Expiration int32

	// Compare and swap ID.
	Cas uint64
}

Reply is an reply to be got or stored in a memcached server.

Jump to

Keyboard shortcuts

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