memcache

package
v0.0.0-...-cd6a66d Latest Latest
Warning

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

Go to latest
Published: Feb 29, 2024 License: Apache-2.0 Imports: 12 Imported by: 4

Documentation

Overview

Package memcache provides a client for the memcached cache server.

Index

Constants

View Source
const (
	// DefaultTimeout is the default socket read/write timeout.
	DefaultTimeout = 100 * time.Millisecond

	// DefaultMaxIdleConns is the default maximum number of idle connections
	// kept for any single address.
	DefaultMaxIdleConns = 2
)

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 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 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")
)

Functions

This section is empty.

Types

type Allocator

type Allocator interface {
	// Get returns a byte slice with at least sz capacity. Length of the slice is
	// not guaranteed and so must be asserted by callers (the Client).
	Get(sz int) *[]byte
	// Put returns the byte slice to the underlying allocator. The Client will
	// only call this method during error handling when allocated values are not
	// returned to the caller as cache results.
	Put(b *[]byte)
}

Allocator allows memory for memcached result values (Item.Value) to be managed by callers of the Client instead of by the Client itself. For example, this can be used by callers to implement arena-style memory management. The default implementation used, when not otherwise overridden, uses `make` and relies on GC for cleanup.

type Client

type Client struct {
	// DialTimeout specifies a custom dialer used to dial new connections to a server.
	DialTimeout func(network, address string, timeout time.Duration) (net.Conn, error)

	// Timeout specifies the socket read/write timeout.
	// If zero, DefaultTimeout is used.
	Timeout time.Duration

	// ConnectTimeout specifies the timeout for new connections.
	// If zero, DefaultTimeout is used.
	ConnectTimeout time.Duration

	// MinIdleConnsHeadroomPercentage specifies the percentage of minimum number of idle connections
	// that should be kept open, compared to the number of free but recently used connections.
	// If there are idle connections but none of them has been recently used, then all idle
	// connections get closed.
	//
	// If the configured value is negative, idle connections are never closed.
	MinIdleConnsHeadroomPercentage float64

	// MaxIdleConns specifies the maximum number of idle connections that will
	// be maintained per address. If less than one, DefaultMaxIdleConns will be
	// used.
	//
	// Consider your expected traffic rates and latency carefully. This should
	// be set to a number higher than your peak parallel requests.
	MaxIdleConns int

	// WriteBufferSizeBytes specifies the size of the write buffer (in bytes). The buffer
	// is allocated for each connection. If <= 0, the default value of 4KB will be used.
	WriteBufferSizeBytes int

	// ReadBufferSizeBytes specifies the size of the read buffer (in bytes). The buffer
	// is allocated for each connection. If <= 0, the default value of 4KB will be used.
	ReadBufferSizeBytes int
	// contains filtered or unexported fields
}

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

func New

func New(server ...string) *Client

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 NewFromSelector

func NewFromSelector(ss ServerSelector) *Client

NewFromSelector returns a new Client using the provided ServerSelector.

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()

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) DeleteAll

func (c *Client) DeleteAll() error

DeleteAll deletes all items in the cache.

func (*Client) FlushAll

func (c *Client) FlushAll() error

func (*Client) Get

func (c *Client) Get(key string, opts ...Option) (item *Item, err 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, opts ...Option) (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) Ping

func (c *Client) Ping() error

Ping checks all instances if they are alive. Returns error if any of them is down.

func (*Client) Replace

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

Replace writes the given item, but only if the server *does* already hold data for this key

func (*Client) Set

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

Set writes the given item, unconditionally.

func (*Client) Touch

func (c *Client) Touch(key string, seconds int32) (err error)

Touch updates the expiry for the given key. The seconds parameter is either a Unix timestamp or, if seconds is less than 1 month, the number of seconds into the future at which time the item will expire. Zero means the item has no expiration time. ErrCacheMiss is returned if the key is not in the cache. The key must be at most 250 bytes in length.

type ConnectTimeoutError

type ConnectTimeoutError struct {
	Addr net.Addr
}

ConnectTimeoutError is the error type used when it takes too long to connect to the desired host. This level of detail can generally be ignored.

func (*ConnectTimeoutError) Error

func (cte *ConnectTimeoutError) Error() string

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 Option

type Option func(opts *Options)

Option is a callback used to modify the Options that a particular Client method uses.

func WithAllocator

func WithAllocator(alloc Allocator) Option

WithAllocator creates a new Option that makes use of a specific memory Allocator for result values (Item.Value) loaded from memcached.

type Options

type Options struct {
	Alloc Allocator
}

Options are used to modify the behavior of an individual Get or GetMulti call made by the Client. They are constructed by applying Option callbacks passed to a Client method to a default Options instance.

type ServerList

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

ServerList is a simple ServerSelector. Its zero value is usable.

func (*ServerList) Each

func (ss *ServerList) Each(f func(net.Addr) error) error

Each iterates over each server calling the given function

func (*ServerList) PickServer

func (ss *ServerList) PickServer(key string) (net.Addr, error)

func (*ServerList) SetServers

func (ss *ServerList) SetServers(servers ...string) error

SetServers changes a ServerList's set of servers at runtime and is safe for concurrent use by multiple goroutines.

Each server is given equal weight. A server is given more weight if it's listed multiple times.

SetServers returns an error if any of the server names fail to resolve. No attempt is made to connect to the server. If any error is returned, no changes are made to the ServerList.

type ServerSelector

type ServerSelector interface {
	// PickServer returns the server address that a given item
	// should be shared onto.
	PickServer(key string) (net.Addr, error)
	Each(func(net.Addr) error) error
}

ServerSelector is the interface that selects a memcache server as a function of the item's key.

All ServerSelector implementations must be safe for concurrent use by multiple goroutines.

Jump to

Keyboard shortcuts

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