# The memcache package - Google App Engine - Google Code

English ▼
Skip to page content Skip to main navigation
e.g. "templates" or "datastore"

# The memcache package

## Introduction

Package memcache provides a client for App Engine's distributed in-memory key-value store for small chunks of arbitrary data.

The fundamental operations get and set items, keyed by a string.

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

and

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

## Variables

    var (
        // ErrCacheMiss means that an operation failed
        // because the item wasn't present.
        ErrCacheMiss = os.NewError("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 = os.NewError("memcache: compare-and-swap conflict")
        // ErrNoStats means that no statistics were available.
        ErrNoStats = os.NewError("memcache: no statistics available")
        // ErrNotStored means that a conditional write operation (i.e. Add or
        // CompareAndSwap) failed because the condition was not satisfied.
        ErrNotStored = os.NewError("memcache: item not stored")
        // ErrServerError means that a server error occurred.
        ErrServerError = os.NewError("memcache: server error")
    )

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

## Functions

func Add

  `func Add(c appengine.Context, item *Item) os.Error`

  Add writes the given item, if no value already exists for its key. ErrNotStored is returned if that condition is not met.

func AddMulti

  `func AddMulti(c appengine.Context, item []*Item) []os.Error`

  AddMulti is a batch version of Add. The returned slice will have the same length as the input slice.

func CompareAndSwap

  `func CompareAndSwap(c appengine.Context, item *Item) os.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 CompareAndSwapMulti

  `func CompareAndSwapMulti(c appengine.Context, item []*Item) []os.Error`

  CompareAndSwapMulti is a batch version of CompareAndSwap. The returned slice will have the same length as the input slice.

func Delete

  `func Delete(c appengine.Context, key string) os.Error`

  Delete deletes the item for the given key. ErrCacheMiss is returned if the specified item can not be found. The key must be at most 250 bytes in length.

func DeleteMulti

  `func DeleteMulti(c appengine.Context, key []string) []os.Error`

  DeleteMulti is a batch version of Delete. The returned slice will have the same length as the input slice. If a given key cannot be found, its corresponding value in the returned error slice is set to ErrCacheMiss. Each key must be at most 250 bytes in length.

func GetMulti

  `func GetMulti(c appengine.Context, key []string) (map[string]*Item, os.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.

func Increment

  `func Increment(c appengine.Context, key string, delta int64, initialValue uint64) (newValue uint64, err os.Error)`

  Increment atomically increments the decimal value in the given key by delta and returns the new value. The value must fit in a uint64. Overflow wraps around, and underflow is capped to zero. The provided delta may be negative. If the key doesn't exist in memcacheg, the provided initial value is used to atomically populate it before the delta is applied. The key must be at most 250 bytes in length.

func IncrementExisting

  `func IncrementExisting(c appengine.Context, key string, delta int64) (newValue uint64, err os.Error)`

  IncrementExisting works like Increment but assumes that the key already exists in memcache and doesn't take an initial value. IncrementExisting can save work if calculating the initial value is expensive. ErrCacheMiss is returned if the specified item can not be found.

func Set

  `func Set(c appengine.Context, item *Item) os.Error`

  Set writes the given item, unconditionally.

func SetMulti

  `func SetMulti(c appengine.Context, item []*Item) []os.Error`

  SetMulti is a batch version of Set. The returned slice will have the same length as the input slice.

## Types

type Codec

Codec represents a symmetric pair of functions that implement a codec. Items stored into or retrieved from memcache using a Codec have their values marshaled or unmarshaled.

      type Codec struct {
          Marshal   func(interface{}) ([]byte, os.Error)
          Unmarshal func([]byte, interface{}) os.Error
      }

  func (Codec) Add

    `func (cd Codec) Add(c appengine.Context, item *Item) os.Error`

  func (Codec) Get

    `func (cd Codec) Get(c appengine.Context, key string, v interface{}) (*Item, os.Error)`

  func (Codec) Set

    `func (cd Codec) Set(c appengine.Context, item *Item) os.Error`

type Item

Item is the unit of memcache gets and sets.

      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 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
      }

  func Get

    `func Get(c appengine.Context, key string) (*Item, os.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.

type Statistics

Statistics represents a set of statistics about the memcache cache.

      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
      }

  func Stats

    `func Stats(c appengine.Context) (*Statistics, os.Error)`

    Stats retrieves the current memcache statistics.