Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: memcache.go

Issue 1243323002: Refactor a bit. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: fix golint Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mathrand_test.go ('k') | memory/README.md » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 gae
6
7 import (
8 "time"
9
10 "golang.org/x/net/context"
11 )
12
13 // MCItem is a wrapper around *memcache.Item. Note that the Set* methods all
14 // return the original MCItem (e.g. they mutate the original), due to
15 // implementation constraints. They return the original item to allow easy
16 // chaining, e.g.:
17 // itm := gae.MC(c).NewItem("foo").SetValue([]byte("stuff"))
18 type MCItem interface {
19 Key() string
20 Value() []byte
21 Object() interface{}
22 Flags() uint32
23 Expiration() time.Duration
24
25 SetKey(string) MCItem
26 SetValue([]byte) MCItem
27 SetObject(interface{}) MCItem
28 SetFlags(uint32) MCItem
29 SetExpiration(time.Duration) MCItem
30 }
31
32 // Memcache is the full interface to the memcache service.
33 type Memcache interface {
34 NewItem(key string) MCItem
35
36 Add(item MCItem) error
37 Set(item MCItem) error
38 Get(key string) (MCItem, error)
39 Delete(key string) error
40 CompareAndSwap(item MCItem) error
41
42 AddMulti(items []MCItem) error
43 SetMulti(items []MCItem) error
44 GetMulti(keys []string) (map[string]MCItem, error)
45 DeleteMulti(keys []string) error
46 CompareAndSwapMulti(items []MCItem) error
47
48 Increment(key string, delta int64, initialValue uint64) (newValue uint64 , err error)
49 IncrementExisting(key string, delta int64) (newValue uint64, err error)
50
51 Flush() error
52
53 Stats() (*MCStatistics, error)
54 }
55
56 // MCFactory is the function signature for factory methods compatible with
57 // SetMCFactory.
58 type MCFactory func(context.Context) Memcache
59
60 // MCFilter is the function signature for a filter MC implementation. It
61 // gets the current MC implementation, and returns a new MC implementation
62 // backed by the one passed in.
63 type MCFilter func(context.Context, Memcache) Memcache
64
65 // GetMCUnfiltered gets gets the Memcache implementation from context without
66 // any of the filters applied.
67 func GetMCUnfiltered(c context.Context) Memcache {
68 if f, ok := c.Value(memcacheKey).(MCFactory); ok && f != nil {
69 return f(c)
70 }
71 return nil
72 }
73
74 // GetMC gets the current memcache implementation from the context.
75 func GetMC(c context.Context) Memcache {
76 ret := GetMCUnfiltered(c)
77 if ret == nil {
78 return nil
79 }
80 for _, f := range getCurMCFilters(c) {
81 ret = f(c, ret)
82 }
83 return ret
84 }
85
86 // SetMCFactory sets the function to produce Memcache instances, as returned by
87 // the GetMC method.
88 func SetMCFactory(c context.Context, mcf MCFactory) context.Context {
89 return context.WithValue(c, memcacheKey, mcf)
90 }
91
92 // SetMC sets the current Memcache object in the context. Useful for testing
93 // with a quick mock. This is just a shorthand SetMCFactory invocation to set
94 // a factory which always returns the same object.
95 func SetMC(c context.Context, mc Memcache) context.Context {
96 return SetMCFactory(c, func(context.Context) Memcache { return mc })
97 }
98
99 func getCurMCFilters(c context.Context) []MCFilter {
100 curFiltsI := c.Value(memcacheFilterKey)
101 if curFiltsI != nil {
102 return curFiltsI.([]MCFilter)
103 }
104 return nil
105 }
106
107 // AddMCFilters adds Memcache filters to the context.
108 func AddMCFilters(c context.Context, filts ...MCFilter) context.Context {
109 if len(filts) == 0 {
110 return c
111 }
112 cur := getCurMCFilters(c)
113 newFilts := make([]MCFilter, 0, len(cur)+len(filts))
114 newFilts = append(newFilts, getCurMCFilters(c)...)
115 newFilts = append(newFilts, filts...)
116 return context.WithValue(c, memcacheFilterKey, newFilts)
117 }
OLDNEW
« no previous file with comments | « mathrand_test.go ('k') | memory/README.md » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698