memcached

package module
v1.5.4 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2019 License: MIT Imports: 12 Imported by: 0

README

go-memcached

What's this?

go-memcached is the client library of memcached for Go.

Install

go get github.com/jumpeiMano/go-memcached

Example

package main

import (
  gm "github.com/jumpeiMano/go-memcached"
)

func main() {
  cl := gm.New(
    gm.Servers{
      {Host: "localhost", Port: 11211, Alias: "mem1"},
    }, "prefix",
  )
  defer cl.Close()
  items, err := cl.Get("key1", "key2")
  if err != nil {
    log.Fatalf("Failed Get: %+v", err)
  }
  keyValueMap := make(map[string]string, len(items))
  for _, item := range items {
    keyValueMap[item.Key] = string(item.Value)
  }
  fmt.Println(keyValueMap)
}

Configure

SetConnMaxOpen

SetConnMaxOpen sets the maximum amount of opening connections. Default is 0 (unlimited).

SetConnMaxLifetime

SetConnMaxLifetime sets the maximum amount of time a connection may be reused. Expired connections may be closed lazily before reuse. If d <= 0, connections are reused forever. Default is 0.

SetConnectTimeout

SetConnectTimeout sets the timeout of connect to memcached server. Default is 1 second.

SetPollTimeout

SetPollTimeout sets the timeout of polling from memcached server. Default is 1 second.

SetFailover

SetFailover is used to specify whether to use the failover option. Default is false.

SetAliveCheckPeriod

SetAliveCheckPeriod sets the period of connection's alive check to memcached server. Default is 10 seconds. It will work only when the failover is true.

SetNoreply

SetNoreply is used to specify whether to use the noreply option. Default is false.

Testing

$ make test

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNonexistentCommand = errors.New("nonexiststent command error")
	ErrClient             = errors.New("client error")
	ErrServer             = errors.New("server error")
	ErrOverMaxKeyLength   = errors.New("key's length is too long")
	ErrCanceldByContext   = errors.New("canceled by context")
)

errors

View Source
var (
	ErrMemcachedClosed = errors.New("memcached is closed")
	ErrDeadNode        = errors.New("dead node")
	ErrConnect         = errors.New("connect error")
	ErrBadConn         = errors.New("bad conn")
	ErrNotFound        = errors.New("not found")
)

Error

Functions

This section is empty.

Types

type Client added in v1.5.0

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

Client is the client of go-memcached.

func New

func New(servers Servers, prefix string) (cl *Client)

New create Client

func (*Client) Add added in v1.5.0

func (cl *Client) Add(noreply bool, items ...*Item) (failedKeys []string, err error)

Add store the value only if it does not already exist.

func (*Client) Append added in v1.5.0

func (cl *Client) Append(noreply bool, items ...*Item) (failedKeys []string, err error)

Append appends the value after the last bytes in an existing item.

func (*Client) Cas added in v1.5.0

func (cl *Client) Cas(noreply bool, items ...*Item) (failedKeys []string, err error)

Cas stores the value only if no one else has updated the data since you read it last.

func (*Client) Close added in v1.5.0

func (cl *Client) Close() error

Close closes all connectionPools and channels

func (*Client) Delete added in v1.5.0

func (cl *Client) Delete(noreply bool, keys ...string) (failedKeys []string, err error)

Delete delete the value for the specified cache key.

func (*Client) FlushAll added in v1.5.0

func (cl *Client) FlushAll() error

FlushAll purges the entire cache.

func (*Client) Gat added in v1.5.0

func (cl *Client) Gat(exp int64, keys ...string) (results []*Item, err error)

Gat is used to fetch items and update the expiration time of an existing items.

func (*Client) GatOrSet added in v1.5.0

func (cl *Client) GatOrSet(key string, exp int64, cb func(key string) (*Item, error)) (*Item, error)

GatOrSet gets from memcached via `gat`, and if no hit, Set value gotten by callback, and return the value

func (*Client) GatOrSetMulti added in v1.5.0

func (cl *Client) GatOrSetMulti(keys []string, exp int64, cb func(keys []string) ([]*Item, error)) ([]*Item, error)

GatOrSetMulti gets from memcached via `gat`, and if no hit, Set value gotten by callback, and return the value

func (*Client) Gats added in v1.5.0

func (cl *Client) Gats(exp int64, keys ...string) (results []*Item, err error)

Gats is used to fetch items and update the expiration time of an existing items.

func (*Client) Get added in v1.5.0

func (cl *Client) Get(keys ...string) (results []*Item, err error)

Get returns cached data for given keys.

func (*Client) GetOrSet added in v1.5.0

func (cl *Client) GetOrSet(key string, cb func(key string) (*Item, error)) (*Item, error)

GetOrSet gets from memcached, and if no hit, Set value gotten by callback, and return the value

func (*Client) GetOrSetMulti added in v1.5.0

func (cl *Client) GetOrSetMulti(keys []string, cb func(keys []string) ([]*Item, error)) ([]*Item, error)

GetOrSetMulti gets from memcached, and if no hit, Set value gotten by callback, and return the value

func (*Client) Gets added in v1.5.0

func (cl *Client) Gets(keys ...string) (results []*Item, err error)

Gets returns cached data for given keys, it is an alternative Get api for using with CAS. Gets returns a CAS identifier with the item. If the item's CAS value has changed since you Gets'ed it, it will not be stored.

func (*Client) Prepend added in v1.5.0

func (cl *Client) Prepend(noreply bool, items ...*Item) (failedKeys []string, err error)

Prepend prepends the value before existing value.

func (*Client) Replace added in v1.5.0

func (cl *Client) Replace(noreply bool, items ...*Item) (failedKeys []string, err error)

Replace replaces the value, only if the value already exists, for the specified cache key.

func (*Client) Set added in v1.5.0

func (cl *Client) Set(noreply bool, items ...*Item) (failedKeys []string, err error)

Set set the value with specified cache key.

func (*Client) SetConnMaxLifetime added in v1.5.0

func (cl *Client) SetConnMaxLifetime(d time.Duration)

SetConnMaxLifetime sets the maximum amount of time a connection may be reused.

Expired connections may be closed lazily before reuse.

If d <= 0, connections are reused forever.

func (*Client) SetConnMaxOpen added in v1.5.0

func (cl *Client) SetConnMaxOpen(maxOpen int)

SetConnMaxOpen sets the maximum amount of opening connections.

func (*Client) SetConnectTimeout added in v1.5.0

func (cl *Client) SetConnectTimeout(timeout time.Duration)

SetConnectTimeout sets the timeout of connect to memcached server.

func (*Client) SetFailover added in v1.5.0

func (cl *Client) SetFailover(failover bool)

SetFailover is used to specify whether to use the failover option.

func (*Client) SetKeepAlivePeriod added in v1.5.0

func (cl *Client) SetKeepAlivePeriod(period time.Duration)

SetKeepAlivePeriod sets the period of keep alive.

func (*Client) SetLogger added in v1.5.0

func (cl *Client) SetLogger(logf func(format string, params ...interface{}))

SetLogger is used to set logger

func (*Client) SetMaxErrorCount added in v1.5.0

func (cl *Client) SetMaxErrorCount(count int64)

SetMaxErrorCount sets the max of error count to close the connection pool.

func (*Client) SetPollTimeout added in v1.5.0

func (cl *Client) SetPollTimeout(timeout time.Duration)

SetPollTimeout sets the timeout of polling from memcached server.

func (*Client) SetTryReconnectPeriod added in v1.5.0

func (cl *Client) SetTryReconnectPeriod(period time.Duration)

SetTryReconnectPeriod sets the period of trying reconnect.

func (*Client) Stats added in v1.5.0

func (cl *Client) Stats(argument string) (resultMap map[string][]byte, err error)

Stats returns a list of basic stats.

func (*Client) Touch added in v1.5.0

func (cl *Client) Touch(key string, exp int64, noreply bool) error

Touch is used to update the expiration time of an existing item without fetching it.

type Item

type Item struct {
	Key   string
	Value []byte
	Flags uint16
	Cas   uint64
	Exp   int64
}

Item gives the cached data.

type Server

type Server struct {
	Host  string
	Port  int
	Alias string
}

Server is the server's info of memcahced.

type Servers

type Servers []Server

Servers are slice of Server.

Jump to

Keyboard shortcuts

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