Chromium Code Reviews| Index: go/src/infra/gae/libs/gae/memcache.go |
| diff --git a/go/src/infra/gae/libs/gae/memcache.go b/go/src/infra/gae/libs/gae/memcache.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1914dfc57a3ceb0fa172f168ca1b9cd5fab452bb |
| --- /dev/null |
| +++ b/go/src/infra/gae/libs/gae/memcache.go |
| @@ -0,0 +1,103 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package gae |
| + |
| +import ( |
| + "time" |
| + |
| + "golang.org/x/net/context" |
| +) |
| + |
| +// MCItem is a wrapper around *memcache.Item. Note that the Set* methods all |
| +// return the original MCItem (e.g. they mutate the original), due to |
| +// implementation constraints. They return the oririnal item to allow easy |
|
Vadim Sh.
2015/07/13 21:17:12
typo: oririnal
iannucci
2015/07/13 22:39:53
Done.
|
| +// chaining, e.g.: |
| +// itm := gae.MC(c).NewItem("foo").SetValue([]byte("stuff")) |
| +type MCItem interface { |
| + Key() string |
| + Value() []byte |
| + Object() interface{} |
| + Flags() uint32 |
| + Expiration() time.Duration |
| + |
| + SetKey(string) MCItem |
|
Vadim Sh.
2015/07/13 21:17:12
No errors?
iannucci
2015/07/13 22:39:53
Correct, this is replacing a dumb struct (with a s
|
| + SetValue([]byte) MCItem |
| + SetObject(interface{}) MCItem |
| + SetFlags(uint32) MCItem |
| + SetExpiration(time.Duration) MCItem |
| +} |
| + |
| +// MCCodecDef represents a symmetric pair of functions that implement a codec. |
| +// Items stored into or retrieved from memcache using MCCodec have their |
| +// values marshaled or unmarshaled. |
| +type MCCodecDef struct { |
| + Marshal func(interface{}) ([]byte, error) |
| + Unmarshal func([]byte, interface{}) error |
| +} |
| + |
| +// MCCodec is the result of inflating a MCCodecDef from a given Memcache (using |
| +// Memcache.InflateCodec). Its functions will automatically Marshal/Unmarshal |
| +// the Object() field of the items, according to the MCCodecDef. |
| +type MCCodec interface { |
|
Vadim Sh.
2015/07/13 21:17:12
MCCodecDef vs MCCodec is odd...
Does MCCoded hav
iannucci
2015/07/13 22:39:53
With your permission, I'd like to drop the codec t
Vadim Sh.
2015/07/14 00:12:36
sgtm
|
| + Add(item MCItem) error |
| + AddMulti(items []MCItem) error |
| + CompareAndSwap(item MCItem) error |
| + CompareAndSwapMulti(items []MCItem) error |
| + Get(key string, v interface{}) (MCItem, error) |
| + Set(item MCItem) error |
| + SetMulti(items []MCItem) error |
| +} |
| + |
| +// Memcache is the full interface to the memcache service. |
| +type Memcache interface { |
| + // Create a new, empty, MCItem |
|
Vadim Sh.
2015/07/13 21:17:12
nit: NewItem creates a new ...
Or just remove the
iannucci
2015/07/13 22:39:53
removed, it's pretty obvious what this does.
|
| + NewItem(key string) MCItem |
| + |
| + Add(item MCItem) error |
| + Set(item MCItem) error |
| + Get(key string) (MCItem, error) |
| + Delete(key string) error |
| + CompareAndSwap(item MCItem) error |
| + |
| + AddMulti(items []MCItem) error |
| + SetMulti(items []MCItem) error |
| + GetMulti(keys []string) (map[string]MCItem, error) |
| + DeleteMulti(keys []string) error |
| + CompareAndSwapMulti(items []MCItem) error |
| + |
| + Increment(key string, delta int64, initialValue uint64) (newValue uint64, err error) |
| + IncrementExisting(key string, delta int64) (newValue uint64, err error) |
| + |
| + InflateCodec(mcd MCCodecDef) MCCodec |
| + |
| + Flush() error |
| + |
| + Stats() (*MCStatistics, error) |
| +} |
| + |
| +// MCFactory is the function signature for factory methods compatible with |
| +// SetMCFactory. |
| +type MCFactory func(context.Context) Memcache |
| + |
| +// GetMC gets the current memcache implementation from the context. |
| +func GetMC(c context.Context) Memcache { |
| + if f, ok := c.Value(memcacheKey).(MCFactory); ok && f != nil { |
| + return f(c) |
| + } |
| + return nil |
| +} |
| + |
| +// SetMCFactory sets the function to produce Memcache instances, as returned by |
| +// the GetMC method. |
| +func SetMCFactory(c context.Context, mcf MCFactory) context.Context { |
| + return context.WithValue(c, memcacheKey, mcf) |
| +} |
| + |
| +// SetMC sets the current Memcache object in the context. Useful for testing |
| +// with a quick mock. This is just a shorthand SetMCFactory invocation to set |
| +// a factory which always returns the same object. |
| +func SetMC(c context.Context, mc Memcache) context.Context { |
| + return SetMCFactory(c, func(context.Context) Memcache { return mc }) |
| +} |