| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package wrapper | |
| 6 | |
| 7 import ( | |
| 8 "golang.org/x/net/context" | |
| 9 | |
| 10 "appengine/memcache" | |
| 11 ) | |
| 12 | |
| 13 // MCSingleReadWriter is the interface for interacting with a single | |
| 14 // memcache.Item at a time. See appengine.memcache for more details. | |
| 15 type MCSingleReadWriter interface { | |
| 16 Add(item *memcache.Item) error | |
| 17 Set(item *memcache.Item) error | |
| 18 Get(key string) (*memcache.Item, error) | |
| 19 Delete(key string) error | |
| 20 CompareAndSwap(item *memcache.Item) error | |
| 21 } | |
| 22 | |
| 23 // MCIncrementer is the interface for atomically incrementing a numeric | |
| 24 // memcache entry. See appengine.memcache for more details. | |
| 25 type MCIncrementer interface { | |
| 26 Increment(key string, delta int64, initialValue uint64) (newValue uint64
, err error) | |
| 27 IncrementExisting(key string, delta int64) (newValue uint64, err error) | |
| 28 } | |
| 29 | |
| 30 // MCMultiReadWriter is the interface for doing batched memcache | |
| 31 // operations. See appengine.memcache for more details. | |
| 32 type MCMultiReadWriter interface { | |
| 33 MCSingleReadWriter | |
| 34 | |
| 35 AddMulti(items []*memcache.Item) error | |
| 36 SetMulti(items []*memcache.Item) error | |
| 37 GetMulti(keys []string) (map[string]*memcache.Item, error) | |
| 38 DeleteMulti(keys []string) error | |
| 39 CompareAndSwapMulti(items []*memcache.Item) error | |
| 40 } | |
| 41 | |
| 42 // MCFlusher is the interface for wiping the whole memcache server. | |
| 43 // See appengine.memcache for more details. | |
| 44 type MCFlusher interface { | |
| 45 Flush() error | |
| 46 } | |
| 47 | |
| 48 // MCStatter is the interface for gathering statistics about the | |
| 49 // memcache server. See appengine.memcache for more details. | |
| 50 type MCStatter interface { | |
| 51 Stats() (*memcache.Statistics, error) | |
| 52 } | |
| 53 | |
| 54 // MCCodec is the interface representing what you can do with a memcache.Codec | |
| 55 // after it's been inflated with InflateCodec. It mirrors | |
| 56 // appengine.memcache.Codec. | |
| 57 type MCCodec interface { | |
| 58 // would use MCSingleReadWriter, but Get has a different signature. | |
| 59 Add(item *memcache.Item) error | |
| 60 Set(item *memcache.Item) error | |
| 61 Get(key string, v interface{}) (*memcache.Item, error) | |
| 62 CompareAndSwap(item *memcache.Item) error | |
| 63 | |
| 64 AddMulti(items []*memcache.Item) error | |
| 65 SetMulti(items []*memcache.Item) error | |
| 66 CompareAndSwapMulti(items []*memcache.Item) error | |
| 67 } | |
| 68 | |
| 69 // MCCodecInflater binds a Memcache to a memcache.Codec. | |
| 70 type MCCodecInflater interface { | |
| 71 InflateCodec(m memcache.Codec) MCCodec | |
| 72 } | |
| 73 | |
| 74 // Memcache is the full interface to the memcache service. | |
| 75 type Memcache interface { | |
| 76 MCMultiReadWriter | |
| 77 MCIncrementer | |
| 78 MCFlusher | |
| 79 MCStatter | |
| 80 MCCodecInflater | |
| 81 } | |
| 82 | |
| 83 // MCFactory is the function signature for factory methods compatible with | |
| 84 // SetMCFactory. | |
| 85 type MCFactory func(context.Context) Memcache | |
| 86 | |
| 87 // GetMC gets the current memcache implementation from the context. | |
| 88 func GetMC(c context.Context) Memcache { | |
| 89 if f, ok := c.Value(memcacheKey).(MCFactory); ok && f != nil { | |
| 90 return f(c) | |
| 91 } | |
| 92 return nil | |
| 93 } | |
| 94 | |
| 95 // SetMCFactory sets the function to produce Memcache instances, as returned by | |
| 96 // the GetMC method. | |
| 97 func SetMCFactory(c context.Context, mcf MCFactory) context.Context { | |
| 98 return context.WithValue(c, memcacheKey, mcf) | |
| 99 } | |
| 100 | |
| 101 // SetMC sets the current Memcache object in the context. Useful for testing | |
| 102 // with a quick mock. This is just a shorthand SetMCFactory invocation to set | |
| 103 // a factory which always returns the same object. | |
| 104 func SetMC(c context.Context, mc Memcache) context.Context { | |
| 105 return SetMCFactory(c, func(context.Context) Memcache { return mc }) | |
| 106 } | |
| OLD | NEW |