Documentation ¶
Overview ¶
Package memcache provides a client for the memcached cache server.
Index ¶
- Constants
- Variables
- func DebugLog(message string)
- func InitWorkerManager(manager *WorkerManager, maxWorkers int, connFactory ConnectionFactory)
- func NewWorkerConnAndProcessor(cf ConnectionFactory) (workerConnAndProcessor, error)
- func ResolveServerAddr(server string) (net.Addr, error)
- type BufferedReader
- type ClientInterface
- type ConnectTimeoutError
- type ConnectionFactory
- type Item
- type PipeliningClient
- func (c *PipeliningClient) Add(item *Item) error
- func (c *PipeliningClient) CompareAndSwap(item *Item) error
- func (c *PipeliningClient) Decrement(key string, delta uint64) (newValue uint64, err error)
- func (c *PipeliningClient) Delete(key string) error
- func (c *PipeliningClient) DeleteAll() error
- func (c *PipeliningClient) Finalize()
- func (c *PipeliningClient) FlushAll() error
- func (c *PipeliningClient) Get(key string) (item *Item, err error)
- func (c *PipeliningClient) GetMulti(keys []string) (map[string]*Item, error)
- func (c *PipeliningClient) GetMultiArray(keys []string) ([]*Item, error)
- func (c *PipeliningClient) GetServer() string
- func (c *PipeliningClient) Increment(key string, delta uint64) (newValue uint64, err error)
- func (c *PipeliningClient) Replace(item *Item) error
- func (c *PipeliningClient) SendProxiedMessageAsync(command *message.SingleMessage)
- func (c *PipeliningClient) Set(item *Item) error
- func (c *PipeliningClient) Touch(key string, seconds int32) (err error)
- type WorkerManager
Constants ¶
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 )
const ( MIN_WORKER_COUNT = 3 // At most 100 requests can be waiting inside of a chan to be unqueued MAX_BACKLOG_SIZE = 100 // There can be at most 100 requests in flight per worker. Not tuned. MAX_BACKLOG_PER_WORKER = 100 // Needed in case of spurious errors, has to be finite in case memcache cluster is down. MAX_RETRY_COUNT = 3 )
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") )
var ErrPreviousRequestFailed = errors.New("A previous request failed")
Functions ¶
func InitWorkerManager ¶
func InitWorkerManager(manager *WorkerManager, maxWorkers int, connFactory ConnectionFactory)
func NewWorkerConnAndProcessor ¶
func NewWorkerConnAndProcessor(cf ConnectionFactory) (workerConnAndProcessor, error)
func ResolveServerAddr ¶
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.
Types ¶
type BufferedReader ¶
type BufferedReader struct {
// contains filtered or unexported fields
}
type ClientInterface ¶
type ClientInterface interface { // TODO generalize SendProxiedMessageAsync(command *message.SingleMessage) Get(key string) (item *Item, err error) GetMulti(keys []string) (map[string]*Item, error) GetMultiArray(keys []string) ([]*Item, error) Set(item *Item) error Add(item *Item) error Replace(item *Item) error Increment(key string, delta uint64) (newValue uint64, err error) Decrement(key string, delta uint64) (newValue uint64, err error) Delete(key string) error DeleteAll() error Touch(key string, seconds int32) error Finalize() }
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 ConnectionFactory ¶
type ConnectionFactory interface {
// contains filtered or unexported methods
}
interface for testability
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 PipeliningClient ¶
type PipeliningClient struct { // Timeout specifies the socket read/write timeout. // If zero, DefaultTimeout is used. Timeout time.Duration // 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 // For ketama Label string Weight int // contains filtered or unexported fields }
PipeliningClient is a memcache client with pipelining. It is safe for unlocked use by multiple concurrent goroutines.
func New ¶
func New(server string, serverConnections int, timeout time.Duration) *PipeliningClient
New returns a memcache client using the provided server.
func (*PipeliningClient) Add ¶
func (c *PipeliningClient) 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 (*PipeliningClient) CompareAndSwap ¶
func (c *PipeliningClient) 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 (*PipeliningClient) Decrement ¶
func (c *PipeliningClient) 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 (*PipeliningClient) Delete ¶
func (c *PipeliningClient) 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 (*PipeliningClient) DeleteAll ¶
func (c *PipeliningClient) DeleteAll() error
DeleteAll deletes all items in the cache.
func (*PipeliningClient) Finalize ¶
func (c *PipeliningClient) Finalize()
Finalize is called in unit tests to free up open connections.
func (*PipeliningClient) FlushAll ¶
func (c *PipeliningClient) FlushAll() error
func (*PipeliningClient) Get ¶
func (c *PipeliningClient) Get(key string) (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 (*PipeliningClient) GetMulti ¶
func (c *PipeliningClient) 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 (*PipeliningClient) GetMultiArray ¶
func (c *PipeliningClient) GetMultiArray(keys []string) ([]*Item, error)
func (*PipeliningClient) GetServer ¶
func (c *PipeliningClient) GetServer() string
GetServer returns the address of the server originally passed to memcache.New().
func (*PipeliningClient) Increment ¶
func (c *PipeliningClient) 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 (*PipeliningClient) Replace ¶
func (c *PipeliningClient) Replace(item *Item) error
Replace writes the given item, but only if the server *does* already hold data for this key
func (*PipeliningClient) SendProxiedMessageAsync ¶
func (c *PipeliningClient) SendProxiedMessageAsync(command *message.SingleMessage)
func (*PipeliningClient) Set ¶
func (c *PipeliningClient) Set(item *Item) error
Set writes the given item, unconditionally.
func (*PipeliningClient) Touch ¶
func (c *PipeliningClient) 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. ErrCacheMiss is returned if the key is not in the cache. The key must be at most 250 bytes in length.
type WorkerManager ¶
type WorkerManager struct {
// contains filtered or unexported fields
}
func (*WorkerManager) Finalize ¶
func (manager *WorkerManager) Finalize()
Directories ¶
Path | Synopsis |
---|---|
proxy listens on a socket and forwards data to one or more memcache servers (TODO: Actually shard requests)
|
proxy listens on a socket and forwards data to one or more memcache servers (TODO: Actually shard requests) |
message
message contains utilities for proxying the sending out of a memcached request and receiving a memcached response
|
message contains utilities for proxying the sending out of a memcached request and receiving a memcached response |
responsequeue
responsequeue ensures that responses to requests are sent in the order they are received
|
responsequeue ensures that responses to requests are sent in the order they are received |