client

package
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package client provides Memcached client functionality including connection handling, key enumeration, CAS operations, and server capability detection.

Index

Constants

View Source
const MinRequiredVersion = "1.4.31"

MinRequiredVersion is the minimum supported Memcached version

Variables

View Source
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

func IsCASConflict(err error) bool

IsCASConflict checks if the error is a CAS conflict error.

func IsCacheMiss

func IsCacheMiss(err error) bool

IsCacheMiss checks if the error is a cache miss error.

func IsVersionSupported

func IsVersionSupported(version string) bool

IsVersionSupported checks if the version meets minimum requirements (1.4.31+)

func ParseVersion

func ParseVersion(version string) (major, minor, patch int, err error)

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

func NewCASItem(key string, value []byte, flags uint32, expiration int32, cas uint64) *CASItem

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

func (item *CASItem) ToMemcacheItem() *memcache.Item

ToMemcacheItem converts a CASItem to a gomemcache Item. Note: The CAS value is handled separately as gomemcache uses internal casid field.

type Capability

type Capability struct {
	Version          string
	SupportsMetadump bool
}

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 New

func New(addr string, opts ...Option) (*Client, error)

New creates a new Memcached client

func (*Client) Address

func (c *Client) Address() string

Address returns the server address

func (*Client) Close

func (c *Client) Close() error

Close closes the client connection

func (*Client) CompareAndSwap

func (c *Client) CompareAndSwap(item *CASItem) error

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:

  1. Call GetWithCAS to get the current value and CAS token
  2. Modify the Value field of the returned CASItem
  3. 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) Delete

func (c *Client) Delete(key string) error

Delete removes an item by key

func (*Client) Get

func (c *Client) Get(key string) (*memcache.Item, error)

Get retrieves an item by key

func (*Client) GetWithCAS

func (c *Client) GetWithCAS(key string) (*CASItem, error)

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) Ping

func (c *Client) Ping(ctx context.Context) error

Ping checks if the server is reachable by executing a "version" command

func (*Client) RawCommand

func (c *Client) RawCommand(ctx context.Context, cmd string) ([]string, error)

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) Set

func (c *Client) Set(item *memcache.Item) error

Set stores an item

func (*Client) SetWithExpiration

func (c *Client) SetWithExpiration(key string, value []byte, flags uint32, expiration int32) error

SetWithExpiration is a convenience method to set a key with expiration. This is useful for setting up test data before CAS operations.

func (*Client) Stats

func (c *Client) Stats() (map[string]string, error)

Stats returns server statistics

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

func (e *KeyEnumerator) EnumerateAll(ctx context.Context) ([]models.KeyInfo, error)

EnumerateAll collects all keys and returns them as a slice

func (*KeyEnumerator) EnumerateStream

func (e *KeyEnumerator) EnumerateStream(ctx context.Context) (<-chan models.KeyInfo, <-chan error)

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

func WithMaxIdleConns(n int) Option

WithMaxIdleConns sets the maximum number of idle connections

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the connection and operation timeout

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL