Documentation ¶
Overview ¶
Package memcache provides a client for the memcached cache server.
Index ¶
- Constants
- Variables
- type Client
- func New(server []string, opts ...ClientOption) *Client
- func NewDiscoveryClient(discoveryAddress string, pollingDuration time.Duration, opts ...ClientOption) (*Client, error)
- func NewDynamicRendezvousClient(discoveryAddress string, pollingDuration time.Duration, opts ...ClientOption) (*Client, error)
- func NewFromSelector(ss ServerSelector, opts ...ClientOption) *Client
- func (c *Client) Add(item *Item) error
- func (c *Client) Append(item *Item) error
- func (c *Client) CompareAndSwap(item *Item) error
- func (c *Client) Decrement(key string, delta uint64) (newValue uint64, err error)
- func (c *Client) Delete(key string) error
- func (c *Client) DeleteAll() error
- func (c *Client) FlushAll() error
- func (c *Client) Get(key string) (item *Item, err error)
- func (c *Client) GetConfig(configType string) (clusterConfig *ClusterConfig, err error)
- func (c *Client) GetMulti(keys []string) (map[string]*Item, error)
- func (c *Client) Increment(key string, delta uint64) (newValue uint64, err error)
- func (c *Client) Ping() error
- func (c *Client) Prepend(item *Item) error
- func (c *Client) Replace(item *Item) error
- func (c *Client) Set(item *Item) error
- func (c *Client) Touch(key string, seconds int32) (err error)
- type ClientOption
- type ClusterConfig
- type ClusterNode
- type ClusterPool
- type Conn
- type ConnectTimeoutError
- type Item
- type Pool
- type PoolConfig
- type PoolStats
- type RendezvousSelector
- type ServerList
- type ServerSelector
Constants ¶
const ( DefaultMaxConns = 10 DefaultIdleLifetime = 5 * time.Minute DefaultIdleClosePeriod = 1 * time.Minute )
const ( // DefaultTimeout is the default socket read/write timeout. DefaultTimeout = 500 * time.Millisecond )
Variables ¶
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") // 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 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") // ErrInvalidPollingDuration is returned when discovery polling is invalid ErrInvalidPollingDuration = errors.New("Discovery polling duration is invalid") // ErrClusterConfigMiss means that GetConfig failed as cluster config was not present ErrClusterConfigMiss = errors.New("memcache: cluster config miss") )
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct { // Timeout specifies the socket read/write timeout. // If zero, DefaultTimeout is used. Timeout time.Duration // ConnectionTimeout specifies the timeout for establishing a connection. // If zero, DefaultTimeout is used. ConnectionTimeout time.Duration Pool *ClusterPool // 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, opts ...ClientOption) *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 NewDiscoveryClient ¶
func NewDiscoveryClient(discoveryAddress string, pollingDuration time.Duration, opts ...ClientOption) (*Client, error)
NewDiscoveryClient returns a discovery config enabled client which polls periodically for new information and update server list if new information is found. All the servers which are found are used with equal weight. discoveryAddress should be in following form "ipv4-address:port" Note: pollingDuration should be at least 1 second.
func NewDynamicRendezvousClient ¶ added in v0.3.0
func NewFromSelector ¶
func NewFromSelector(ss ServerSelector, opts ...ClientOption) *Client
NewFromSelector returns a new Client using the provided ServerSelector.
func (*Client) Add ¶
Add writes the given item, if no value already exists for its key. ErrNotStored is returned if that condition is not met.
func (*Client) Append ¶ added in v0.5.0
Append appends the given item to the existing item, if a value already exists for its key. ErrNotStored is returned if that condition is not met.
func (*Client) CompareAndSwap ¶
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 ¶
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 ¶
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) Get ¶
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) GetConfig ¶
func (c *Client) GetConfig(configType string) (clusterConfig *ClusterConfig, err error)
GetConfig gets the config type. ErrClusterConfigMiss is returned if config for the type cluster is not found. The type must be at most 250 bytes in length.
func (*Client) GetMulti ¶
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 ¶
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 ¶
Ping checks all instances if they are alive. Returns error if any of them is down.
func (*Client) Prepend ¶ added in v0.5.0
Prepend prepends the given item to the existing item, if a value already exists for its key. ErrNotStored is returned if that condition is not met.
func (*Client) Replace ¶
Replace writes the given item, but only if the server *does* already hold data for this key
func (*Client) Touch ¶
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 ClientOption ¶ added in v0.5.0
type ClientOption func(*Client)
type ClusterConfig ¶
type ClusterConfig struct { // ConfigId is the monotonically increasing identifier for the config information ConfigID int64 // NodeAddresses are array of ClusterNode which contain address of a memcache node. NodeAddresses []ClusterNode }
ClusterConfig represents cluster configuration which contains nodes and version.
type ClusterNode ¶
ClusterNode represents address of a memcached node.
type ClusterPool ¶ added in v0.7.0
type ClusterPool struct {
// contains filtered or unexported fields
}
ClusterPool is a thin wrapper around multiple Pools, one for each server in the cluster.
func NewClusterPool ¶ added in v0.7.0
func NewClusterPool(dial dialer, config PoolConfig) *ClusterPool
func (*ClusterPool) Each ¶ added in v0.7.0
func (cp *ClusterPool) Each(fn func(string, *Pool))
func (*ClusterPool) Put ¶ added in v0.7.0
func (cp *ClusterPool) Put(c *Conn)
func (*ClusterPool) Release ¶ added in v0.7.0
func (cp *ClusterPool) Release(addr net.Addr)
type ConnectTimeoutError ¶
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 // CasID is the compare and swap ID. // // It's populated by get requests and then the same value is // required for a CompareAndSwap request to succeed. CasID uint64 }
Item is an item to be got or stored in a memcached server.
type Pool ¶ added in v0.7.0
type Pool struct {
// contains filtered or unexported fields
}
Pool is a connection pool for a single server. It manages a pool of idle connections and limits the number of active connections. It also periodically closes idle connections to minimize unused resources. Note: Any connection obtained from the pool must be returned to the pool when done (either using the Put or Release method).
func (*Pool) Get ¶ added in v0.7.0
Get returns a connection from the pool or creates a new one if necessary. The connection must be returned to the pool when done. If max connections are already in use, Get will block until a connection is available or the context is canceled.
type PoolConfig ¶ added in v0.7.0
type PoolConfig struct { MaxConns int // maximum number of connections to keep open per server (includes active and idle connections) MaxIdleLifetime time.Duration // maximum amount of time an idle connection is kept open IdleClosePeriod time.Duration // how often to close (reap) idle connections OnAcquire func(string, error, time.Duration) // when we acquire a connection slot OnCreate func(string, error, time.Duration) // when we create a new connection }
type RendezvousSelector ¶ added in v0.3.0
type RendezvousSelector struct {
*ServerList
}
RendezvousSelector is a ServerSelector which uses the rendezvous hashing algorithm to select a server for a given key. See https://en.wikipedia.org/wiki/Rendezvous_hashing
func NewRendezvousSelector ¶ added in v0.3.0
func NewRendezvousSelector() RendezvousSelector
func (RendezvousSelector) PickServer ¶ added in v0.3.0
func (rs RendezvousSelector) PickServer(key string) (net.Addr, error)
PickServer uses the rendezvous hashing algorithm. It loops over each server, hashes key+server together, and selects the server with the largest hash value. Key -> server mappings should be deterministic and uniformly distributed. This method is threadsafe.
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) PickAnyServer ¶
func (ss *ServerList) PickAnyServer() (net.Addr, error)
PickAnyServer picks any active server This can be used to get information which is not linked to a key or which could be on any server.
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) // PickAnyServer returns any active server, preferably not the // same one every time in order to distribute the load. // This can be used to get information which is server agnostic. PickAnyServer() (net.Addr, error) Each(func(net.Addr) error) error // SetServers updates the list of servers available for selection. SetServers(servers ...string) 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.