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 memcache | 5 package memcache |
6 | 6 |
7 // Interface is the full interface to the memcache service. | 7 // Interface is the full interface to the memcache service. |
8 // | 8 // |
9 // The *Multi methods may return a "github.com/luci/luci-go/common/errors".Multi
Error | 9 // The *Multi methods may return a "github.com/luci/luci-go/common/errors".Multi
Error |
10 // if the rpc to the server was successful, but, e.g. some of the items were | 10 // if the rpc to the server was successful, but, e.g. some of the items were |
11 // missing. They may also return a regular error, if, for example, the rpc | 11 // missing. They may also return a regular error, if, for example, the rpc |
12 // failed outright. | 12 // failed outright. |
13 type Interface interface { | 13 type Interface interface { |
14 // NewItem creates a new, mutable, memcache item. | 14 // NewItem creates a new, mutable, memcache item. |
15 NewItem(key string) Item | 15 NewItem(key string) Item |
16 | 16 |
17 // Add puts a single item into memcache, but only if it didn't exist in | 17 // Add puts a single item into memcache, but only if it didn't exist in |
18 // memcache before. | 18 // memcache before. |
19 Add(item Item) error | 19 Add(item Item) error |
20 | 20 |
21 // Set the item in memcache, whether or not it exists. | 21 // Set the item in memcache, whether or not it exists. |
22 Set(item Item) error | 22 Set(item Item) error |
23 | 23 |
24 » // Get retrieves an item from memcache. Only the key field is read. The
item | 24 » // Get retrieves an item from memcache. |
25 » // will be mutated to contain the data in the memcache item. | 25 » // |
26 » Get(item Item) error | 26 » // On a cache miss ErrCacheMiss will be returned. Item will always be |
| 27 » // returned, even on a miss, but it's value may be empty if it was a mis
s. |
| 28 » Get(key string) (Item, error) |
27 | 29 |
28 // Delete removes an item from memcache. | 30 // Delete removes an item from memcache. |
29 Delete(key string) error | 31 Delete(key string) error |
30 | 32 |
31 // CompareAndSwap accepts an item which is the result of Get() or GetMul
ti(). | 33 // CompareAndSwap accepts an item which is the result of Get() or GetMul
ti(). |
32 // The Get functions add a secret field to item ('CasID'), which is used
as | 34 // The Get functions add a secret field to item ('CasID'), which is used
as |
33 // the "compare" value for the "CompareAndSwap". The actual "Value" fiel
d of | 35 // the "compare" value for the "CompareAndSwap". The actual "Value" fiel
d of |
34 // the object set by the Get functions is the "swap" value. | 36 // the object set by the Get functions is the "swap" value. |
35 // | 37 // |
36 // Example: | 38 // Example: |
37 // mc := memcache.Get(context) | 39 // mc := memcache.Get(context) |
38 // itm := mc.NewItem("aKey") | 40 // itm := mc.NewItem("aKey") |
39 // mc.Get(itm) // check error | 41 // mc.Get(itm) // check error |
40 // itm.SetValue(append(itm.Value(), []byte("more bytes"))) | 42 // itm.SetValue(append(itm.Value(), []byte("more bytes"))) |
41 // mc.CompareAndSwap(itm) // check error | 43 // mc.CompareAndSwap(itm) // check error |
42 CompareAndSwap(item Item) error | 44 CompareAndSwap(item Item) error |
43 | 45 |
| 46 // Batch operations; GetMulti takes a []Item instead of []string to impr
ove |
| 47 // ergonomics when streamlining these operations. |
44 AddMulti(items []Item) error | 48 AddMulti(items []Item) error |
45 SetMulti(items []Item) error | 49 SetMulti(items []Item) error |
46 GetMulti(items []Item) error | 50 GetMulti(items []Item) error |
47 DeleteMulti(keys []string) error | 51 DeleteMulti(keys []string) error |
48 CompareAndSwapMulti(items []Item) error | 52 CompareAndSwapMulti(items []Item) error |
49 | 53 |
50 // Increment adds delta to the uint64 contained at key. If the memcache
key | 54 // Increment adds delta to the uint64 contained at key. If the memcache
key |
51 // is missing, it's populated with initialValue before applying delta (i
.e. | 55 // is missing, it's populated with initialValue before applying delta (i
.e. |
52 // the final value would be initialValue+delta). | 56 // the final value would be initialValue+delta). |
53 // | 57 // |
54 // Underflow caps at 0, overflow wraps back to 0. | 58 // Underflow caps at 0, overflow wraps back to 0. |
55 // | 59 // |
56 // If key contains a value which is not exactly 8 bytes, it's assumed to | 60 // If key contains a value which is not exactly 8 bytes, it's assumed to |
57 // contain non-number data and this method will return an error. | 61 // contain non-number data and this method will return an error. |
58 Increment(key string, delta int64, initialValue uint64) (newValue uint64
, err error) | 62 Increment(key string, delta int64, initialValue uint64) (newValue uint64
, err error) |
59 | 63 |
60 // IncrementExisting is like Increment, except that the valu must exist | 64 // IncrementExisting is like Increment, except that the valu must exist |
61 // already. | 65 // already. |
62 IncrementExisting(key string, delta int64) (newValue uint64, err error) | 66 IncrementExisting(key string, delta int64) (newValue uint64, err error) |
63 | 67 |
64 // Flush dumps the entire memcache state. | 68 // Flush dumps the entire memcache state. |
65 Flush() error | 69 Flush() error |
66 | 70 |
67 // Stats gets some best-effort statistics about the current state of mem
cache. | 71 // Stats gets some best-effort statistics about the current state of mem
cache. |
68 Stats() (*Statistics, error) | 72 Stats() (*Statistics, error) |
69 | 73 |
70 Raw() RawInterface | 74 Raw() RawInterface |
71 } | 75 } |
OLD | NEW |