go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/filter/count/mc.go (about) 1 // Copyright 2015 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package count 16 17 import ( 18 "context" 19 20 mc "go.chromium.org/luci/gae/service/memcache" 21 ) 22 23 // MCCounter is the counter object for the Memcache service. 24 type MCCounter struct { 25 NewItem Entry 26 AddMulti Entry 27 SetMulti Entry 28 GetMulti Entry 29 DeleteMulti Entry 30 CompareAndSwapMulti Entry 31 Increment Entry 32 Flush Entry 33 Stats Entry 34 } 35 36 type mcCounter struct { 37 c *MCCounter 38 39 mc mc.RawInterface 40 } 41 42 var _ mc.RawInterface = (*mcCounter)(nil) 43 44 func (m *mcCounter) NewItem(key string) mc.Item { 45 _ = m.c.NewItem.up() 46 return m.mc.NewItem(key) 47 } 48 49 func (m *mcCounter) GetMulti(keys []string, cb mc.RawItemCB) error { 50 return m.c.GetMulti.up(m.mc.GetMulti(keys, cb)) 51 } 52 53 func (m *mcCounter) AddMulti(items []mc.Item, cb mc.RawCB) error { 54 return m.c.AddMulti.up(m.mc.AddMulti(items, cb)) 55 } 56 57 func (m *mcCounter) SetMulti(items []mc.Item, cb mc.RawCB) error { 58 return m.c.SetMulti.up(m.mc.SetMulti(items, cb)) 59 } 60 61 func (m *mcCounter) DeleteMulti(keys []string, cb mc.RawCB) error { 62 return m.c.DeleteMulti.up(m.mc.DeleteMulti(keys, cb)) 63 } 64 65 func (m *mcCounter) CompareAndSwapMulti(items []mc.Item, cb mc.RawCB) error { 66 return m.c.CompareAndSwapMulti.up(m.mc.CompareAndSwapMulti(items, cb)) 67 } 68 69 func (m *mcCounter) Flush() error { return m.c.Flush.up(m.mc.Flush()) } 70 71 func (m *mcCounter) Increment(key string, delta int64, initialValue *uint64) (newValue uint64, err error) { 72 ret, err := m.mc.Increment(key, delta, initialValue) 73 return ret, m.c.Increment.up(err) 74 } 75 76 func (m *mcCounter) Stats() (*mc.Statistics, error) { 77 ret, err := m.mc.Stats() 78 return ret, m.c.Stats.up(err) 79 } 80 81 // FilterMC installs a counter Memcache filter in the context. 82 func FilterMC(c context.Context) (context.Context, *MCCounter) { 83 state := &MCCounter{} 84 return mc.AddRawFilters(c, func(ic context.Context, mc mc.RawInterface) mc.RawInterface { 85 return &mcCounter{state, mc} 86 }), state 87 }