import "code.google.com/p/appengine-go/appengine/memcache"
memcache包提供了App Engine的分布式内存键值对模式的对小块任意数据的存储服务。
基本操作是获取和设定item,键为字符串:
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 }
var ( // 表示因item不存在而操作失败 ErrCacheMiss = errors.New("memcache: cache miss") // 表示CompareAndSwap调用因为在Get和CompareAndSwap调用之间修改了缓存值而失败 // 如果缓存值是被删除而不是替换,将返回ErrNotStored ErrCASConflict = errors.New("memcache: compare-and-swap conflict") // 表示没有可用的统计信息 ErrNoStats = errors.New("memcache: no statistics available") // 表示某个有条件的写操作(如Add 或 CompareAndSwap)因为条件不满足而失败 ErrNotStored = errors.New("memcache: item not stored") // 表示出现了服务端错误 ErrServerError = errors.New("memcache: server error") )
var ( // 使用gob包的编解码器 Gob = Codec{gobMarshal, gobUnmarshal} // 使用json包的编解码器 JSON = Codec{json.Marshal, json.Unmarshal} )
type Statistics struct { Hits uint64 // 缓存命中计数器 Misses uint64 // 缓存遗漏计数器 ByteHits uint64 // 获取转移计数器 Items uint64 // 当前缓存中的Item的数量 Bytes uint64 // 当前缓存中的所有Item的大小 Oldest int64 // 最老Item的存在时间,以秒为单位 }
Statistics代表一个memcache缓存的统计数据集。
func Stats(c appengine.Context) (*Statistics, error)
Stats获取当前memcache缓存的统计数据。
type Item struct { // Key是Item的键(最大250字节)。 Key string // Value是Item的值。 Value []byte // Object是Item的值,用于Codec。 Object interface{} // Flags是用于服务端的不透明操作项,其意义完全取决于App Engine服务。 Flags uint32 // Expiration是该Item的最大缓存时间,零值表示无过期时间,小于1秒的精度被忽略。 // 获取Item时不设置该项。 Expiration time.Duration // 内含隐藏或非导出字段 }
Item是memcache存取的基本单元。
func Get(c appengine.Context, key string) (*Item, error)
使用给定的键获取对应的Item,当item不存在时返回ErrCacheMiss,key最长250字节。
func GetMulti(c appengine.Context, key []string) (map[string]*Item, error)
Get的批处理版本。返回字典可能会因为缓存中没有对应item而导致键值对数比key的数目少。
func Set(c appengine.Context, item *Item) error
无条件的写入给定的Item。
func SetMulti(c appengine.Context, item []*Item) error
Set的批处理版本,可能返回appengine.MultiError类型的错误。
func Add(c appengine.Context, item *Item) error
如果item的键没有存在对应的item,则写入该item,否则返回ErrNotStored。
func AddMulti(c appengine.Context, item []*Item) error
Add的批处理版本,可能返回appengine.MultiError类型的错误。
func Delete(c appengine.Context, key string) error
删除key对应的Item,如果未找到该Item返回ErrCacheMiss。
func DeleteMulti(c appengine.Context, key []string) error
Delete的批处理版本。
func CompareAndSwap(c appengine.Context, item *Item) error
函数修改Get函数之前返回的Item,Item的键不可改变,但其它字段都可以修改。在本次CompareAndSwap和Get之间,如果有其它对该Item的修改被写入缓存,会返回ErrCASConflict错误;如果两者中间该Item被删除了,会返回ErrNotStored错误。
func CompareAndSwapMulti(c appengine.Context, item []*Item) error
CompareAndSwap的批处理版本,可能返回appengine.MultiError类型的错误。
func Flush(c appengine.Context) error
清空缓存中的所有Item。
func Increment(c appengine.Context, key string, delta int64, initialValue uint64) (newValue uint64, err error)
让key表示的十进制value加上delta,并返回新的value。Value应能用int64表示,上溢会截断,下溢则置为0。Delta可以是负数。如果缓存中不存在键key,则会使用initalValue原子性的创建它,在之后才加上delta。
func IncrementExisting(c appengine.Context, key string, delta int64) (newValue uint64, err error)
函数类似Increment但假设key已经存在于缓存中,以便节省工作量。如果没有找到key对应的Item会返回错误。
type Codec struct { Marshal func(interface{}) ([]byte, error) Unmarshal func([]byte, interface{}) error }
Codec代表两个对称的实现了编解码器的函数。Item从memcache中恢复或者存入memcache都会使用Codec对值进行序列化和反序列化。Codec提供的所有方法都和包水平的同名方法效果类似。
func (cd Codec) Get(c appengine.Context, key string, v interface{}) (*Item, error)
获取key对应的Item并解码数据填写入v。如果没找到Item会返回ErrCacheMiss错误。
func (cd Codec) Set(c appengine.Context, item *Item) error
无条件的存入Item。
func (cd Codec) SetMulti(c appengine.Context, items []*Item) error
Set的批处理版本,可能返回appengine.MultiError类型的错误。
func (cd Codec) Add(c appengine.Context, item *Item) error
当item的Key字段在缓存中没有对应的值时,存入item;否则返回ErrNotStored错误。
func (cd Codec) AddMulti(c appengine.Context, items []*Item) error
Add的批处理版本,可能返回appengine.MultiError类型的错误。
func (cd Codec) CompareAndSwap(c appengine.Context, item *Item) error
方法修改Get方法之前返回的Item,Item的键不可改变,但其它字段都可以修改。在本次CompareAndSwap和Get之间,如果有其它对该Item的修改被写入缓存,会返回ErrCASConflict错误;如果两者中间该Item被删除了,会返回ErrNotStored错误。
func (cd Codec) CompareAndSwapMulti(c appengine.Context, items []*Item) error
CompareAndSwap的批处理版本,可能返回appengine.MultiError类型的错误。