-
Notifications
You must be signed in to change notification settings - Fork 1
/
plain_memcache.go
207 lines (172 loc) · 4.58 KB
/
plain_memcache.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package memproxy
import (
"context"
"sync"
"github.com/QuangTung97/go-memcache/memcache"
)
type plainMemcacheImpl struct {
client *memcache.Client
sessProvider SessionProvider
leaseDuration uint32
}
var _ Memcache = &plainMemcacheImpl{}
type plainPipelineImpl struct {
sess Session
pipeline *memcache.Pipeline
leaseDuration uint32
}
type plainMemcacheConfig struct {
leaseDurationSeconds uint32
sessProvider SessionProvider
}
func computePlainMemcacheConfig(options ...PlainMemcacheOption) *plainMemcacheConfig {
conf := &plainMemcacheConfig{
leaseDurationSeconds: 3,
sessProvider: NewSessionProvider(),
}
for _, fn := range options {
fn(conf)
}
return conf
}
// PlainMemcacheOption ...
type PlainMemcacheOption func(opts *plainMemcacheConfig)
// WithPlainMemcacheLeaseDuration ...
func WithPlainMemcacheLeaseDuration(leaseDurationSeconds uint32) PlainMemcacheOption {
return func(opts *plainMemcacheConfig) {
opts.leaseDurationSeconds = leaseDurationSeconds
}
}
// WithPlainMemcacheSessionProvider ...
func WithPlainMemcacheSessionProvider(sessProvider SessionProvider) PlainMemcacheOption {
return func(opts *plainMemcacheConfig) {
opts.sessProvider = sessProvider
}
}
var _ Pipeline = &plainPipelineImpl{}
// NewPlainMemcache a light wrapper around memcached client
func NewPlainMemcache(
client *memcache.Client,
options ...PlainMemcacheOption,
) Memcache {
conf := computePlainMemcacheConfig(options...)
return &plainMemcacheImpl{
client: client,
sessProvider: conf.sessProvider,
leaseDuration: conf.leaseDurationSeconds,
}
}
// Pipeline ...
func (m *plainMemcacheImpl) Pipeline(_ context.Context, options ...PipelineOption) Pipeline {
conf := ComputePipelineConfig(options)
sess := conf.GetSession(m.sessProvider)
return &plainPipelineImpl{
sess: sess,
pipeline: m.client.Pipeline(),
leaseDuration: m.leaseDuration,
}
}
// Close ...
func (m *plainMemcacheImpl) Close() error {
return m.client.Close()
}
func (p *plainPipelineImpl) LowerSession() Session {
return p.sess.GetLower()
}
// LeaseGet ...
func (p *plainPipelineImpl) LeaseGet(key string, _ LeaseGetOptions) LeaseGetResult {
result, getErr := p.pipeline.MGetFast(key, memcache.MGetOptions{
N: p.leaseDuration,
CAS: true,
})
if getErr != nil {
return LeaseGetErrorResult{Error: getErr}
}
r := getPlainLeaseGetResult()
r.mgetResult = result
return r
}
type plainLeaseGetResult struct {
mgetResult memcache.MGetResult
}
func (r *plainLeaseGetResult) Result() (LeaseGetResponse, error) {
defer putPlainLeaseGetResult(r)
mgetResp, err := r.mgetResult.Result()
memcache.ReleaseMGetResult(r.mgetResult)
if err != nil {
return LeaseGetResponse{}, err
}
if mgetResp.Type != memcache.MGetResponseTypeVA {
return LeaseGetResponse{}, ErrInvalidLeaseGetResponse
}
if mgetResp.Flags == 0 {
return LeaseGetResponse{
Status: LeaseGetStatusFound,
CAS: mgetResp.CAS,
Data: mgetResp.Data,
}, nil
}
if (mgetResp.Flags & memcache.MGetFlagW) > 0 {
return LeaseGetResponse{
Status: LeaseGetStatusLeaseGranted,
CAS: mgetResp.CAS,
}, nil
}
return LeaseGetResponse{
Status: LeaseGetStatusLeaseRejected,
CAS: mgetResp.CAS,
}, nil
}
// LeaseSet ...
func (p *plainPipelineImpl) LeaseSet(
key string, data []byte, cas uint64, options LeaseSetOptions,
) func() (LeaseSetResponse, error) {
fn := p.pipeline.MSet(key, data, memcache.MSetOptions{
CAS: cas,
TTL: options.TTL,
})
return func() (LeaseSetResponse, error) {
resp, err := fn()
if err != nil {
return LeaseSetResponse{}, err
}
status := LeaseSetStatusNotStored
if resp.Type == memcache.MSetResponseTypeHD {
status = LeaseSetStatusStored
}
return LeaseSetResponse{
status,
}, nil
}
}
// Delete ...
func (p *plainPipelineImpl) Delete(key string, _ DeleteOptions) func() (DeleteResponse, error) {
fn := p.pipeline.MDel(key, memcache.MDelOptions{})
return func() (DeleteResponse, error) {
_, err := fn()
return DeleteResponse{}, err
}
}
// Execute ...
func (p *plainPipelineImpl) Execute() {
p.pipeline.Execute()
}
// Finish ...
func (p *plainPipelineImpl) Finish() {
p.pipeline.Finish()
}
// ========================================
// plain memcache lease get result pool
// ========================================
var plainLeaseResultPool = sync.Pool{
New: func() any {
return &plainLeaseGetResult{}
},
}
func getPlainLeaseGetResult() *plainLeaseGetResult {
return plainLeaseResultPool.Get().(*plainLeaseGetResult)
}
func putPlainLeaseGetResult(r *plainLeaseGetResult) {
*r = plainLeaseGetResult{}
plainLeaseResultPool.Put(r)
}