Documentation
¶
Overview ¶
Package client provides Memcached client functionality including connection handling, key enumeration, CAS operations, and server capability detection.
Index ¶
- Constants
- Variables
- func IsCASConflict(err error) bool
- func IsCacheMiss(err error) bool
- func IsVersionSupported(version string) bool
- func ParseVersion(version string) (major, minor, patch int, err error)
- type CASConflictError
- type CASItem
- type Capability
- type CapabilityDetector
- type Client
- func (c *Client) Address() string
- func (c *Client) Close() error
- func (c *Client) CompareAndSwap(item *CASItem) error
- func (c *Client) Delete(key string) error
- func (c *Client) Get(key string) (*memcache.Item, error)
- func (c *Client) GetWithCAS(key string) (*CASItem, error)
- func (c *Client) Ping(ctx context.Context) error
- func (c *Client) RawCommand(ctx context.Context, cmd string) ([]string, error)
- func (c *Client) Set(item *memcache.Item) error
- func (c *Client) SetWithExpiration(key string, value []byte, flags uint32, expiration int32) error
- func (c *Client) Stats() (map[string]string, error)
- type KeyEnumerator
- type MemcachedClient
- type Option
Constants ¶
const MinRequiredVersion = "1.4.31"
MinRequiredVersion is the minimum supported Memcached version
Variables ¶
var ErrUnsupportedVersion = errors.New(
"memcached version 1.4.31 or later is required for lru_crawler metadump support")
ErrUnsupportedVersion is returned when the Memcached version is too old
Functions ¶
func IsCASConflict ¶
IsCASConflict checks if the error is a CAS conflict error.
func IsCacheMiss ¶
IsCacheMiss checks if the error is a cache miss error.
func IsVersionSupported ¶
IsVersionSupported checks if the version meets minimum requirements (1.4.31+)
func ParseVersion ¶
ParseVersion parses a version string like "1.6.22" into major, minor, patch
Types ¶
type CASConflictError ¶
type CASConflictError struct {
// contains filtered or unexported fields
}
CASConflictError is returned when a CompareAndSwap operation fails because the item has been modified by another client since it was read.
func NewCASConflictError ¶
func NewCASConflictError(key string) *CASConflictError
NewCASConflictError creates a new CASConflictError for the given key.
func (*CASConflictError) Error ¶
func (e *CASConflictError) Error() string
Error implements the error interface.
func (*CASConflictError) Key ¶
func (e *CASConflictError) Key() string
Key returns the key that caused the conflict.
type CASItem ¶
type CASItem struct {
// Key is the item's key (max 250 bytes)
Key string
// Value is the item's value
Value []byte
// Flags are server-opaque flags whose semantics are defined by the client
Flags uint32
// Expiration is the cache expiration time, in seconds:
// 0 means no expiration, values up to 30 days are interpreted as relative,
// larger values are interpreted as absolute Unix timestamps
Expiration int32
// CAS is the compare-and-swap token returned by GetWithCAS.
// This value is informational only - the actual CAS token is stored
// internally in the mcItem field for use with CompareAndSwap.
CAS uint64
// contains filtered or unexported fields
}
CASItem represents a Memcached item with CAS (Compare-And-Swap) support. The CAS token is used for optimistic locking - it ensures that the item has not been modified by another client between read and write operations.
func NewCASItem ¶
NewCASItem creates a new CASItem with the specified values. Note: Items created this way cannot be used with CompareAndSwap as they don't have a valid CAS token from the server.
func (*CASItem) ToMemcacheItem ¶
ToMemcacheItem converts a CASItem to a gomemcache Item. Note: The CAS value is handled separately as gomemcache uses internal casid field.
type Capability ¶
Capability represents the detected server capabilities
type CapabilityDetector ¶
type CapabilityDetector struct {
// contains filtered or unexported fields
}
CapabilityDetector detects server capabilities
func NewCapabilityDetector ¶
func NewCapabilityDetector() *CapabilityDetector
NewCapabilityDetector creates a new capability detector
func (*CapabilityDetector) Detect ¶
func (d *CapabilityDetector) Detect(addr string) (*Capability, error)
Detect connects to the server and detects its capabilities
func (*CapabilityDetector) Verify ¶
func (d *CapabilityDetector) Verify(addr string) (*Capability, error)
Verify checks if the server meets the minimum requirements
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client wraps gomemcache client with additional functionality
func (*Client) CompareAndSwap ¶
CompareAndSwap atomically updates an item only if it hasn't been modified since it was last read (i.e., the CAS token still matches).
The typical usage pattern is:
- Call GetWithCAS to get the current value and CAS token
- Modify the Value field of the returned CASItem
- Call CompareAndSwap with the modified CASItem
Returns:
- nil: The update was successful
- *CASConflictError: Another client modified the item (CAS mismatch)
- other errors: Network or server errors
func (*Client) GetWithCAS ¶
GetWithCAS retrieves an item by key along with its CAS token. The returned CASItem can be modified and passed to CompareAndSwap to perform an atomic update.
Returns:
- *CASItem: The item with its current value and CAS token
- error: ErrCacheMiss if the key doesn't exist, or other errors
func (*Client) RawCommand ¶
RawCommand sends a raw command and returns the response lines This is used for commands not supported by gomemcache like lru_crawler metadump
func (*Client) SetWithExpiration ¶
SetWithExpiration is a convenience method to set a key with expiration. This is useful for setting up test data before CAS operations.
type KeyEnumerator ¶
type KeyEnumerator struct {
// contains filtered or unexported fields
}
KeyEnumerator enumerates keys from Memcached using lru_crawler metadump
func NewKeyEnumerator ¶
func NewKeyEnumerator(addr string) *KeyEnumerator
NewKeyEnumerator creates a new key enumerator
func (*KeyEnumerator) EnumerateAll ¶
EnumerateAll collects all keys and returns them as a slice
func (*KeyEnumerator) EnumerateStream ¶
EnumerateStream returns channels for streaming key enumeration
func (*KeyEnumerator) WithTimeout ¶
func (e *KeyEnumerator) WithTimeout(d time.Duration) *KeyEnumerator
WithTimeout sets the timeout for enumeration
type MemcachedClient ¶
type MemcachedClient interface {
// Get retrieves an item by key
Get(key string) (*memcache.Item, error)
// GetWithCAS retrieves an item by key along with its CAS token
// for optimistic locking
GetWithCAS(key string) (*CASItem, error)
// Set stores an item
Set(item *memcache.Item) error
// CompareAndSwap atomically updates an item only if it hasn't been
// modified since it was last read
CompareAndSwap(item *CASItem) error
// Delete removes an item by key
Delete(key string) error
// Close closes the client connection
Close() error
// Address returns the server address
Address() string
}
MemcachedClient defines the interface for memcached operations. This interface unifies basic operations and CAS (Compare-And-Swap) support.
type Option ¶
type Option func(*Client)
Option configures the client
func WithMaxIdleConns ¶
WithMaxIdleConns sets the maximum number of idle connections
func WithTimeout ¶
WithTimeout sets the connection and operation timeout