-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmeta.go
83 lines (75 loc) · 2.42 KB
/
meta.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package memcache
import (
"context"
"encoding/base64"
"strconv"
)
type MetaResult struct {
CasToken casToken
Flags uint32
Key string
LastAccess uint64
Opaque string
Size int
TTL int64
Value []byte
Hit bool
Won bool
Stale bool
isNoOp bool
}
// The meta get command is the generic command for retrieving key data from
// memcached. Based on the flags supplied, it can replace all of the commands:
// "get", "gets", "gat", "gats", "touch", as well as adding new options.
func (c *Client) MetaGet(ctx context.Context, opt MetaGetOptions) (i MetaResult, err error) {
err = c.do(ctx, func(c *Conn) error {
i, err = c.metaCmd("mg", stringfyKey(opt.Key, opt.BinaryKey), opt.marshal(), nil)
return err
})
return
}
// The meta set command a generic command for storing data to memcached. Based
// on the flags supplied, it can replace all storage commands (see token M) as
// well as adds new options.
func (c *Client) MetaSet(ctx context.Context, opt MetaSetOptions) (i MetaResult, err error) {
if opt.Value == nil {
opt.Value = []byte{}
}
err = c.do(ctx, func(c *Conn) error {
i, err = c.metaCmd("ms", stringfyKey(opt.Key, opt.BinaryKey), opt.marshal(), opt.Value)
return err
})
return
}
// The meta delete command allows for explicit deletion of items, as well as
// marking items as "stale" to allow serving items as stale during revalidation.
func (c *Client) MetaDelete(ctx context.Context, opt MetaDeletOptions) (i MetaResult, err error) {
err = c.do(ctx, func(c *Conn) error {
i, err = c.metaCmd("md", stringfyKey(opt.Key, opt.BinaryKey), opt.marshal(), nil)
return err
})
return
}
// The meta arithmetic command allows for basic operations against numerical
// values. This replaces the "incr" and "decr" commands. Values are unsigned
// 64bit integers. Decrementing will reach 0 rather than underflow. Incrementing
// can overflow.
func (c *Client) MetaArithmetic(ctx context.Context, opt MetaArithmeticOptions) (v uint64, i MetaResult, err error) {
err = c.do(ctx, func(c *Conn) error {
if i, err = c.metaCmd("ma", stringfyKey(opt.Key, opt.BinaryKey), opt.marshal(), nil); err != nil {
return err
}
if opt.GetValue {
v, err = strconv.ParseUint(string(i.Value), 10, 64)
return err
}
return nil
})
return
}
func stringfyKey(key string, binaryKey []byte) string {
if len(binaryKey) > 0 {
return base64.StdEncoding.EncodeToString(binaryKey)
}
return key
}