memcache

package module
v0.0.0-...-2491910 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2021 License: MIT Imports: 12 Imported by: 0

README

memcache

轻量 memcache 客户端

支持传入 ctx 对象。纯单机版 SDK,集群相关内容请使用 twemproxy 等中间件。

Documentation

Overview

Package memcache provides a client for the memcached cache server.

Index

Constants

This section is empty.

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

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

Functions

func IsResumableErr

func IsResumableErr(err error) bool

IsResumableErr returns true if err is only a protocol-level cache error. This is used to determine whether or not a server connection should be re-used or not. If an error occurs, by default we don't reuse the connection, unless it was just a cache error.

Types

type Client

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

Client memcache client

func New

func New(addr string, initialCap int, maxCap int) (*Client, error)

New init client

func (*Client) Add

func (c *Client) Add(ctx context.Context, item *Item) error

Add only set new key

func (*Client) Close

func (c *Client) Close()

Close close all connection

func (*Client) CompareAndSwap

func (c *Client) CompareAndSwap(ctx context.Context, item *Item) error

CompareAndSwap cas set

func (*Client) Decrement

func (c *Client) Decrement(ctx context.Context, key string, delta uint64) (d uint64, err error)

Decrement decr key

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, key string) error

Delete delete key

func (*Client) Get

func (c *Client) Get(ctx context.Context, key string) (i *Item, err error)

Get get one key

func (*Client) GetMulti

func (c *Client) GetMulti(ctx context.Context, keys []string) (is map[string]*Item, err error)

GetMulti get multi keys

func (*Client) Increment

func (c *Client) Increment(ctx context.Context, key string, delta uint64) (d uint64, err error)

Increment incr key

func (*Client) PoolStats

func (c *Client) PoolStats() *pool.Stats

PoolStats 返回连接池状态

func (*Client) Replace

func (c *Client) Replace(ctx context.Context, item *Item) error

Replace set old key

func (*Client) Set

func (c *Client) Set(ctx context.Context, item *Item) error

Set set key

func (*Client) Touch

func (c *Client) Touch(ctx context.Context, key string, seconds int32) error

Touch change ttl

type Conn

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

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

func NewConn

func NewConn(c net.Conn) *Conn

NewConn create a new memcache connection.

func (*Conn) Add

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

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

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

func (c *Conn) 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 (*Conn) FlushAll

func (c *Conn) FlushAll() error

FlushAll clear all item

func (*Conn) Get

func (c *Conn) Get(key string) (*Item, 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 (*Conn) GetMulti

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

func (c *Conn) 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 (*Conn) Replace

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

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

func (*Conn) Set

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

Set writes the given item, unconditionally.

func (*Conn) Touch

func (c *Conn) 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. ErrCacheMiss is returned if the key is not in the cache. The key must be at most 250 bytes in length.

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.

Jump to

Keyboard shortcuts

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