forked from eolinker/apinto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
99 lines (84 loc) · 2.09 KB
/
cache.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package resources
import (
"context"
"time"
"unsafe"
)
const CacheSkill = "github.com/eolinker/apinto/resources.resources.ICache"
type ICache interface {
Set(ctx context.Context, key string, value []byte, expiration time.Duration) StatusResult
SetNX(ctx context.Context, key string, value []byte, expiration time.Duration) BoolResult
DecrBy(ctx context.Context, key string, decrement int64, expiration time.Duration) IntResult
IncrBy(ctx context.Context, key string, decrement int64, expiration time.Duration) IntResult
Get(ctx context.Context, key string) StringResult
GetDel(ctx context.Context, key string) StringResult
Del(ctx context.Context, keys ...string) IntResult
Tx() TX
}
type TX interface {
ICache
Exec(ctx context.Context) error
}
type BoolResult interface {
Result() (bool, error)
}
type IntResult interface {
Result() (int64, error)
}
type StringResult interface {
Result() (string, error)
Bytes() ([]byte, error)
}
type StatusResult interface {
Result() error
}
type statusResult struct {
err error
}
func NewStatusResult(err error) *statusResult {
return &statusResult{err: err}
}
func (s *statusResult) Result() error {
return s.err
}
type stringResult struct {
err error
val string
}
func NewStringResult(val string, err error) *stringResult {
return &stringResult{err: err, val: val}
}
func NewStringResultBytes(value []byte, err error) *stringResult {
return &stringResult{val: *(*string)(unsafe.Pointer(&value)), err: err}
}
func (s *stringResult) Result() (string, error) {
return s.val, s.err
}
func (s *stringResult) Bytes() ([]byte, error) {
return *(*[]byte)(unsafe.Pointer(
&struct {
string
Cap int
}{s.val, len(s.val)},
)), s.err
}
type boolResult struct {
val bool
err error
}
func NewBoolResult(val bool, err error) *boolResult {
return &boolResult{val: val, err: err}
}
func (b *boolResult) Result() (bool, error) {
return b.val, b.err
}
type intResult struct {
val int64
err error
}
func NewIntResult(val int64, err error) *intResult {
return &intResult{val: val, err: err}
}
func (b *intResult) Result() (int64, error) {
return b.val, b.err
}