Documentation
¶
Overview ¶
Package memc provides a modern memcached client for Go.
Index ¶
- Variables
- func Add[T any](c *Client, key string, item T, opts ...Option) error
- func AddMulti[T any](c *Client, items []*Pair[string, T], opts ...Option) error
- func Append[T any](c *Client, key string, item T, opts ...Option) error
- func CompareAndSwap[T any](c *Client, key string, cas CAS, item T, opts ...Option) error
- func Decrement[T Countable](c *Client, key string, delta T) (T, error)
- func Delete(c *Client, key string) error
- func Flush(c *Client, timeout time.Duration) error
- func Get[T any](c *Client, key string) (T, error)
- func Increment[T Countable](c *Client, key string, delta T) (T, error)
- func Prepend[T any](c *Client, key string, item T, opts ...Option) error
- func Replace[T any](c *Client, key string, item T, opts ...Option) error
- func Set[T any](c *Client, key string, item T, opts ...Option) error
- func SetMulti[T any](c *Client, items []*Pair[string, T], opts ...Option) error
- type CAS
- type Client
- type ClientOption
- type ClockFunc
- type Countable
- type ItemStatistics
- type Option
- type Options
- type Pair
- type Slab
- type SlabStatistics
- type Statistics
Constants ¶
This section is empty.
Variables ¶
var ( ErrCacheMiss = errors.New("memc: cache miss") ErrKeyNotValid = errors.New("memc: key is not valid") ErrNotStored = errors.New("memc: item not stored") ErrNotFound = errors.New("memc: item not found") ErrConflict = errors.New("memc: CAS conflict") ErrExpiration = errors.New("memc: expiration ttl is not valid") ErrClientClosed = errors.New("memc: client has been closed") ErrNegativeInc = errors.New("memc: increment delta must be non-negative") ErrNonNumeric = errors.New("memc: cannot increment non-numeric value") ErrCommandIssue = errors.New("memc: got command error response") )
Functions ¶
func Add ¶
Add will store the item using the given key, but only if no item currently exists. New items are at the top of the LRU.
Uses Client c to connect to a memcached instance, and automatically handles connection pooling and reuse.
One or more Option(s) may be applied to configure things such as the value expiration TTL or its associated flags.
func AddMulti ¶ added in v0.3.0
AddMulti will store each item in items using the item's associated key, but only if the item does not currently exist. New items are at the top of the LRU.
Errors are accumulated using errors.Join.
Uses Client c to connect to a memcached instance, and automatically handles connection pooling and reuse.
One or more Option(s) may be applied to configure things such as the value expiration TTL or its associated flags.
func Append ¶ added in v0.5.0
Append will append the given value to the value associated with the given key.
Append differs from Set in that it is meant to add additional data to an existing key, rather than replace the existing value entirely. The key must already exist.
Uses Client c to connect to a memcached instance, and automatically handles connection pooling and reuse.
One or more Option(s) may be applied to configure things such as the value expiration TTL or its associated flags.
func CompareAndSwap ¶ added in v0.5.0
CompareAndSwap will store the item using the given key, but only if the CAS token matches the current value's CAS token. This provides atomic compare-and-swap functionality for optimistic locking.
If the key does not exist, ErrNotFound is returned.
If the CAS token does not match (meaning the value was modified since it was retrieved with Gets), ErrConflict is returned.
Uses Client c to connect to a memcached instance, and automatically handles connection pooling and reuse.
One or more Option(s) may be applied to configure things such as the value expiration TTL or its associated flags.
func Decrement ¶
Decrement will decrement the value associated with the given key by delta.
Note: the value must be an ASCII integer. It must have been initially stored as a string value, e.g. by using Set. The delta value must be positive.
Set(client, "counter", "100") Decrement(client, "counter", 1) // counter = 99
func Delete ¶
Delete will remove the value associated with key from memcached.
Uses Client c to connect to a memcached instance, and automatically handles connection pooling and reuse.
func Flush ¶ added in v0.5.0
Flush will delete all items from memcached.
The timeout parameter is optional. A timeout of 0 means flush right now. A non-zero timeout will delay the flush by that many seconds.
Note: this operation is performed on a single memcached server, even when the Client is configured with multiple server addresses. This is intentional, as flush is typically used by local administration tools that connect to a single memcached instance.
func Get ¶
Get the value associated with the given key.
Uses Client c to connect to a memcached instance, and automatically handles connection pooling and reuse.
func Increment ¶
Increment will increment the value associated with the given key by delta.
Note: the value must be an ASCII integer. It must have been initially stored as a string value, e.g. by using Set. The delta value must be positive.
Set(client, "counter", "100") Increment(client, "counter", 1) // counter = 101
func Prepend ¶ added in v0.5.0
Prepend will prepend the given value to the value associated with the given key.
Prepend differs from Set in that it is meant to add additional data to an existing key, rather than replace the existing value entirely. The key must already exist.
Uses Client c to connect to a memcached instance, and automatically handles connection pooling and reuse.
One or more Option(s) may be applied to configure things such as the value expiration TTL or its associated flags.
func Replace ¶ added in v0.5.0
Replace will store the item using the given key, but only if the key already exists. New items are at the top of the LRU.
Uses Client c to connect to a memcached instance, and automatically handles connection pooling and reuse.
One or more Option(s) may be applied to configure things such as the value expiration TTL or its associated flags.
func Set ¶
Set will store the item using the given key, possibly overwriting any existing data. New items are at the top of the LRU.
Uses Client c to connect to a memcached instance, and automatically handles connection pooling and reuse.
One or more Option(s) may be applied to configure things such as the value expiration TTL or its associated flags.
func SetMulti ¶ added in v0.3.0
SetMulti will store each item in items using the item's associated key, possibly overwritting any existing data. New items are at the top of the LRU.
Errors are accumulated using errors.Join.
Uses Client c to connect to a memcached instance, and automatically handles connection pooling and reuse.
One or more Option(s) may be applied to configure things such as the value expiration TTL or its associated flags.
Types ¶
type CAS ¶ added in v0.5.0
type CAS uint64
CAS represents a Compare-And-Swap token used for optimistic locking. It is returned by Gets and must be provided to CompareAndSwap to atomically update a value.
func Gets ¶ added in v0.5.0
Gets the value associated with the given key, along with its CAS token.
The CAS token can be used with CompareAndSwap to atomically update the value, providing optimistic locking. If the value has been modified since it was retrieved, CompareAndSwap will return an ErrConflict error.
Uses Client c to connect to a memcached instance, and automatically handles connection pooling and reuse.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
A Client is used for making network requests to memcached instances.
Use the package functions Set, Get, Delete, etc. by providing this Client to manage data in memcached.
func New ¶
func New(instances []string, opts ...ClientOption) *Client
New creates a new Client capable of sharding across the given set of instances and pooling connections to each instance.
Certain behaviors can be configured by specifying one or more ClientOption options.
type ClientOption ¶
type ClientOption func(c *Client)
func SetClock ¶ added in v0.5.0
func SetClock(f ClockFunc) ClientOption
SetClock sets the ClockFunc used for getting the current time.
If unset the default is to use the time.Now function.
Note this should typically only be set in testing.
func SetDefaultTTL ¶
func SetDefaultTTL(expiration time.Duration) ClientOption
SetDefaultTTL adjusts the default expiration time of values set into the memcached instance(s).
If unset the default expiration TTL is 1 hour.
The expiration time must be more than 1 second, or set to 0 to indicate no expiration time (and values stay in the cache indefinitely).
func SetDialTimeout ¶
func SetDialTimeout(timeout time.Duration) ClientOption
SetDialTimeout adjusts the amount of time to wait on establishing a TCP connection to the memached instance(s).
If unset the default timeout is 5 seconds.
func SetIdleConnections ¶
func SetIdleConnections(count int) ClientOption
SetIdleConnections adjusts the maximum number of idle connections to maintain for each memcached instance.
If unset the default idle connection limit is 1.
Note that idle connections are created on demand, not at startup.
type ClockFunc ¶ added in v0.5.0
ClockFunc is a function that returns the current time.
Normally this should just be the time.Now function.
type Countable ¶
Countable represents types that work with Increment and Decrement operations.
Note: memcached does not allow negative values for either operation.
type ItemStatistics ¶ added in v0.5.0
type ItemStatistics struct {
Class int `json:"slab_class"`
Number int `json:"number"`
NumberHot int `json:"number_hot"`
NumberWarm int `json:"number_warm"`
NumberCold int `json:"number_cold"`
AgeHot int `json:"age_hot"`
AgeWarm int `json:"age_warm"`
Age int `json:"age"`
MemRequested int `json:"mem_requested"`
Evicted int `json:"evicted"`
EvictedNonZero int `json:"evicted_nonzero"`
EvictedTime int `json:"evicted_time"`
OutOfMemory int `json:"outofmemory"`
TailRepairs int `json:"tailrepairs"`
Reclaimed int `json:"reclaimed"`
ExpiredUnfetched int `json:"expired_unfetched"`
EvictedUnfetched int `json:"evicted_unfetched"`
EvictedActive int `json:"evicted_active"`
CrawlerReclaimed int `json:"crawler_reclaimed"`
CrawlerItemsChecked int `json:"crawler_items_checked"`
LRUTailReflocked int `json:"lrutail_reflocked"`
MovesToCold int `json:"moves_to_cold"`
MovesToWarm int `json:"moves_to_warm"`
MovesWithinLRU int `json:"moves_within_lru"`
DirectReclaims int `json:"direct_reclaims"`
HitsToHot int `json:"hits_to_hot"`
HitsToWarm int `json:"hits_to_warm"`
HitsToCold int `json:"hits_to_cold"`
HitsToTemp int `json:"hits_to_temp"`
}
func StatsItems ¶ added in v0.5.0
func StatsItems(c *Client) ([]*ItemStatistics, error)
StatsItems returns item statistics for a single memcached server.
Note: this operation is performed on a single memcached server, even when the Client is configured with multiple server addresses. This is intentional, as stats is typically used by local monitoring tools that connect to a single memcached instance.
type Option ¶
type Option func(o *Options)
Option to apply when executing a verb like Get, Set, etc.
type Options ¶
type Options struct {
// contains filtered or unexported fields
}
Options contains configuration parameters that may be applied when executing a verb like Get, Set, etc.
type Pair ¶ added in v0.3.0
type Pair[T, U any] struct { A T B U }
A Pair associates two elements.
type Slab ¶ added in v0.5.0
type Slab struct {
Class int `json:"slab_class"`
ChunkSize int `json:"chunk_size"`
ChunksPerPage int `json:"chunks_per_page"`
TotalPages int `json:"total_pages"`
TotalChunks int `json:"total_chunks"`
UsedChunks int `json:"used_chunks"`
FreeChunks int `json:"free_chunks"`
FreeChunksEnd int `json:"free_chunks_end"`
GetHits int `json:"get_hits"`
CmdSet int `json:"cmd_set"`
DeleteHits int `json:"delete_hits"`
IncrementHits int `json:"incr_hits"`
DecrementHits int `json:"decr_hits"`
CASHits int `json:"cas_hits"`
CASBadVal int `json:"cas_badval"`
TouchHits int `json:"touch_hits"`
}
type SlabStatistics ¶ added in v0.5.0
type SlabStatistics struct {
ActiveSlabs int `json:"active_slabs"`
TotalMalloced int `json:"total_malloced"`
Slabs []*Slab `json:"slabs"`
}
func StatsSlabs ¶ added in v0.5.0
func StatsSlabs(c *Client) (*SlabStatistics, error)
StatsSlabs returns slab statistics for a single memcached server.
Note: this operation is performed on a single memcached server, even when the Client is configured with multiple server addresses. This is intentional, as stats is typically used by local monitoring tools that connect to a single memcached instance.
type Statistics ¶ added in v0.5.0
type Statistics struct {
Runtime struct {
PID int `json:"pid"`
Uptime int `json:"uptime"`
Time int `json:"time"`
Version string `json:"version"`
LibEvent string `json:"libevent"`
PointerSize int `json:"pointer_size"`
Threads int `json:"threads"`
}
Resources struct {
RUsageUser float64 `json:"rusage_user"`
RUsageSystem float64 `json:"rusage_system"`
}
Connections struct {
Max int `json:"max_connections"`
Current int `json:"curr_connections"`
Total int `json:"total_connections"`
Rejected int `json:"rejected_connections"`
Structures int `json:"connection_structures"`
}
Commands struct {
Get int `json:"cmd_get"`
Set int `json:"cmd_set"`
Flush int `json:"cmd_flush"`
Touch int `json:"cmd_touch"`
Meta int `json:"cmd_meta"`
Hit struct {
Get int `json:"get_hits"`
Delete int `json:"delete_hits"`
Increment int `json:"incr_hits"`
Decrement int `json:"decr_hits"`
Touch int `json:"touch_hits"`
CAS int `json:"cas_hits"`
}
Miss struct {
Get int `json:"get_misses"`
Delete int `json:"delete_misses"`
Increment int `json:"incr_misses"`
Decrement int `json:"decr_misses"`
Touch int `json:"touch_misses"`
CAS int `json:"cas_misses"`
}
Failure struct {
GetExpired int `json:"get_expired"`
GetFlushed int `json:"get_flushed"`
CASBadValue int `json:"cas_badval"`
}
}
Items struct {
Bytes int `json:"bytes"`
Current int `json:"curr_items"`
Total int `json:"total_items"`
}
}
func Stats ¶ added in v0.5.0
func Stats(c *Client) (*Statistics, error)
Stats returns runtime statistics for a single memcached server.
Note: this operation is performed on a single memcached server, even when the Client is configured with multiple server addresses. This is intentional, as stats is typically used by local monitoring tools that connect to a single memcached instance.