google.golang.org/appengine/memcache 包 (v1.6.8)

Memcache 软件包为 App Engine 的小型随机数据块的分布式内存键值存储提供一个客户端。

基本操作包括获取和设置项,以及按字符串排列。

item0, err := memcache.Get(c, "key")
if err != nil && err != memcache.ErrCacheMiss {
    return err
}
if err == nil {
    fmt.Fprintf(w, "memcache hit: Key=%q Val=[% x]\n", item0.Key, item0.Value)
} else {
    fmt.Fprintf(w, "memcache miss\n")
}

item1 := &memcache.Item{
    Key:   "foo",
    Value: []byte("bar"),
}
if err := memcache.Set(c, item1); err != nil {
    return err
}

变量

ErrCacheMiss、ErrCASConflict、ErrNoStats、ErrNotStored、ErrServerError

var (
	// ErrCacheMiss means that an operation 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")
	// ErrNoStats means that no statistics were available.
	ErrNoStats = errors.New("memcache: no statistics available")
	// 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")
)

Gob、JSON

var (
	// Gob is a Codec that uses the gob package.
	Gob = Codec{gobMarshal, gobUnmarshal}
	// JSON is a Codec that uses the json package.
	JSON = Codec{json.Marshal, json.Unmarshal}
)

函数

func Add

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

Add 在给定项的键不存在现有值的情况下写入该给定项。如果不满足此条件,则返回 ErrNotStored。

func AddMulti

func AddMulti(c context.Context, item []*Item) error

AddMulti 是 Add 的批处理版本。系统可能会返回 appengine.MultiError。

func CompareAndSwap

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

CompareAndSwap 写入之前由 Get 返回的给定项,前提是其值在 Get 与 CompareAndSwap 调用之间未被修改或清除。在两次调用之间,该项的键不应发生变化,但该项的其他所有字段均可变化。如果该值在两次调用之间发生修改,则返回 ErrCASConflict。如果在两次调用之间清除了该值,则返回 ErrNotStored。

func CompareAndSwapMulti

func CompareAndSwapMulti(c context.Context, item []*Item) error

CompareAndSwapMulti 是 CompareAndSwap 的批处理版本。系统可能会返回 appengine.MultiError。

func Delete

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

Delete 会删除给定键的项。如果找不到指定的项,则返回 ErrCacheMiss。键的最大长度为 250 字节。

func DeleteMulti

func DeleteMulti(c context.Context, key []string) error

DeleteMulti 是 Delete 的批处理版本。如果找不到任何键,则返回 appengine.MultiError。每个键的最大长度为 250 字节。

func Flush

func Flush(c context.Context) error

Flush 会清空 memcache 中的所有项。

func GetMulti

func GetMulti(c context.Context, key []string) (map[string]*Item, error)

GetMulti 是 Get 的批处理版本。由于存在 memcache 缓存未命中的情况,因此所返回的从键到项的映射所包含的元素可能会比输入切片的元素少。每个键的最大长度为 250 字节。

func Increment

func Increment(c context.Context, key string, delta int64, initialValue uint64) (newValue uint64, err error)

Increment 以原子方式自增给定键的十进制值并返回新值。该值必须适合 uint64。溢出会发生截断,下溢上限为零。提供的增量可能是负数。如果键在 memcache 中不存在,则提供的初始值用于在应用增量之前以原子方式填充它。键的最大长度为 250 字节。

func IncrementExisting

func IncrementExisting(c context.Context, key string, delta int64) (newValue uint64, err error)

IncrementExisting 的工作方式类似于 Increment,但前提是键已经存在于 memcache 中,并且不采用初始值。如果计算初始值代价昂贵,使用 IncrementExisting 可以节省工作量。如果找不到指定的项,则系统会返回错误。

func PeekMulti

func PeekMulti(c context.Context, key []string) (map[string]*Item, error)

PeekMulti 是 Peek 的批处理版本。它类似于 GetMulti,但另外填充了 Item.Timestamps。

func Set

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

Set 可无条件地写入给定项。

func SetMulti

func SetMulti(c context.Context, item []*Item) error

SetMulti 是 Set 的批处理版本。系统可能会返回 appengine.MultiError。

编解码器

type Codec struct {
	Marshal   func(interface{}) ([]byte, error)
	Unmarshal func([]byte, interface{}) error
}

Codec 表示实现编解码器的对称函数对。使用 Codec 存储到 memcache 或从 memcache 检索的项会对其值编组或取消编组。

为 Codec 提供的所有方法都与具有相同名称的包级别函数类似。

func (Codec) Add

func (cd Codec) Add(c context.Context, item *Item) error

Add 在给定项的键不存在现有值的情况下写入该给定项。如果不满足此条件,则返回 ErrNotStored。

func (Codec) AddMulti

func (cd Codec) AddMulti(c context.Context, items []*Item) error

AddMulti 是 Add 的批处理版本。系统可能会返回 appengine.MultiError。

func (Codec) CompareAndSwap

func (cd Codec) CompareAndSwap(c context.Context, item *Item) error

CompareAndSwap 写入之前由 Get 返回的给定项,前提是其值在 Get 与 CompareAndSwap 调用之间未被修改或清除。在两次调用之间,该项的键不应发生变化,但该项的其他所有字段均可变化。如果该值在两次调用之间发生修改,则返回 ErrCASConflict。如果在两次调用之间清除了该值,则返回 ErrNotStored。

func (Codec) CompareAndSwapMulti

func (cd Codec) CompareAndSwapMulti(c context.Context, items []*Item) error

CompareAndSwapMulti 是 CompareAndSwap 的批处理版本。系统可能会返回 appengine.MultiError。

func (Codec) Get

func (cd Codec) Get(c context.Context, key string, v interface{}) (*Item, error)

Get 会获取给定键的项并将获取的值解码为 v。如果出现 memcache 缓存未命中,则系统会返回 ErrCacheMiss。键的长度不得超过 250 个字节。

func (Codec) Set

func (cd Codec) Set(c context.Context, item *Item) error

Set 可无条件地写入给定项。

func (Codec) SetMulti

func (cd Codec) SetMulti(c context.Context, items []*Item) error

SetMulti 是 Set 的批处理版本。系统可能会返回 appengine.MultiError。

资源项

type Item struct {
	// Key is the Item's key (250 bytes maximum).
	Key string
	// Value is the Item's value.
	Value []byte
	// Object is the Item's value for use with a Codec.
	Object interface{}
	// Flags are server-opaque flags whose semantics are entirely up to the
	// App Engine app.
	Flags uint32
	// Expiration is the maximum duration that the item will stay
	// in the cache.
	// The zero value means the Item has no expiration time.
	// Subsecond precision is ignored.
	// This is not set when getting items.
	Expiration time.Duration

	// ItemTimestamps are server values only returned when calling Peek and PeekMulti.
	// The timestamps are nil when calling Get and GetMulti.
	Timestamps ItemTimestamps
	// contains filtered or unexported fields
}

Item 是内存缓存 get 和 set 操作的单位。

func Get

func Get(c context.Context, key string) (*Item, error)

Get 为给定键获取项。如果出现 memcache 缓存未命中,则系统会返回 ErrCacheMiss。键的长度不得超过 250 个字节。

func Peek

func Peek(c context.Context, key string) (*Item, error)

Peek 会获取给定键的项,并另外填充 Item.Timestamps。如果出现 memcache 缓存未命中,则系统会返回 ErrCacheMiss。键的长度不得超过 250 个字节。

ItemTimestamps

type ItemTimestamps struct {
	// Expiration is related to Item.Expiration but it is a Time (not a Duration),
	// provided by the server. It is not meant to be set by the user.
	Expiration *time.Time
	// LastAccess is the last time the Item was accessed.
	LastAccess *time.Time
}

ItemTimestamps 是服务器可以选择提供的时间戳。请参阅 Peek 和 PeekMulti。

统计信息

type Statistics struct {
	Hits     uint64 // Counter of cache hits
	Misses   uint64 // Counter of cache misses
	ByteHits uint64 // Counter of bytes transferred for gets

	Items uint64 // Items currently in the cache
	Bytes uint64 // Size of all items currently in the cache

	Oldest int64 // Age of access of the oldest item, in seconds
}

Statistics 表示有关 memcache 缓存的一组统计信息。这可能包括已过期但尚未从缓存中移除的项。

func Stats

func Stats(c context.Context) (*Statistics, error)

Stats 检索当前内存缓存统计信息。