memcache

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

README

Memcache Client in Go (golang) with auth

Installing

$ go get github.com/dev-lazarev/memcache

After this command memcache is ready to use. Its source will be in:

$GOPATH/src/github.com/dev-lazarev/memcache

You can use go get -u -a for update all installed packages.

Example

import (
        "github.com/dev-lazarev/memcache"
)

func main() {
     mc := memcache.New("User:Password@110.0.0.1:11211", "10.0.0.2:11211", "10.0.0.3:11212")
     mc.Set(&memcache.Item{Key: "foo", Value: []byte("my value")})

     it, err := mc.Get("foo")
     ...
}

About

This is a memcache client library for the Go programming language (http://golang.org/). This is a high performance fork of the original library at http://github.com/rainycape/memcache ( fork github.com/bradfitz/gomemcache). The following is a comparison between the original library and this one:

benchmark                               old ns/op    new ns/op    delta
BenchmarkSetGet                            214443       138200  -35.55%
BenchmarkSetGetLarge                       262164       146594  -44.08%
BenchmarkConcurrentSetGetSmall10_100     82561221     51282962  -37.88%
BenchmarkConcurrentSetGetLarge10_100     96067285     63887183  -33.50%
BenchmarkConcurrentSetGetSmall20_100    152834658     75607154  -50.53%
BenchmarkConcurrentSetGetLarge20_100    202574186     96010615  -52.60%

benchmark                                old MB/s     new MB/s  speedup
BenchmarkSetGet                              0.03         0.04    1.33x
BenchmarkSetGetLarge                         4.82         8.62    1.79x
BenchmarkConcurrentSetGetSmall10_100         0.07         0.12    1.71x
BenchmarkConcurrentSetGetLarge10_100        13.16        19.78    1.50x
BenchmarkConcurrentSetGetSmall20_100         0.08         0.16    2.00x
BenchmarkConcurrentSetGetLarge20_100        12.48        26.33    2.11x

benchmark                              old allocs   new allocs    delta
BenchmarkSetGet                                18            6  -66.67%
BenchmarkSetGetLarge                           19            6  -68.42%
BenchmarkConcurrentSetGetSmall10_100        58469         6199  -89.40%
BenchmarkConcurrentSetGetLarge10_100        59848         6196  -89.65%
BenchmarkConcurrentSetGetSmall20_100       117177        12432  -89.39%
BenchmarkConcurrentSetGetLarge20_100       120173        12413  -89.67%

benchmark                               old bytes    new bytes    delta
BenchmarkSetGet                              2479          170  -93.14%
BenchmarkSetGetLarge                         7537         1184  -84.29%
BenchmarkConcurrentSetGetSmall10_100      3101520       187245  -93.96%
BenchmarkConcurrentSetGetLarge10_100      8330341      1197783  -85.62%
BenchmarkConcurrentSetGetSmall20_100      6318072       374977  -94.07%
BenchmarkConcurrentSetGetLarge20_100     16884200      2398491  -85.79%

Documentation

Overview

Package memcache provides a client for the memcached cache server.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCacheMiss means that a Get failed because the item wasn't present.
	ErrCacheMiss = errors.New("memcache: cache miss")

	// ErrCASConflict means that a CompareAndSwap call failed due to the
	// cached value being modified between the Get and the CompareAndSwap.
	// If the cached value was simply evicted rather than replaced,
	// ErrNotStored will be returned instead.
	ErrCASConflict = errors.New("memcache: compare-and-swap conflict")

	// ErrNotStored means that a conditional write operation (i.e. Add or
	// CompareAndSwap) failed because the condition was not satisfied.
	ErrNotStored = errors.New("memcache: item not stored")

	// ErrServerError ErrServer means that a server error occurred.
	ErrServerError = errors.New("memcache: server error")

	// ErrNoStats means that no statistics were available.
	ErrNoStats = errors.New("memcache: no statistics available")

	// ErrMalformedKey is returned when an invalid key is used.
	// Keys must be at maximum 250 bytes long, ASCII, and not
	// contain whitespace or control characters.
	ErrMalformedKey = errors.New("malformed: key is too long or contains invalid characters")

	// ErrNoServers is returned when no servers are configured or available.
	ErrNoServers = errors.New("memcache: no servers configured or available")

	// ErrBadMagic is returned when the magic number in a response is not valid.
	ErrBadMagic = errors.New("memcache: bad magic number in response")

	// ErrBadIncrDec is returned when performing a incr/decr on non-numeric values.
	ErrBadIncrDec = errors.New("memcache: incr or decr on non-numeric value")
)

Functions

This section is empty.

Types

type Client

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

Client is a memcache client. It is safe for unlocked use by multiple concurrent goroutines.

func New

func New(config []Config) (*Client, error)

New returns a memcache client using the provided server(s) with equal weight. If a server is listed multiple times, it gets a proportional amount of weight.

func NewFromServers

func NewFromServers(servers *ServerList) *Client

NewFromServers returns a new Client using the provided Servers.

func (*Client) Add

func (c *Client) Add(item *Item) error

Add writes the given item, if no value already exists for its key. ErrNotStored is returned if that condition is not met.

func (*Client) Close

func (c *Client) Close()

Close closes all currently open connections.

func (*Client) CompareAndSwap

func (c *Client) CompareAndSwap(item *Item) error

CompareAndSwap writes the given item that was previously returned by Get, if the value was neither modified or evicted between the Get and the CompareAndSwap calls. The item's Key should not change between calls but all other item fields may differ. ErrCASConflict is returned if the value was modified in between the calls. ErrNotStored is returned if the value was evicted in between the calls.

func (*Client) Decrement

func (c *Client) Decrement(key string, delta uint64) (newValue uint64, err error)

Decrement atomically decrements key by delta. The return value is the new value after being decremented or an error. If the value didn't exist in memcached the error is ErrCacheMiss. The value in memcached must be an decimal number, or an error will be returned. On underflow, the new value is capped at zero and does not wrap around.

func (*Client) Delete

func (c *Client) Delete(key string) error

Delete deletes the item with the provided key. The error ErrCacheMiss is returned if the item didn't already exist in the cache.

func (*Client) Flush

func (c *Client) Flush(expiration int) error

Flush removes all the items in the cache after expiration seconds. If expiration is <= 0, it removes all the items right now.

func (*Client) Get

func (c *Client) Get(key string) (*Item, error)

Get gets the item for the given key. ErrCacheMiss is returned for a memcache cache miss. The key must be at most 250 bytes in length.

func (*Client) GetMulti

func (c *Client) GetMulti(keys []string) (map[string]*Item, error)

GetMulti is a batch version of Get. The returned map from keys to items may have fewer elements than the input slice, due to memcache cache misses. Each key must be at most 250 bytes in length. If no error is returned, the returned map will also be non-nil.

func (*Client) Increment

func (c *Client) Increment(key string, delta uint64) (newValue uint64, err error)

Increment atomically increments key by delta. The return value is the new value after being incremented or an error. If the value didn't exist in memcached the error is ErrCacheMiss. The value in memcached must be an decimal number, or an error will be returned. On 64-bit overflow, the new value wraps around.

func (*Client) Set

func (c *Client) Set(item *Item) error

Set writes the given item, unconditionally.

type Config added in v0.0.2

type Config struct {
	Server   string
	User     string
	Password string

	//The minimum number of connections to have in the connection pool
	InitialCap int

	//Maximum number of concurrent live connections
	MaxCap int

	//Max idle connections
	MaxIdle int

	//The maximum idle time of the connection, if it exceeds this event, it will be invalid
	IdleTimeout time.Duration

	ConnectionTimeout time.Duration
}

type Item

type Item 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
	// contains filtered or unexported fields
}

Item is an item to be got or stored in a memcached server.

type ServerList

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

ServerList is an implementation of the Servers interface. To initialize a ServerList use NewServerList.

func NewServerList

func NewServerList(configs []Config) (*ServerList, error)

func (*ServerList) CloseConnection added in v0.0.2

func (s *ServerList) CloseConnection(index uint32, conn net.Conn) error

func (*ServerList) Count added in v0.0.2

func (s *ServerList) Count() int

func (*ServerList) GetConnection added in v0.0.2

func (s *ServerList) GetConnection(index uint32) (net.Conn, error)

func (*ServerList) Name added in v0.0.2

func (s *ServerList) Name(index uint32) string

func (*ServerList) PickServerIndex added in v0.0.2

func (s *ServerList) PickServerIndex(key string) (uint32, error)

func (*ServerList) PoolLen added in v0.0.2

func (s *ServerList) PoolLen() uint32

func (*ServerList) PutConnection added in v0.0.2

func (s *ServerList) PutConnection(index uint32, conn net.Conn) error

func (*ServerList) Release added in v0.0.2

func (s *ServerList) Release()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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