Documentation ¶
Overview ¶
Package memcache provides a client for the memcached cache server.
Index ¶
- Constants
- Variables
- type Addr
- type Client
- func (c *Client) Add(item *Item) error
- func (c *Client) Close() 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) Flush(expiration int) error
- func (c *Client) Get(key string) (*Item, 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) MaxIdleConnsPerAddr() int
- func (c *Client) Set(item *Item) error
- func (c *Client) SetAuthCredentials(login, password string)
- func (c *Client) SetMaxIdleConnsPerAddr(maxIdle int)
- func (c *Client) SetTimeout(timeout time.Duration)
- func (c *Client) Timeout() time.Duration
- type ConnectTimeoutError
- type Item
- type ServerList
- type Servers
Constants ¶
const DefaultTimeout = time.Duration(100) * time.Millisecond
DefaultTimeout is the default socket read/write timeout.
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, 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 {
Login, Password string
// contains filtered or unexported fields
}
Client is a memcache client. It is safe for unlocked use by multiple concurrent goroutines.
func New ¶
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 ¶
NewFromServers returns a new Client using the provided Servers.
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) 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) Flush ¶
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 ¶
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 ¶
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) MaxIdleConnsPerAddr ¶
MaxIdleConnsPerAddr returns the maximum number of idle connections kept per server address.
func (*Client) SetAuthCredentials ¶
func (*Client) SetMaxIdleConnsPerAddr ¶
SetMaxIdleConnsPerAddr changes the maximum number of idle connections kept per server. If maxIdle < 0, no idle connections are kept. If maxIdle == 0, the default number (currently 2) is used.
func (*Client) SetTimeout ¶
SetTimeout specifies the socket read/write timeout. If zero, DefaultTimeout is used. If < 0, there's no timeout. This method must be called before any connections to the memcached server are opened.
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
func (*ConnectTimeoutError) Temporary ¶
func (cte *ConnectTimeoutError) Temporary() bool
func (*ConnectTimeoutError) Timeout ¶
func (cte *ConnectTimeoutError) Timeout() bool
type Item ¶
type Item struct { // Key is the Item's key (250 bytes maximum). Key string // Value is the Item's value. Value []byte // Object is the Item's value for use with a Codec. Object interface{} // 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(servers ...string) (*ServerList, error)
NewServerList returns a new ServerList with the given servers. All servers have the same weight. To give a server more weight, list it multiple times.
NewServerList returns an error if any of the received addresses is not valid or fails to resolve, but it doesn't try to connect to the provided servers.
func (*ServerList) PickServer ¶
func (s *ServerList) PickServer(key string) (*Addr, error)
func (*ServerList) Servers ¶
func (s *ServerList) Servers() ([]*Addr, error)
type Servers ¶
type Servers interface { // PickServer selects one server from the ones by // managed by the Servers instance, based on the // given key. PickServer(key string) (*Addr, error) // Servers returns all the servers managed by the // Servers instance. Servers() ([]*Addr, error) }
Servers is the interface used to manage a set of memcached servers.
Implementations must be safely accessible from multiple goroutines.