memcache

package
Version: v0.0.0-...-8b7da69 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2020 License: Apache-2.0 Imports: 4 Imported by: 54

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCacheMiss   = orig.ErrCacheMiss
	ErrCASConflict = orig.ErrCASConflict
	ErrNoStats     = orig.ErrNoStats
	ErrNotStored   = orig.ErrNotStored
	ErrServerError = orig.ErrServerError
)

These are pass-through versions from the managed-VM SDK. All implementations must return these (exact) errors (not just an error with the same text).

Functions

func Add

func Add(c context.Context, items ...Item) error

Add writes items to memcache iff they don't already exist.

If only one item is provided its error will be returned directly. If more than one item is provided, an errors.MultiError will be returned in the event of an error, with a given error index corresponding to the error encountered when processing the item at that index.

func AddRawFilters

func AddRawFilters(c context.Context, filts ...RawFilter) context.Context

AddRawFilters adds RawInterface filters to the context.

func CompareAndSwap

func CompareAndSwap(c context.Context, items ...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.

Example:

itm := memcache.NewItem(context, "aKey")
memcache.Get(context, itm) // check error
itm.SetValue(append(itm.Value(), []byte("more bytes")))
memcache.CompareAndSwap(context, itm) // check error

If only one item is provided its error will be returned directly. If more than one item is provided, an errors.MultiError will be returned in the event of an error, with a given error index corresponding to the error encountered when processing the item at that index.

func Delete

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

Delete deletes items from memcache.

If only one item is provided its error will be returned directly. If more than one item is provided, an errors.MultiError will be returned in the event of an error, with a given error index corresponding to the error encountered when processing the item at that index.

func Flush

func Flush(c context.Context) error

Flush dumps the entire memcache state.

func Get

func Get(c context.Context, items ...Item) error

Get retrieves items from memcache.

func Increment

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

Increment adds delta to the uint64 contained at key. If the memcache key is missing, it's populated with initialValue before applying delta (i.e. the final value would be initialValue+delta).

Underflow caps at 0, overflow wraps back to 0.

The value is stored as little-endian uint64, see "encoding/binary".LittleEndian. If the value is not exactly 8 bytes, it's assumed to contain non-number data and this method will return an error.

func IncrementExisting

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

IncrementExisting is like Increment, except that the value must exist already.

func Set

func Set(c context.Context, items ...Item) error

Set writes items into memcache unconditionally.

If only one item is provided its error will be returned directly. If more than one item is provided, an errors.MultiError will be returned in the event of an error, with a given error index corresponding to the error encountered when processing the item at that index.

func SetRaw

SetRaw sets the current RawInterface object in the context. Useful for testing with a quick mock. This is just a shorthand SetRawFactory invocation to SetRaw a RawFactory which always returns the same object.

func SetRawFactory

func SetRawFactory(c context.Context, mcf RawFactory) context.Context

SetRawFactory sets the function to produce RawInterface instances, as returned by the Get method.

Types

type Item

type Item interface {
	Key() string
	Value() []byte
	Flags() uint32
	Expiration() time.Duration

	SetKey(string) Item
	SetValue([]byte) Item
	SetFlags(uint32) Item
	SetExpiration(time.Duration) Item

	// SetAll copies all the values from other, except Key, into this item
	// (including the hidden CasID field). If other is nil, then all non-Key
	// fields are reset.
	SetAll(other Item)
}

Item is a wrapper around *memcache.Item. Note that the Set* methods all return the original Item (e.g. they mutate the original), due to implementation constraints. They return the original item to allow easy chaining, e.g.:

itm := memcache.NewItem(c, "foo").SetValue([]byte("stuff"))

func GetKey

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

GetKey is a convenience method for generating and retrieving an Item instance for the specified from memcache key.

On a cache miss ErrCacheMiss will be returned. Item will always be returned, even on a miss, but it's value may be empty if it was a miss.

func NewItem

func NewItem(c context.Context, key string) Item

NewItem creates a new, mutable, memcache item.

type RawCB

type RawCB func(error)

RawCB is a simple error callback for most of the methods in RawInterface.

type RawFactory

type RawFactory func(context.Context) RawInterface

RawFactory is the function signature for RawFactory methods compatible with SetRawFactory.

type RawFilter

type RawFilter func(context.Context, RawInterface) RawInterface

RawFilter is the function signature for a RawFilter MC implementation. It gets the current MC implementation, and returns a new MC implementation backed by the one passed in.

type RawInterface

type RawInterface interface {
	NewItem(key string) Item

	AddMulti(items []Item, cb RawCB) error
	SetMulti(items []Item, cb RawCB) error
	GetMulti(keys []string, cb RawItemCB) error
	DeleteMulti(keys []string, cb RawCB) error
	CompareAndSwapMulti(items []Item, cb RawCB) error

	Increment(key string, delta int64, initialValue *uint64) (newValue uint64, err error)

	Flush() error

	Stats() (*Statistics, error)
}

RawInterface is the full interface to the memcache service.

func Raw

Raw gets the current memcache implementation from the context.

type RawItemCB

type RawItemCB func(Item, error)

RawItemCB is the callback for RawInterface.GetMulti. It takes the retrieved item and the error for that item (e.g. ErrCacheMiss) if there was one. Item is guaranteed to be nil if error is not nil.

type Statistics

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 represents a set of statistics about the memcache cache. This may include items that have expired but have not yet been removed from the cache.

func Stats

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

Stats gets some best-effort statistics about the current state of memcache.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto