| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package prod | 5 package prod |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "time" | 8 "time" |
| 9 | 9 |
| 10 mc "github.com/luci/gae/service/memcache" | 10 mc "github.com/luci/gae/service/memcache" |
| 11 "golang.org/x/net/context" | 11 "golang.org/x/net/context" |
| 12 "google.golang.org/appengine" | 12 "google.golang.org/appengine" |
| 13 "google.golang.org/appengine/memcache" | 13 "google.golang.org/appengine/memcache" |
| 14 ) | 14 ) |
| 15 | 15 |
| 16 // useMC adds a gae.Memcache implementation to context, accessible | 16 // useMC adds a gae.Memcache implementation to context, accessible |
| 17 // by gae.GetMC(c) | 17 // by gae.GetMC(c) |
| 18 func useMC(c context.Context) context.Context { | 18 func useMC(c context.Context) context.Context { |
| 19 return mc.SetRawFactory(c, func(ci context.Context) mc.RawInterface { | 19 return mc.SetRawFactory(c, func(ci context.Context) mc.RawInterface { |
| 20 » » return mcImpl{ci} | 20 » » return mcImpl{AEContext(ci)} |
| 21 }) | 21 }) |
| 22 } | 22 } |
| 23 | 23 |
| 24 type mcImpl struct{ context.Context } | 24 type mcImpl struct { |
| 25 » aeCtx context.Context |
| 26 } |
| 25 | 27 |
| 26 type mcItem struct { | 28 type mcItem struct { |
| 27 i *memcache.Item | 29 i *memcache.Item |
| 28 } | 30 } |
| 29 | 31 |
| 30 var _ mc.Item = mcItem{} | 32 var _ mc.Item = mcItem{} |
| 31 | 33 |
| 32 func (i mcItem) Key() string { return i.i.Key } | 34 func (i mcItem) Key() string { return i.i.Key } |
| 33 func (i mcItem) Value() []byte { return i.i.Value } | 35 func (i mcItem) Value() []byte { return i.i.Value } |
| 34 func (i mcItem) Flags() uint32 { return i.i.Flags } | 36 func (i mcItem) Flags() uint32 { return i.i.Flags } |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 if me, ok := err.(appengine.MultiError); ok { | 102 if me, ok := err.(appengine.MultiError); ok { |
| 101 for _, err := range me { | 103 for _, err := range me { |
| 102 cb(err) | 104 cb(err) |
| 103 } | 105 } |
| 104 err = nil | 106 err = nil |
| 105 } | 107 } |
| 106 return err | 108 return err |
| 107 } | 109 } |
| 108 | 110 |
| 109 func (m mcImpl) DeleteMulti(keys []string, cb mc.RawCB) error { | 111 func (m mcImpl) DeleteMulti(keys []string, cb mc.RawCB) error { |
| 110 » return doCB(memcache.DeleteMulti(m.Context, keys), cb) | 112 » return doCB(memcache.DeleteMulti(m.aeCtx, keys), cb) |
| 111 } | 113 } |
| 112 | 114 |
| 113 func (m mcImpl) AddMulti(items []mc.Item, cb mc.RawCB) error { | 115 func (m mcImpl) AddMulti(items []mc.Item, cb mc.RawCB) error { |
| 114 » return doCB(memcache.AddMulti(m.Context, mcMF2R(items)), cb) | 116 » return doCB(memcache.AddMulti(m.aeCtx, mcMF2R(items)), cb) |
| 115 } | 117 } |
| 116 | 118 |
| 117 func (m mcImpl) SetMulti(items []mc.Item, cb mc.RawCB) error { | 119 func (m mcImpl) SetMulti(items []mc.Item, cb mc.RawCB) error { |
| 118 » return doCB(memcache.SetMulti(m.Context, mcMF2R(items)), cb) | 120 » return doCB(memcache.SetMulti(m.aeCtx, mcMF2R(items)), cb) |
| 119 } | 121 } |
| 120 | 122 |
| 121 func (m mcImpl) GetMulti(keys []string, cb mc.RawItemCB) error { | 123 func (m mcImpl) GetMulti(keys []string, cb mc.RawItemCB) error { |
| 122 » realItems, err := memcache.GetMulti(m.Context, keys) | 124 » realItems, err := memcache.GetMulti(m.aeCtx, keys) |
| 123 if err != nil { | 125 if err != nil { |
| 124 return err | 126 return err |
| 125 } | 127 } |
| 126 for _, k := range keys { | 128 for _, k := range keys { |
| 127 itm := realItems[k] | 129 itm := realItems[k] |
| 128 if itm == nil { | 130 if itm == nil { |
| 129 cb(nil, memcache.ErrCacheMiss) | 131 cb(nil, memcache.ErrCacheMiss) |
| 130 } else { | 132 } else { |
| 131 cb(mcItem{itm}, nil) | 133 cb(mcItem{itm}, nil) |
| 132 } | 134 } |
| 133 } | 135 } |
| 134 return nil | 136 return nil |
| 135 } | 137 } |
| 136 | 138 |
| 137 func (m mcImpl) CompareAndSwapMulti(items []mc.Item, cb mc.RawCB) error { | 139 func (m mcImpl) CompareAndSwapMulti(items []mc.Item, cb mc.RawCB) error { |
| 138 » return doCB(memcache.CompareAndSwapMulti(m.Context, mcMF2R(items)), cb) | 140 » return doCB(memcache.CompareAndSwapMulti(m.aeCtx, mcMF2R(items)), cb) |
| 139 } | 141 } |
| 140 | 142 |
| 141 func (m mcImpl) Increment(key string, delta int64, initialValue *uint64) (uint64
, error) { | 143 func (m mcImpl) Increment(key string, delta int64, initialValue *uint64) (uint64
, error) { |
| 142 if initialValue == nil { | 144 if initialValue == nil { |
| 143 » » return memcache.IncrementExisting(m.Context, key, delta) | 145 » » return memcache.IncrementExisting(m.aeCtx, key, delta) |
| 144 } | 146 } |
| 145 » return memcache.Increment(m.Context, key, delta, *initialValue) | 147 » return memcache.Increment(m.aeCtx, key, delta, *initialValue) |
| 146 } | 148 } |
| 147 | 149 |
| 148 func (m mcImpl) Flush() error { | 150 func (m mcImpl) Flush() error { |
| 149 » return memcache.Flush(m.Context) | 151 » return memcache.Flush(m.aeCtx) |
| 150 } | 152 } |
| 151 | 153 |
| 152 func (m mcImpl) Stats() (*mc.Statistics, error) { | 154 func (m mcImpl) Stats() (*mc.Statistics, error) { |
| 153 » stats, err := memcache.Stats(m.Context) | 155 » stats, err := memcache.Stats(m.aeCtx) |
| 154 if err != nil { | 156 if err != nil { |
| 155 return nil, err | 157 return nil, err |
| 156 } | 158 } |
| 157 return (*mc.Statistics)(stats), nil | 159 return (*mc.Statistics)(stats), nil |
| 158 } | 160 } |
| OLD | NEW |