memcache

package
v0.0.0-...-4fbb213 Latest Latest
Warning

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

Go to latest
Published: May 5, 2022 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package memcache provides a client for the memcached cache server.

Index

Constants

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

Variables

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

	// ErrServerError 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")

	// ErrNoServers is returned when no servers are configured or available.
	ErrNoServers = errors.New("memcache: no servers configured or available")

	// ErrDumpDisable is returned when the "stats cachedump" and
	// "lru_crawler metadump" commands are disabled.
	ErrDumpDisable = errors.New("memcache: cachedump/metadump disabled")
)

Functions

This section is empty.

Types

type Client

type Client 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
	// contains filtered or unexported fields
}

Client is a memcache client. It is safe for unlocked use by multiple concurrent goroutines.

func New

func New(server ...string) *Client

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 NewFromSelector

func NewFromSelector(ss ServerSelector) *Client

NewFromSelector returns a new Client using the provided ServerSelector.

func (*Client) Add

func (c *Client) 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 (*Client) CompareAndSwap

func (c *Client) 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 (*Client) Decrement

func (c *Client) 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 (*Client) Delete

func (c *Client) 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 (*Client) DeleteAll

func (c *Client) DeleteAll() error

DeleteAll deletes all items in the cache.

func (*Client) FlushAll

func (c *Client) FlushAll() error

FlushAll Invalidates all existing cache items.

This command does not pause the server, as it returns immediately. It does not free up or flush memory at all, it just causes all items to expire.

func (*Client) Get

func (c *Client) 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 (*Client) GetMulti

func (c *Client) 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 (*Client) Increment

func (c *Client) 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 (*Client) Ping

func (c *Client) Ping() error

Ping checks all instances if they are alive. Returns error if any of them is down.

func (*Client) Replace

func (c *Client) Replace(item *Item) error

Replace writes the given item, but only if the server *does* already hold data for this key

func (*Client) Set

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

Set writes the given item, unconditionally.

func (*Client) StatsItemsServers

func (c *Client) StatsItemsServers(maxDumpKeys int) (servers map[net.Addr]*StatsItemsServer, err error)

StatsItemsServers returns information about item storage per slab class of all the servers, retrieved with the `stats items` command.

maxDumpKeys is the maximum number of keys that are going to be retrieved if maxDumpKeys < 0, doesn't dump the keys.

func (*Client) StatsServers

func (c *Client) StatsServers() (servers map[net.Addr]*ServerStats, err error)

StatsServers returns the general statistics of the servers retrieved with the `stats` command.

func (*Client) StatsSlabsServers

func (c *Client) StatsSlabsServers() (servers map[net.Addr]*StatsSlabsServer, err error)

StatsSlabsServers returns information about the storage per slab class of the servers, retrieved with the `stats slabs` command.

func (*Client) Touch

func (c *Client) 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. Zero means the item has no expiration time. ErrCacheMiss is returned if the key is not in the cache. The key must be at most 250 bytes in length.

type ConnectTimeoutError

type ConnectTimeoutError struct {
	Addr net.Addr
}

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 DumpKey

type DumpKey struct {
	// Key item key.
	Key string
	// Size item size (including key) in bytes.
	Size uint64
	// Expiration expiration time, as a unix timestamp.
	Expiration uint64
}

DumpKey information of a stored key.

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 ServerList

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

ServerList is a simple ServerSelector. Its zero value is usable.

func (*ServerList) Each

func (ss *ServerList) Each(f func(net.Addr) error) error

Each iterates over each server calling the given function

func (*ServerList) PickServer

func (ss *ServerList) PickServer(key string) (net.Addr, error)

func (*ServerList) SetServers

func (ss *ServerList) SetServers(servers ...string) error

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.

type ServerSelector

type ServerSelector interface {
	// PickServer returns the server address that a given item
	// should be shared onto.
	PickServer(key string) (net.Addr, error)
	Each(func(net.Addr) error) error
}

ServerSelector is the interface that selects a memcache server as a function of the item's key.

All ServerSelector implementations must be safe for concurrent use by multiple goroutines.

type ServerStats

type ServerStats struct {
	// ServerErr error if can't get the stats information.
	ServerErr error
	// Errs contains the errors that occurred while parsing the response.
	Errs errorsSlice
	// UnknownStats contains the stats that are not in the struct. This can be useful if the memcached developers add new stats in newer versions.
	UnknownStats map[string]string

	// Version version string of this server.
	Version string
	// AcceptingConns whether or not server is accepting conns.
	AcceptingConns bool
	// HashIsExpanding indicates if the hash table is being grown to a new size.
	HashIsExpanding bool
	// SlabReassignRunning if a slab page is being moved.
	SlabReassignRunning bool
	// Pid process id of this server process.
	Pid uint32
	// Uptime number of secs since the server started.
	Uptime uint32
	// Time current UNIX time according to the server.
	Time uint32
	// RusageUser accumulated user time for this process (seconds:microseconds).
	RusageUser float64
	// RusageSystem accumulated system time for this process (seconds:microseconds).
	RusageSystem float64
	// MaxConnections max number of simultaneous connections.
	MaxConnections uint32
	// CurrConnections number of open connections.
	CurrConnections uint32
	// TotalConnections total number of connections opened since the server started running.
	TotalConnections uint32
	// ConnectionStructures number of connection structures allocated by the server.
	ConnectionStructures uint32
	// ReservedFds number of misc fds used internally.
	ReservedFds uint32
	// Threads number of worker threads requested. (see doc/threads.txt).
	Threads uint32
	// HashPowerLevel current size multiplier for hash table.
	HashPowerLevel uint32
	// SlabGlobalPagePool slab pages returned to global pool for reassignment to other slab classes.
	SlabGlobalPagePool uint32
	// PointerSize default size of pointers on the host OS (generally 32 or 64).
	PointerSize uint64
	// CurrItems current number of items stored.
	CurrItems uint64
	// TotalItems total number of items stored since the server started.
	TotalItems uint64
	// Bytes current number of bytes used to store items.
	Bytes uint64
	// RejectedConnections conns rejected in maxconns_fast mode.
	RejectedConnections uint64
	// CmdGet cumulative number of retrieval reqs.
	CmdGet uint64
	// CmdSet cumulative number of storage reqs.
	CmdSet uint64
	// CmdFlush cumulative number of flush reqs.
	CmdFlush uint64
	// CmdTouch cumulative number of touch reqs.
	CmdTouch uint64
	// GetHits number of keys that have been requested and found present.
	GetHits uint64
	// GetMisses number of items that have been requested and not found.
	GetMisses uint64
	// GetExpired number of items that have been requested but had already expired.
	GetExpired uint64
	// GetFlushed number of items that have been requested but have been flushed via flush_all.
	GetFlushed uint64
	// DeleteMisses number of deletions reqs for missing keys.
	DeleteMisses uint64
	// DeleteHits number of deletion reqs resulting in an item being removed.
	DeleteHits uint64
	// IncrMisses number of incr reqs against missing keys.
	IncrMisses uint64
	// IncrHits number of successful incr reqs.
	IncrHits uint64
	// DecrMisses number of decr reqs against missing keys.
	DecrMisses uint64
	// DecrHits number of successful decr reqs.
	DecrHits uint64
	// CasMisses number of CAS reqs against missing keys.
	CasMisses uint64
	// CasHits number of successful CAS reqs.
	CasHits uint64
	// CasBadval number of CAS reqs for which a key was found, but the CAS value did not match.
	CasBadval uint64
	// TouchHits number of keys that have been touched with a new expiration time.
	TouchHits uint64
	// TouchMisses number of items that have been touched and not found.
	TouchMisses uint64
	// AuthCmds number of authentication commands handled, success or failure.
	AuthCmds uint64
	// AuthErrors number of failed authentications.
	AuthErrors uint64
	// IdleKicks number of connections closed due to reaching their idle timeout.
	IdleKicks uint64
	// Evictions number of valid items removed from cache to free memory for new items.
	Evictions uint64
	// Reclaimed number of times an entry was stored using memory from an expired entry.
	Reclaimed uint64
	// BytesRead total number of bytes read by this server from network.
	BytesRead uint64
	// BytesWritten total number of bytes sent by this server to network.
	BytesWritten uint64
	// LimitMaxbytes number of bytes this server is allowed to use for storage.
	LimitMaxbytes uint64
	// ListenDisabledNum number of times server has stopped accepting new connections (maxconns).
	ListenDisabledNum uint64
	// TimeInListenDisabledUs number of microseconds in maxconns.
	TimeInListenDisabledUs uint64
	// ConnYields number of times any connection yielded to another due to hitting the -R limit.
	ConnYields uint64
	// HashBytes bytes currently used by hash tables.
	HashBytes uint64
	// ExpiredUnfetched items pulled from LRU that were never touched by get/incr/append/etc before expiring.
	ExpiredUnfetched uint64
	// EvictedUnfetched items evicted from LRU that were never touched by get/incr/append/etc.
	EvictedUnfetched uint64
	// EvictedActive items evicted from LRU that had been hit recently but did not jump to top of LRU.
	EvictedActive uint64
	// SlabsMoved total slab pages moved.
	SlabsMoved uint64
	// CrawlerReclaimed total items freed by LRU Crawler.
	CrawlerReclaimed uint64
	// CrawlerItemsChecked total items examined by LRU Crawler.
	CrawlerItemsChecked uint64
	// LrutailReflocked times LRU tail was found with active ref. Items can be evicted to avoid OOM errors.
	LrutailReflocked uint64
	// MovesToCold items moved from HOT/WARM to COLD LRU's.
	MovesToCold uint64
	// MovesToWarm items moved from COLD to WARM LRU.
	MovesToWarm uint64
	// MovesWithinLru items reshuffled within HOT or WARM LRU's.
	MovesWithinLru uint64
	// DirectReclaims times worker threads had to directly reclaim or evict items.
	DirectReclaims uint64
	// LruCrawlerStarts times an LRU crawler was started.
	LruCrawlerStarts uint64
	// LruMaintainerJuggles number of times the LRU bg thread woke up.
	LruMaintainerJuggles uint64
	// SlabReassignRescues items rescued from eviction in page move.
	SlabReassignRescues uint64
	// SlabReassignEvictionsNomem valid items evicted during a page move (due to no free memory in slab).
	SlabReassignEvictionsNomem uint64
	// SlabReassignChunkRescues individual sections of an item rescued during a page move.
	SlabReassignChunkRescues uint64
	// SlabReassignInlineReclaim internal stat counter for when the page mover clears memory from the chunk freelist when it wasn't expecting to.
	SlabReassignInlineReclaim uint64
	// SlabReassignBusyItems items busy during page move, requiring a retry before page can be moved.
	SlabReassignBusyItems uint64
	// SlabReassignBusyDeletes items busy during page move, requiring deletion before page can be moved.
	SlabReassignBusyDeletes uint64
	// LogWorkerDropped logs a worker never wrote due to full buf.
	LogWorkerDropped uint64
	// LogWorkerWritten logs written by a worker, to be picked up.
	LogWorkerWritten uint64
	// LogWatcherSkipped logs not sent to slow watchers.
	LogWatcherSkipped uint64
	// LogWatcherSent logs written to watchers.
	LogWatcherSent uint64
	// Libevent libevent string version.
	Libevent string
	// LruCrawlerRunning crawl in progress.
	LruCrawlerRunning bool
	// MallocFails number of malloc fails.
	MallocFails uint64
	// LruBumpsDropped lru total bumps dropped.
	LruBumpsDropped uint64
}

ServerStats contains the general statistics from one server.

type StatsItems

type StatsItems struct {
	// Errs contains the errors that occurred while parsing the response.
	Errs errorsSlice
	// Keys retrieved dumped keys.
	Keys []DumpKey
	// UnknownStats contains the stats that are not in the struct. This can be useful if the memcached developers add new stats in newer versions.
	UnknownStats map[string]string

	// Number number of items presently stored in this class. Expired items are not automatically excluded.
	Number uint64
	// NumberHot number of items presently stored in the HOT LRU.
	NumberHot uint64
	// NumberWarm number of items presently stored in the WARM LRU.
	NumberWarm uint64
	// NumberCold number of items presently stored in the COLD LRU.
	NumberCold uint64
	// NumberTemp number of items presently stored in the TEMPORARY LRU.
	NumberTemp uint64
	// AgeHot age of the oldest item in HOT LRU.
	AgeHot uint64
	// AgeWarm age of the oldest item in WARM LRU.
	AgeWarm uint64
	// Age age of the oldest item in the LRU.
	Age uint64
	// Evicted number of times an item had to be evicted from the LRU  before it expired.
	Evicted uint64
	// EvictedNonzero number of times an item which had an explicit expire time set had to be evicted from the LRU before it expired.
	EvictedNonzero uint64
	// EvictedTime seconds since the last access for the most recent item evicted from this class. Use this to judge how recently active your evicted data is.
	EvictedTime uint64
	// Outofmemory number of times the underlying slab class was unable to store a new item. This means you are running with -M or an eviction failed.
	Outofmemory uint64
	// Tailrepairs number of times we self-healed a slab with a refcount leak. If this counter is increasing a lot, please report your situation to the developers.
	Tailrepairs uint64
	// Reclaimed number of times an entry was stored using memory from an expired entry.
	Reclaimed uint64
	// ExpiredUnfetched number of expired items reclaimed from the LRU which were never touched after being set.
	ExpiredUnfetched uint64
	// EvictedUnfetched number of valid items evicted from the LRU which were never touched after being set.
	EvictedUnfetched uint64
	// EvictedActive number of valid items evicted from the LRU which were recently touched but were evicted before being moved to the top of the LRU again.
	EvictedActive uint64
	// CrawlerReclaimed number of items freed by the LRU Crawler.
	CrawlerReclaimed uint64
	// LrutailReflocked number of items found to be refcount locked in the LRU tail.
	LrutailReflocked uint64
	// MovesToCold number of items moved from HOT or WARM into COLD.
	MovesToCold uint64
	// MovesToWarm number of items moved from COLD to WARM.
	MovesToWarm uint64
	// MovesWithinLru number of times active items were bumped within HOT or WARM.
	MovesWithinLru uint64
	// DirectReclaims number of times worker threads had to directly pull LRU tails to find memory for a new item.
	DirectReclaims uint64
	// HitsToHot number of keys that have been requested and found present in the HOT LRU.
	HitsToHot uint64
	// HitsToWarm number of keys that have been requested and found present in the WARM LRU.
	HitsToWarm uint64
	// HitsToCold number of keys that have been requested and found present in the COLD LRU.
	HitsToCold uint64
	// HitsToTemp number of keys that have been requested and found present in each sub-LRU.
	HitsToTemp uint64
}

StatsItems information of the items in the slab.

type StatsItemsServer

type StatsItemsServer struct {
	// ServerErr irror if can't get the stats information.
	ServerErr error
	// StatsItemsSlabs statistics for each slab.
	StatsItemsSlabs map[string]*StatsItems
	// DumpErr error if can't cachedump.
	DumpErr error
}

StatsItemsServer information about item storage per slab class.

type StatsSlab

type StatsSlab struct {
	// Errs contains the errors that occurred while parsing the response.
	Errs errorsSlice
	// UnknownStats contains the stats that are not in the struct. This can be useful if the memcached developers add new stats in newer versions.
	UnknownStats map[string]string

	// ChunkSize the amount of space each chunk uses. One item will use one chunk of the appropriate size.
	ChunkSize uint64
	// ChunksPerPage how many chunks exist within one page. A page by default is less than or equal to one megabyte in size. Slabs are allocated by page, then broken into chunks.
	ChunksPerPage uint64
	// TotalPages total number of pages allocated to the slab class.
	TotalPages uint64
	// TotalChunks total number of chunks allocated to the slab class.
	TotalChunks uint64
	// GetHits total number of get requests serviced by this class.
	GetHits uint64
	// CmdSet total number of set requests storing data in this class.
	CmdSet uint64
	// DeleteHits total number of successful deletes from this class.
	DeleteHits uint64
	// IncrHits total number of incrs modifying this class.
	IncrHits uint64
	// DecrHits total number of decrs modifying this class.
	DecrHits uint64
	// CasHits total number of CAS commands modifying this class.
	CasHits uint64
	// CasBadval total number of CAS commands that failed to modify a value due to a bad CAS id.
	CasBadval uint64
	// TouchHits total number of touches serviced by this class.
	TouchHits uint64
	// UsedChunks how many chunks have been allocated to items.
	UsedChunks uint64
	// FreeChunks chunks not yet allocated to items, or freed via delete.
	FreeChunks uint64
	// FreeChunksEnd number of free chunks at the end of the last allocated page.
	FreeChunksEnd uint64
	// MemRequested number of bytes requested to be stored in this slab[*].
	MemRequested uint64
	// ActiveSlabs total number of slab classes allocated.
	ActiveSlabs uint64
	// TotalMalloced total amount of memory allocated to slab pages.
	TotalMalloced uint64
}

StatsSlab statistics for one slab.

type StatsSlabsServer

type StatsSlabsServer struct {
	// ServerErr error if can't get the stats information.
	ServerErr error
	// StatsSlabs statistics for each slab.
	StatsSlabs map[string]*StatsSlab
	// ActiveSlabs total number of slab classes allocated.
	ActiveSlabs uint64
	// TotalMalloced total amount of memory allocated to slab pages.
	TotalMalloced uint64
}

StatsSlabsServer information broken down by slab about items stored in memcached, more centered to performance of a slab rather than counts of particular items.

Jump to

Keyboard shortcuts

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