godropbox: github.com/dropbox/godropbox/memcache Index | Examples | Files

package memcache

import "github.com/dropbox/godropbox/memcache"

A generic memcache client library which supports connection pooling and flexible sharding.

Implementation note: this client uses memcached's binary protocol. See https://code.google.com/p/memcached/wiki/BinaryProtocolRevamped for additional details.

Index

Examples

Package Files

base_shard_manager.go constants.go doc.go interface.go mock_client.go raw_ascii_client.go raw_binary_client.go responses.go sharded_client.go static_shard_manager.go

Constants

const (
    ActiveServer    = MemcachedState(0)
    WriteOnlyServer = MemcachedState(1)
    DownServer      = MemcachedState(2)
    WarmUpServer    = MemcachedState(4)
)

func NewStatusCodeError Uses

func NewStatusCodeError(status ResponseStatus) error

type BaseShardManager Uses

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

A base shard manager implementation that can be used to implement other shard managers.

func (*BaseShardManager) GetAllShards Uses

func (m *BaseShardManager) GetAllShards() map[int]net2.ManagedConn

See ShardManager interface for documentation.

func (*BaseShardManager) GetShard Uses

func (m *BaseShardManager) GetShard(
    key string) (
    shardId int,
    conn net2.ManagedConn,
    err error)

See ShardManager interface for documentation.

func (*BaseShardManager) GetShardsForItems Uses

func (m *BaseShardManager) GetShardsForItems(
    items []*Item) map[int]*ShardMapping

See ShardManager interface for documentation.

func (*BaseShardManager) GetShardsForKeys Uses

func (m *BaseShardManager) GetShardsForKeys(
    keys []string) map[int]*ShardMapping

See ShardManager interface for documentation.

func (*BaseShardManager) GetShardsForSentinelsFromItems Uses

func (m *BaseShardManager) GetShardsForSentinelsFromItems(
    items []*Item) map[int]*ShardMapping

See ShardManager interface for documentation.

func (*BaseShardManager) GetShardsForSentinelsFromKeys Uses

func (m *BaseShardManager) GetShardsForSentinelsFromKeys(
    keys []string) map[int]*ShardMapping

See ShardManager interface for documentation.

func (*BaseShardManager) Init Uses

func (m *BaseShardManager) Init(
    shardFunc func(key string, numShard int) (shard int),
    logError func(err error),
    logInfo func(v ...interface{}),
    options net2.ConnectionOptions)

Initializes the BaseShardManager.

func (*BaseShardManager) InitWithPool Uses

func (m *BaseShardManager) InitWithPool(
    shardFunc func(key string, numShard int) (shard int),
    logError func(err error),
    logInfo func(v ...interface{}),
    pool net2.ConnectionPool)

func (*BaseShardManager) UpdateShardStates Uses

func (m *BaseShardManager) UpdateShardStates(shardStates []ShardState)

This updates the shard manager to use new shard states.

type Client Uses

type Client interface {
    // This retrieves a single entry from memcache.
    Get(key string) GetResponse

    // Batch version of the Get method.
    GetMulti(keys []string) map[string]GetResponse

    // *** This method is specific to Dropbox zookeeper-managed memcache ***
    // This is the same as GetMulti.  The only difference is that GetMulti will
    // only read from ACTIVE memcache shards, while GetSentinels will read from
    // both ACTIVE and WRITE_ONLY memcache shards.
    GetSentinels(keys []string) map[string]GetResponse

    // This sets a single entry into memcache.  If the item's data version id
    // (aka CAS) is nonzero, the set operation can only succeed if the item
    // exists in memcache and has a same data version id; otherwise, this will
    // do an unconditional set.
    Set(item *Item) MutateResponse

    // Batch version of the Set method.  Note that the response entries
    // ordering is undefined (i.e., may not match the input ordering).
    SetMulti(items []*Item) []MutateResponse

    // *** This method is specific to Dropbox zookeeper-managed memcache ***
    // This is the same as SetMulti.  The only difference is that SetMulti will
    // only write to ACTIVE memcache shards, while SetSentinels will write to
    // both ACTIVE and WRITE_ONLY memcache shards.
    SetSentinels(items []*Item) []MutateResponse

    // Just like SetMulti, but if item's data version id (aka CAS) is zero,
    // it will do a **conditional** add (will fail if the item already exists
    // in memcache).
    CasMulti(items []*Item) []MutateResponse

    // *** This method is specific to Dropbox zookeeper-managed memcache ***
    // This is the same as CasMulti.  The only difference is that CasMulti will
    // only write to ACTIVE memcache shards, while CasSentinels will write to
    // both ACTIVE and WRITE_ONLY memcache shards.
    CasSentinels(items []*Item) []MutateResponse

    // This adds a single entry into memcache.  Note: Add will fail if the
    // item already exist in memcache.
    Add(item *Item) MutateResponse

    // Batch version of the Add method.  Note that the response entries
    // ordering is undefined (i.e., may not match the input ordering).
    AddMulti(item []*Item) []MutateResponse

    // This replaces a single entry in memcache.  Note: Replace will fail if
    // the does not exist in memcache.
    Replace(item *Item) MutateResponse

    // This delets a single entry from memcache.
    Delete(key string) MutateResponse

    // Batch version of the Delete method.  Note that the response entries
    // ordering is undefined (i.e., may not match the input ordering)
    DeleteMulti(keys []string) []MutateResponse

    // This appends the value bytes to the end of an existing entry.  Note that
    // this does not allow you to extend past the item limit.
    Append(key string, value []byte) MutateResponse

    // This prepends the value bytes to the end of an existing entry.  Note that
    // this does not allow you to extend past the item limit.
    Prepend(key string, value []byte) MutateResponse

    // This increments the key's counter by delta.  If the counter does not
    // exist, one of two things may happen:
    // 1. If the expiration value is all one-bits (0xffffffff), the operation
    //    will fail with StatusNotFound.
    // 2. For all other expiration values, the operation will succeed by
    //    seeding the value for this key with the provided initValue to expire
    //    with the provided expiration time. The flags will be set to zero.
    //
    // NOTE:
    // 1. If you want to set the value of the counter with add/set/replace,
    //    the objects data must be the ascii representation of the value and
    //    not the byte values of a 64 bit integer.
    // 2. Incrementing the counter may cause the counter to wrap.
    Increment(
        key string,
        delta uint64,
        initValue uint64,
        expiration uint32) CountResponse

    // This decrements the key's counter by delta.  If the counter does not
    // exist, one of two things may happen:
    // 1. If the expiration value is all one-bits (0xffffffff), the operation
    //    will fail with StatusNotFound.
    // 2. For all other expiration values, the operation will succeed by
    //    seeding the value for this key with the provided initValue to expire
    //    with the provided expiration time. The flags will be set to zero.
    //
    // NOTE:
    // 1. If you want to set the value of the counter with add/set/replace,
    //    the objects data must be the ascii representation of the value and
    //    not the byte values of a 64 bit integer.
    // 2. Decrementing a counter will never result in a "negative value" (or
    //    cause the counter to "wrap"). instead the counter is set to 0.
    Decrement(
        key string,
        delta uint64,
        initValue uint64,
        expiration uint32) CountResponse

    // This invalidates all existing cache items after expiration number of
    // seconds.
    Flush(expiration uint32) Response

    // This requests the server statistics. When the key is an empty string,
    // the server will respond with a "default" set of statistics information.
    Stat(statsKey string) StatResponse

    // This returns the server's version string.
    Version() VersionResponse

    // This set the verbosity level of the server.
    Verbosity(verbosity uint32) Response
}

NOTE(mihnea) - ideally we should be able to separate methods that are state-aware (i.e. GetSentinels / SetSentinels) away from this interface and into a new interface (e.g. StateAwareClient / ConsistentCachingClient). Such an interface should also have fewer methods (e.g. no Increment, Flush, etc.)

func NewMockClient Uses

func NewMockClient() Client

func NewMockClientErrorAllSets Uses

func NewMockClientErrorAllSets() Client

func NewMockClientFailEverything Uses

func NewMockClientFailEverything() Client

func NewMockClientMissAllGets Uses

func NewMockClientMissAllGets() Client

func NewShardedClient Uses

func NewShardedClient(
    manager ShardManager,
    builder ClientShardBuilder) Client

This creates a new ShardedClient.

type ClientShard Uses

type ClientShard interface {
    Client

    // This returns the memcache server's shard id.
    ShardId() int

    // This returns true if the client is in a valid state.  If the client is
    // in invalid state, the user should abandon the current client / channel,
    // and create a new client / channel as replacment.
    IsValidState() bool
}

A memcache client which communicates with a specific memcache shard.

func NewLargeRawBinaryClient Uses

func NewLargeRawBinaryClient(shard int, channel io.ReadWriter) ClientShard

This creates a new memcache RawBinaryClient for use with np-large cluster.

func NewRawAsciiClient Uses

func NewRawAsciiClient(shard int, channel io.ReadWriter) ClientShard

This creates a new memcache RawAsciiClient.

func NewRawBinaryClient Uses

func NewRawBinaryClient(shard int, channel io.ReadWriter) ClientShard

This creates a new memcache RawBinaryClient.

type ClientShardBuilder Uses

type ClientShardBuilder func(shard int, channel io.ReadWriter) ClientShard

The ClientShardBuilder creates ClientShard by shard id and connection.

type CountResponse Uses

type CountResponse interface {
    Response

    // This returns the input key (useful for SetMulti where operations may be
    // applied out of order).
    Key() string

    // This returns the resulting count value.  On error status, this returns
    // zero.
    Count() uint64
}

Response returned by Increment/Decrement requests.

func NewCountErrorResponse Uses

func NewCountErrorResponse(key string, err error) CountResponse

This creates a CountResponse from an error.

func NewCountResponse Uses

func NewCountResponse(
    key string,
    status ResponseStatus,
    count uint64) CountResponse

This creates a normal CountResponse.

type GetResponse Uses

type GetResponse interface {
    Response

    // This returns the key for the requested value.
    Key() string

    // This returns the retreived entry.  The value may be nil.
    Value() []byte

    // This returns the entry's flags value.  The value is only valid when
    // the entry is found.
    Flags() uint32

    // This returns the data version id (aka CAS) for the item.  The value is
    // only valid when the entry is found.
    DataVersionId() uint64
}

Response returned by Get/GetKey/GetAndTouch requests.

func NewGetErrorResponse Uses

func NewGetErrorResponse(key string, err error) GetResponse

This creates a GetResponse from an error.

func NewGetResponse Uses

func NewGetResponse(
    key string,
    status ResponseStatus,
    flags uint32,
    value []byte,
    version uint64) GetResponse

This creates a normal GetResponse.

type Item Uses

type Item struct {
    // The item's key (the key can be up to 250 bytes maximum).
    Key string

    // The item's value.
    Value []byte

    // Flags are server-opaque flags whose semantics are entirely up to the app.
    Flags uint32

    // aka CAS (check and set) in memcache documentation.
    DataVersionId uint64

    // 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 uint32
}

An item to be gotten from or stored in a memcache server.

type MemcachedState Uses

type MemcachedState int

type MockClient Uses

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

func (*MockClient) Add Uses

func (c *MockClient) Add(item *Item) MutateResponse

This adds a single entry into memcache. Note: Add will fail if the item already exist in memcache.

func (*MockClient) AddMulti Uses

func (c *MockClient) AddMulti(items []*Item) []MutateResponse

Batch version of the Add method. Note that the response entries ordering is undefined (i.e., may not match the input ordering).

func (*MockClient) Append Uses

func (c *MockClient) Append(key string, value []byte) MutateResponse

This appends the value bytes to the end of an existing entry. Note that this does not allow you to extend past the item limit.

func (*MockClient) CasMulti Uses

func (c *MockClient) CasMulti(items []*Item) []MutateResponse

func (*MockClient) CasSentinels Uses

func (c *MockClient) CasSentinels(items []*Item) []MutateResponse

func (*MockClient) Decrement Uses

func (c *MockClient) Decrement(
    key string,
    delta uint64,
    initValue uint64,
    expiration uint32) CountResponse

This decrements the key's counter by delta. If the counter does not exist, one of two things may happen: 1. If the expiration value is all one-bits (0xffffffff), the operation will fail with StatusNotFound. 2. For all other expiration values, the operation will succeed by seeding the value for this key with the provided initValue to expire with the provided expiration time. The flags will be set to zero.

NOTE: 1. If you want to set the value of the counter with add/set/replace, the objects data must be the ascii representation of the value and not the byte values of a 64 bit integer. 2. Decrementing a counter will never result in a "negative value" (or cause the counter to "wrap"). instead the counter is set to 0.

func (*MockClient) Delete Uses

func (c *MockClient) Delete(key string) MutateResponse

This deletes a single entry from memcache.

func (*MockClient) DeleteMulti Uses

func (c *MockClient) DeleteMulti(keys []string) []MutateResponse

Batch version of the Delete method. Note that the response entries ordering is undefined (i.e., may not match the input ordering)

func (*MockClient) Flush Uses

func (c *MockClient) Flush(expiration uint32) Response

This invalidates all existing cache items after expiration number of seconds.

func (*MockClient) Get Uses

func (c *MockClient) Get(key string) GetResponse

This retrieves a single entry from memcache.

func (*MockClient) GetMulti Uses

func (c *MockClient) GetMulti(keys []string) map[string]GetResponse

Batch version of the Get method.

func (*MockClient) GetSentinels Uses

func (c *MockClient) GetSentinels(keys []string) map[string]GetResponse

func (*MockClient) Increment Uses

func (c *MockClient) Increment(
    key string,
    delta uint64,
    initValue uint64,
    expiration uint32) CountResponse

This increments the key's counter by delta. If the counter does not exist, one of two things may happen: 1. If the expiration value is all one-bits (0xffffffff), the operation will fail with StatusNotFound. 2. For all other expiration values, the operation will succeed by seeding the value for this key with the provided initValue to expire with the provided expiration time. The flags will be set to zero.

NOTE: 1. If you want to set the value of the counter with add/set/replace, the objects data must be the ascii representation of the value and not the byte values of a 64 bit integer. 2. Incrementing the counter may cause the counter to wrap.

func (*MockClient) Prepend Uses

func (c *MockClient) Prepend(key string, value []byte) MutateResponse

This prepends the value bytes to the end of an existing entry. Note that this does not allow you to extend past the item limit.

func (*MockClient) Replace Uses

func (c *MockClient) Replace(item *Item) MutateResponse

This replaces a single entry in memcache. Note: Replace will fail if the does not exist in memcache.

func (*MockClient) Set Uses

func (c *MockClient) Set(item *Item) MutateResponse

This sets a single entry into memcache. If the item's data version id (aka CAS) is nonzero, the set operation can only succeed if the item exists in memcache and has a same data version id.

func (*MockClient) SetMulti Uses

func (c *MockClient) SetMulti(items []*Item) []MutateResponse

Batch version of the Set method. Note that the response entries ordering is undefined (i.e., may not match the input ordering).

func (*MockClient) SetSentinels Uses

func (c *MockClient) SetSentinels(items []*Item) []MutateResponse

func (*MockClient) Stat Uses

func (c *MockClient) Stat(statsKey string) StatResponse

This requests the server statistics. When the key is an empty string, the server will respond with a "default" set of statistics information.

func (*MockClient) Verbosity Uses

func (c *MockClient) Verbosity(verbosity uint32) Response

This set the verbosity level of the server.

func (*MockClient) Version Uses

func (c *MockClient) Version() VersionResponse

This returns the server's version string.

type MutateResponse Uses

type MutateResponse interface {
    Response

    // This returns the input key (useful for SetMulti where operations may be
    // applied out of order).
    Key() string

    // This returns the data version id (aka CAS) for the item.  For delete
    // requests, this always returns zero.
    DataVersionId() uint64
}

Response returned by Set/Add/Replace/Delete/Append/Prepend requests.

func NewMutateErrorResponse Uses

func NewMutateErrorResponse(key string, err error) MutateResponse

This creates a MutateResponse from an error.

func NewMutateResponse Uses

func NewMutateResponse(
    key string,
    status ResponseStatus,
    version uint64) MutateResponse

This creates a normal MutateResponse.

type Operation Uses

type Operation int
const (
    Increment Operation = iota
    Decrement
)

type RawAsciiClient Uses

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

An unsharded memcache client implementation which operates on a pre-existing io channel (The user must explicitly setup and close down the channel), using the ascii memcache protocol. Note that the client assumes nothing else is sending or receiving on the network channel. In general, all client operations are serialized (Use multiple channels / clients if parallelism is needed).

Code:

conn, _ := net.Dial("tcp", "localhost:11211")

client := memcache.NewRawAsciiClient(0, conn)

clientExample(client)

func (*RawAsciiClient) Add Uses

func (c *RawAsciiClient) Add(item *Item) MutateResponse

func (*RawAsciiClient) AddMulti Uses

func (c *RawAsciiClient) AddMulti(items []*Item) []MutateResponse

func (*RawAsciiClient) Append Uses

func (c *RawAsciiClient) Append(key string, value []byte) MutateResponse

func (*RawAsciiClient) CasMulti Uses

func (c *RawAsciiClient) CasMulti(items []*Item) []MutateResponse

func (*RawAsciiClient) CasSentinels Uses

func (c *RawAsciiClient) CasSentinels(items []*Item) []MutateResponse

func (*RawAsciiClient) Decrement Uses

func (c *RawAsciiClient) Decrement(
    key string,
    delta uint64,
    initValue uint64,
    expiration uint32) CountResponse

func (*RawAsciiClient) Delete Uses

func (c *RawAsciiClient) Delete(key string) MutateResponse

func (*RawAsciiClient) DeleteMulti Uses

func (c *RawAsciiClient) DeleteMulti(keys []string) []MutateResponse

func (*RawAsciiClient) Flush Uses

func (c *RawAsciiClient) Flush(expiration uint32) Response

func (*RawAsciiClient) Get Uses

func (c *RawAsciiClient) Get(key string) GetResponse

func (*RawAsciiClient) GetMulti Uses

func (c *RawAsciiClient) GetMulti(keys []string) map[string]GetResponse

func (*RawAsciiClient) GetSentinels Uses

func (c *RawAsciiClient) GetSentinels(keys []string) map[string]GetResponse

func (*RawAsciiClient) Increment Uses

func (c *RawAsciiClient) Increment(
    key string,
    delta uint64,
    initValue uint64,
    expiration uint32) CountResponse

func (*RawAsciiClient) IsValidState Uses

func (c *RawAsciiClient) IsValidState() bool

func (*RawAsciiClient) Prepend Uses

func (c *RawAsciiClient) Prepend(key string, value []byte) MutateResponse

func (*RawAsciiClient) Replace Uses

func (c *RawAsciiClient) Replace(item *Item) MutateResponse

func (*RawAsciiClient) Set Uses

func (c *RawAsciiClient) Set(item *Item) MutateResponse

func (*RawAsciiClient) SetMulti Uses

func (c *RawAsciiClient) SetMulti(items []*Item) []MutateResponse

func (*RawAsciiClient) SetSentinels Uses

func (c *RawAsciiClient) SetSentinels(items []*Item) []MutateResponse

func (*RawAsciiClient) ShardId Uses

func (c *RawAsciiClient) ShardId() int

func (*RawAsciiClient) Stat Uses

func (c *RawAsciiClient) Stat(statsKey string) StatResponse

func (*RawAsciiClient) Verbosity Uses

func (c *RawAsciiClient) Verbosity(verbosity uint32) Response

func (*RawAsciiClient) Version Uses

func (c *RawAsciiClient) Version() VersionResponse

type RawBinaryClient Uses

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

An unsharded memcache client implementation which operates on a pre-existing io channel (The user must explicitly setup and close down the channel), using the binary memcached protocol. Note that the client assumes nothing else is sending or receiving on the network channel. In general, all client operations are serialized (Use multiple channels / clients if parallelism is needed).

Code:

conn, _ := net.Dial("tcp", "localhost:11211")

client := memcache.NewRawBinaryClient(0, conn)

clientExample(client)

func (*RawBinaryClient) Add Uses

func (c *RawBinaryClient) Add(item *Item) MutateResponse

See Client interface for documentation.

func (*RawBinaryClient) AddMulti Uses

func (c *RawBinaryClient) AddMulti(items []*Item) []MutateResponse

See Client interface for documentation.

func (*RawBinaryClient) Append Uses

func (c *RawBinaryClient) Append(key string, value []byte) MutateResponse

See Client interface for documentation.

func (*RawBinaryClient) CasMulti Uses

func (c *RawBinaryClient) CasMulti(items []*Item) []MutateResponse

See Client interface for documentation.

func (*RawBinaryClient) CasSentinels Uses

func (c *RawBinaryClient) CasSentinels(items []*Item) []MutateResponse

See Client interface for documentation.

func (*RawBinaryClient) Decrement Uses

func (c *RawBinaryClient) Decrement(
    key string,
    delta uint64,
    initValue uint64,
    expiration uint32) CountResponse

See Client interface for documentation.

func (*RawBinaryClient) Delete Uses

func (c *RawBinaryClient) Delete(key string) MutateResponse

See Client interface for documentation.

func (*RawBinaryClient) DeleteMulti Uses

func (c *RawBinaryClient) DeleteMulti(keys []string) []MutateResponse

See Client interface for documentation.

func (*RawBinaryClient) Flush Uses

func (c *RawBinaryClient) Flush(expiration uint32) Response

See Client interface for documentation.

func (*RawBinaryClient) Get Uses

func (c *RawBinaryClient) Get(key string) GetResponse

See Client interface for documentation.

func (*RawBinaryClient) GetMulti Uses

func (c *RawBinaryClient) GetMulti(keys []string) map[string]GetResponse

See Client interface for documentation.

func (*RawBinaryClient) GetSentinels Uses

func (c *RawBinaryClient) GetSentinels(keys []string) map[string]GetResponse

func (*RawBinaryClient) Increment Uses

func (c *RawBinaryClient) Increment(
    key string,
    delta uint64,
    initValue uint64,
    expiration uint32) CountResponse

See Client interface for documentation.

func (*RawBinaryClient) IsValidState Uses

func (c *RawBinaryClient) IsValidState() bool

See ClientShard interface for documentation.

func (*RawBinaryClient) Prepend Uses

func (c *RawBinaryClient) Prepend(key string, value []byte) MutateResponse

See Client interface for documentation.

func (*RawBinaryClient) Replace Uses

func (c *RawBinaryClient) Replace(item *Item) MutateResponse

See Client interface for documentation.

func (*RawBinaryClient) Set Uses

func (c *RawBinaryClient) Set(item *Item) MutateResponse

See Client interface for documentation.

func (*RawBinaryClient) SetMulti Uses

func (c *RawBinaryClient) SetMulti(items []*Item) []MutateResponse

See Client interface for documentation.

func (*RawBinaryClient) SetSentinels Uses

func (c *RawBinaryClient) SetSentinels(items []*Item) []MutateResponse

See Client interface for documentation.

func (*RawBinaryClient) ShardId Uses

func (c *RawBinaryClient) ShardId() int

See ClientShard interface for documentation.

func (*RawBinaryClient) Stat Uses

func (c *RawBinaryClient) Stat(statsKey string) StatResponse

See Client interface for documentation.

func (*RawBinaryClient) Verbosity Uses

func (c *RawBinaryClient) Verbosity(verbosity uint32) Response

See Client interface for documentation.

func (*RawBinaryClient) Version Uses

func (c *RawBinaryClient) Version() VersionResponse

See Client interface for documentation.

type Response Uses

type Response interface {
    // This returns the status returned by the memcache server.  When Error()
    // is non-nil, this value may not be valid.
    //
    // NOTE:
    // 1. For stat request, this returns the first non-StatusNoError encountered
    //    (or StatusNoError if there were no errors).
    // 2. If the client is sharded, flush/stats/version/verbosity requests
    //    will return the first non-StatusNoError encountered (or StatusNoError
    //    if there were no errors).
    Status() ResponseStatus

    // This returns nil when no error is encountered by the client, and the
    // response status returned by the memcache server is StatusNoError.
    // Otherwise, this returns an error.
    //
    // NOTE:
    // 1. For get requests, this also returns nil when the response status
    //    StatusKeyNotFound.
    // 2. For stat request, this returns the first error encountered (or nil
    //    if there were no errors).
    // 3. If the client is sharded, flush/stats/version/verbosity requests
    //    will return the first error encountered (or nil if there were no
    //    errors).
    Error() error
}

A generic response to a memcache request.

func NewErrorResponse Uses

func NewErrorResponse(err error) Response

This creates a Response from an error.

func NewResponse Uses

func NewResponse(status ResponseStatus) Response

This creates a Response from status.

type ResponseStatus Uses

type ResponseStatus uint16
const (
    StatusNoError ResponseStatus = iota
    StatusKeyNotFound
    StatusKeyExists
    StatusValueTooLarge
    StatusInvalidArguments
    StatusItemNotStored
    StatusIncrDecrOnNonNumericValue
    StatusVbucketBelongsToAnotherServer // Not used
    StatusAuthenticationError           // Not used
    StatusAuthenticationContinue        // Not used
)
const (
    StatusUnknownCommand ResponseStatus = 0x81 + iota
    StatusOutOfMemory
    StatusNotSupported
    StatusInternalError
    StatusBusy
    StatusTempFailure
)

type ShardManager Uses

type ShardManager interface {
    // This returns the shard id and a connection to shard for a single key.
    // If the key does not belong to any shard, this returns (-1, nil). Note
    // that the connection may be nil for valid shard id (for instance, if
    // the shard server is temporarily unavailable).
    GetShard(key string) (shardId int, conn net2.ManagedConn, err error)

    // This returns a (shard id -> (connection, list of keys)) mapping for
    // the requested keys.  Keys that do not belong to any shard are mapped
    // to shard id -1.
    GetShardsForKeys(keys []string) map[int]*ShardMapping

    // This returns a (shard id -> (connection, list of items)) mapping for
    // the requested items.  Items that do not belong to any shard are mapped
    // to shard id -1.
    GetShardsForItems(items []*Item) map[int]*ShardMapping

    // *** This method is specific to Dropbox zookeeper-managed memcache ***
    // This returns a (shard id -> (connection, list of items)) mapping for
    // the requested sentinel keys. Sentinel keys that do not belong to any
    // shard are mapped to shard id -1.
    GetShardsForSentinelsFromKeys(keys []string) map[int]*ShardMapping

    // *** This method is specific to Dropbox zookeeper-managed memcache ***
    // This returns a (shard id -> (connection, list of items)) mapping for
    // the requested sentinel items. Sentinel items that do not belong to any
    // shard are mapped to shard id -1.
    GetShardsForSentinelsFromItems(items []*Item) map[int]*ShardMapping

    // This return a (shard id -> connection) mapping for all shards.
    GetAllShards() map[int]net2.ManagedConn
}

The ShardManager decides which memcache shard a key/item belongs to, and provide connections to the shards.

func NewStaticShardManager Uses

func NewStaticShardManager(
    serverAddrs []string,
    shardFunc func(key string, numShard int) (shard int),
    options net2.ConnectionOptions) ShardManager

This creates a StaticShardManager, which returns connections from a static list of memcache shards.

type ShardMapping Uses

type ShardMapping struct {
    Connection net2.ManagedConn
    ConnErr    error
    Keys       []string // Populated by the 4 GetShards* methods
    Items      []*Item  // Populated by GetShardsForItems and GetShardsForSentinelsFromItems
    WarmingUp  bool     // Populated by GetShardsForSentinels
}

Used for returning shard mapping results from ShardManager's GetShardsForKeys/GetShardsForItems calls.

type ShardState Uses

type ShardState struct {
    Address string
    State   MemcachedState
}

type ShardedClient Uses

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

A sharded memcache client implementation where sharding management is handled by the provided ShardManager.

Code:

options := net2.ConnectionOptions{
    MaxActiveConnections: 4,
}
manager := memcache.NewStaticShardManager(
    []string{"localhost:11211", "localhost:11212"},
    func(key string, numShard int) int {
        return int(crc32.ChecksumIEEE([]byte(key))) % 2
    },
    options)

client := memcache.NewShardedClient(manager, memcache.NewRawBinaryClient)

clientExample(client)

func (*ShardedClient) Add Uses

func (c *ShardedClient) Add(item *Item) MutateResponse

See Client interface for documentation.

func (*ShardedClient) AddMulti Uses

func (c *ShardedClient) AddMulti(items []*Item) []MutateResponse

See Client interface for documentation.

func (*ShardedClient) Append Uses

func (c *ShardedClient) Append(key string, value []byte) MutateResponse

See Client interface for documentation.

func (*ShardedClient) CasMulti Uses

func (c *ShardedClient) CasMulti(items []*Item) []MutateResponse

See Client interface for documentation.

func (*ShardedClient) CasSentinels Uses

func (c *ShardedClient) CasSentinels(items []*Item) []MutateResponse

See Client interface for documentation.

func (*ShardedClient) Decrement Uses

func (c *ShardedClient) Decrement(
    key string,
    delta uint64,
    initValue uint64,
    expiration uint32) CountResponse

See Client interface for documentation.

func (*ShardedClient) Delete Uses

func (c *ShardedClient) Delete(key string) MutateResponse

See Client interface for documentation.

func (*ShardedClient) DeleteMulti Uses

func (c *ShardedClient) DeleteMulti(keys []string) []MutateResponse

See Client interface for documentation.

func (*ShardedClient) Flush Uses

func (c *ShardedClient) Flush(expiration uint32) Response

See Client interface for documentation.

func (*ShardedClient) Get Uses

func (c *ShardedClient) Get(key string) GetResponse

See Client interface for documentation.

func (*ShardedClient) GetMulti Uses

func (c *ShardedClient) GetMulti(keys []string) map[string]GetResponse

See Client interface for documentation.

func (*ShardedClient) GetSentinels Uses

func (c *ShardedClient) GetSentinels(keys []string) map[string]GetResponse

See Client interface for documentation.

func (*ShardedClient) Increment Uses

func (c *ShardedClient) Increment(
    key string,
    delta uint64,
    initValue uint64,
    expiration uint32) CountResponse

See Client interface for documentation.

func (*ShardedClient) Prepend Uses

func (c *ShardedClient) Prepend(key string, value []byte) MutateResponse

See Client interface for documentation.

func (*ShardedClient) Replace Uses

func (c *ShardedClient) Replace(item *Item) MutateResponse

See Client interface for documentation.

func (*ShardedClient) Set Uses

func (c *ShardedClient) Set(item *Item) MutateResponse

See Client interface for documentation.

func (*ShardedClient) SetMulti Uses

func (c *ShardedClient) SetMulti(items []*Item) []MutateResponse

See Client interface for documentation.

func (*ShardedClient) SetSentinels Uses

func (c *ShardedClient) SetSentinels(items []*Item) []MutateResponse

See Client interface for documentation.

func (*ShardedClient) Stat Uses

func (c *ShardedClient) Stat(statsKey string) StatResponse

See Client interface for documentation.

func (*ShardedClient) Verbosity Uses

func (c *ShardedClient) Verbosity(verbosity uint32) Response

See Client interface for documentation.

func (*ShardedClient) Version Uses

func (c *ShardedClient) Version() VersionResponse

See Client interface for documentation.

type StatResponse Uses

type StatResponse interface {
    Response

    // This returns the retrieved stat entries.  On error status, this returns
    // nil.  The mapping is stored as:
    //      shard id -> stats key -> stats value
    // (If the client is unsharded, the shard id is always zero).
    Entries() map[int](map[string]string)
}

Response returned by Stat request.

func NewStatErrorResponse Uses

func NewStatErrorResponse(
    err error,
    entries map[int](map[string]string)) StatResponse

This creates a StatResponse from an error.

func NewStatResponse Uses

func NewStatResponse(
    status ResponseStatus,
    entries map[int](map[string]string)) StatResponse

This creates a normal StatResponse.

type StaticShardManager Uses

type StaticShardManager struct {
    BaseShardManager
}

A shard manager that returns connections from a static list of memcache shards. NOTE: This is only for illustration purposes. DO NOT USE IN PRODUCTION. (Dropbox internally uses a different shard manager which is also based on BaseShardManager. Our memcache config is managed by zookeeper. When our memcache config changes, zookeeper will notify the shard manager of these updates and the shard manager will in turn swap in/out shards via UpdateShardStates.)

type VersionResponse Uses

type VersionResponse interface {
    Response

    // This returns the memcache version entries.  On error status, this
    // returns an empty string.  The mapping is stored as:
    //      shard id -> version string
    // (If the client is unsharded, the shard id is always zero).
    Versions() map[int]string
}

Response returned by Version request.

func NewVersionErrorResponse Uses

func NewVersionErrorResponse(
    err error,
    versions map[int]string) VersionResponse

This creates a VersionResponse from an error.

func NewVersionResponse Uses

func NewVersionResponse(
    status ResponseStatus,
    versions map[int]string) VersionResponse

This creates a normal VersionResponse.

Package memcache imports 12 packages (graph) and is imported by 3 packages. Updated 2017-10-13. Refresh now. Tools for package owners.