-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem.go
76 lines (68 loc) · 1.55 KB
/
item.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
package mc
import (
"encoding"
"encoding/json"
"github.com/bytedance/sonic"
"github.com/tinylib/msgp/msgp"
"google.golang.org/protobuf/proto"
)
type (
Item struct {
Key string
Value Value
Flags uint16
cr float32 // compression ratio
cas uint64
hit bool
won bool
stale bool
opaque int
lastAccess int64
}
Value []byte
)
func (i *Item) Won() bool { return i.won }
func (i *Item) Hit() bool { return i.hit }
func (i *Item) Stale() bool { return i.stale }
func (i *Item) LastAccess() int { return int(i.lastAccess) }
func (i *Item) CompressionRatio() float32 { return i.cr }
func (val *Value) Marshal(v any) error {
r, err := marshal(v)
if err != nil {
return err
}
*val = r
return nil
}
func (val Value) Unmarshal(v any) error {
return unmarshal(val, v)
}
func marshal(v any) ([]byte, error) {
switch v := v.(type) {
case proto.Message:
return proto.Marshal(v)
case json.Marshaler:
return v.MarshalJSON()
case msgp.Marshaler:
return v.MarshalMsg(nil)
case encoding.BinaryMarshaler:
return v.MarshalBinary()
}
return sonic.Marshal(v)
}
func unmarshal(data []byte, v any) error {
switch v := v.(type) {
case proto.Message:
return proto.Unmarshal(data, v)
case json.Unmarshaler:
return v.UnmarshalJSON(data)
case msgp.Unmarshaler:
if _, err := v.UnmarshalMsg(data); err != nil {
return err
}
return nil
case encoding.BinaryUnmarshaler:
return v.UnmarshalBinary(data)
}
return sonic.Unmarshal(data, v)
}