/
caching_client.go
395 lines (354 loc) · 12.2 KB
/
caching_client.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package memcache
import (
"encoding/binary"
"github.com/valyala/ybc/bindings/go/ybc"
"log"
"time"
)
// Memcache client with in-process data caching.
//
// It can save network bandwidth between the client and memcache servers.
//
// The client uses approach similar to HTTP cache validation with entity tags -
// see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.11 .
//
// Usage:
//
// cache := openCache()
// defer cache.Close()
//
// client.Start()
// defer client.Stop()
//
// c := memcache.CachingClient{
// Client: client,
// Cache: cache,
// }
//
// item := memcache.Item{
// Key: []byte("key"),
// Value: []byte("value"),
// }
// if err := c.Set(&item); err != nil {
// log.Fatalf("Error in c.Set(): %s", err)
// }
// if err := c.Get(&item); err != nil {
// log.Fatalf("Error in c.Get(): %s", err)
// }
//
type CachingClient struct {
// The underlying memcache client.
//
// The client must be initialized before passing it here.
//
// Currently Client and DistributedClient may be passed here.
Client Ccacher
// The underlying local cache.
//
// The cache should be initialized before passing it here.
//
// Currently ybc.Cache and ybc.Cluster may be passed here.
Cache ybc.Cacher
}
const metadataSize = casidSize + flagsSize + validateTtlSize + validateExpirationSize
func writeItemMetadata(cache ybc.Cacher, key []byte, size int, casid uint64, flags, validateTtl uint32) *ybc.SetTxn {
validateExpiration64 := uint64(time.Now().Add(time.Duration(validateTtl) * time.Millisecond).UnixNano())
size += metadataSize
txn, err := cache.NewSetTxn(key, size, ybc.MaxTtl)
if err != nil {
log.Printf("Unexpected error in Cache.NewSetTxn(size=%d): [%s]", size, err)
return nil
}
var buf [metadataSize]byte
binary.LittleEndian.PutUint64(buf[:casidSize], casid)
binary.LittleEndian.PutUint32(buf[casidSize:], flags)
binary.LittleEndian.PutUint32(buf[casidSize+flagsSize:], validateTtl)
binary.LittleEndian.PutUint64(buf[casidSize+flagsSize+validateTtlSize:], validateExpiration64)
n, err := txn.Write(buf[:])
if err != nil {
log.Fatalf("Error in SetTxn.Write(): [%s]", err)
}
if n != len(buf) {
log.Fatalf("Unexpected result returned from SetTxn.Write(): %d. Expected %d", n, len(buf))
}
return txn
}
func readItemMetadata(it *ybc.Item) (casid uint64, flags uint32, validateExpiration time.Time, validateTtl uint32, ok bool) {
var buf [metadataSize]byte
n, err := it.Read(buf[:])
if err != nil {
log.Printf("Cannot read metadata for cached item: [%s]", err)
return
}
if n != len(buf) {
log.Printf("Unexpected result returned from ybc.Item.Read(): %d. Expected %d", n, len(buf))
return
}
casid = binary.LittleEndian.Uint64(buf[:])
flags = binary.LittleEndian.Uint32(buf[casidSize:])
validateTtl = binary.LittleEndian.Uint32(buf[casidSize+flagsSize:])
validateExpiration64 := binary.LittleEndian.Uint64(buf[casidSize+flagsSize+validateTtlSize:])
validateExpiration = time.Unix(0, int64(validateExpiration64))
ok = true
return
}
func cacheItem(cache ybc.Cacher, item *Item) error {
if len(item.Value) < validateTtlSize {
log.Printf("Cannot read validateTtl from too short item.Value. Its' size is %d bytes, while expected size should be greater than %d", len(item.Value), validateTtlSize-1)
return ErrCacheMiss
}
validateTtl := binary.LittleEndian.Uint32(item.Value)
item.Value = item.Value[validateTtlSize:]
txn := writeItemMetadata(cache, item.Key, len(item.Value), item.Casid, item.Flags, validateTtl)
if txn == nil {
return nil
}
// do not use defer txn.Commit() for performance reasons
size := len(item.Value)
n, err := txn.Write(item.Value)
if err != nil {
log.Fatalf("Unexpected error in SetTxn.Write(size=%d): [%s]", size, err)
}
if n != size {
log.Fatalf("Unexpected number of bytes written in SetTxn.Write(size=%d): %d", size, n)
}
txn.Commit()
return nil
}
func getAndCacheRemoteItem(client Ccacher, cache ybc.Cacher, item *Item) error {
if err := client.Get(item); err != nil {
return err
}
return cacheItem(cache, item)
}
func getDeAndCacheRemoteItem(client Ccacher, cache ybc.Cacher, item *Item, graceDuration time.Duration) error {
if err := client.GetDe(item, graceDuration); err != nil {
return err
}
return cacheItem(cache, item)
}
func setItemValue(it *ybc.Item, item *Item) error {
size := it.Available()
item.Value = make([]byte, size)
n, err := it.Read(item.Value)
if err != nil {
log.Fatalf("Unexpected error in Item.Read(size=%d): [%s]", size, err)
}
if n != size {
log.Fatalf("Unexpected number of bytes read in Item.Read(size=%d): %d", size, n)
}
return nil
}
func updateLocalItem(cache ybc.Cacher, it *ybc.Item, key []byte, casid uint64, flags, validateTtl uint32) {
size := it.Available()
defer it.Seek(-int64(size), 2)
txn := writeItemMetadata(cache, key, size, casid, flags, validateTtl)
if txn == nil {
return
}
defer txn.Commit()
n, err := txn.ReadFrom(it)
if err != nil {
log.Fatalf("Unexpected error in SetTxn.ReadFrom(size=%d): [%s]", size, err)
}
if n != int64(size) {
log.Fatalf("Unexpected number of bytes copied in SetTxn.ReadFrom(size=%d): %d", size, n)
}
}
func revalidateAndSetItemValueInternal(client Ccacher, cache ybc.Cacher, it *ybc.Item, item *Item, casid uint64, flags, validateTtl uint32, getFunc func() error) error {
if err := getFunc(); err != nil {
if err == ErrNotModified {
if validateTtl > 0 {
updateLocalItem(cache, it, item.Key, casid, flags, validateTtl)
}
return setItemValue(it, item)
}
if err == ErrCacheMiss {
cache.Delete(item.Key)
}
return err
}
return cacheItem(cache, item)
}
func revalidateAndSetItemValue(client Ccacher, cache ybc.Cacher, it *ybc.Item, item *Item, casid uint64, flags, validateTtl uint32) error {
getFunc := func() error {
return client.Cget(item)
}
return revalidateAndSetItemValueInternal(client, cache, it, item, casid, flags, validateTtl, getFunc)
}
func revalidateDeAndSetItemValue(client Ccacher, cache ybc.Cacher, it *ybc.Item, item *Item, casid uint64, flags, validateTtl uint32, graceDuration time.Duration) error {
getFunc := func() error {
return client.CgetDe(item, graceDuration)
}
return revalidateAndSetItemValueInternal(client, cache, it, item, casid, flags, validateTtl, getFunc)
}
// See Client.Get()
func (c *CachingClient) Get(item *Item) error {
it, err := c.Cache.GetItem(item.Key)
if err == ybc.ErrCacheMiss {
return getAndCacheRemoteItem(c.Client, c.Cache, item)
}
if err != nil {
log.Fatalf("Unexpected error returned from Cache.GetItem() for key=[%s]: [%s]", item.Key, err)
}
// do not use defer it.Close() for performance reasons.
casid, flags, validateExpiration, validateTtl, ok := readItemMetadata(it)
if !ok {
it.Close()
return getAndCacheRemoteItem(c.Client, c.Cache, item)
}
item.Casid = casid
item.Flags = flags
if time.Now().After(validateExpiration) {
err = revalidateAndSetItemValue(c.Client, c.Cache, it, item, casid, flags, validateTtl)
} else {
err = setItemValue(it, item)
}
it.Close()
return err
}
// See Client.GetMulti()
func (c *CachingClient) GetMulti(items []Item) error {
// TODO(valyala): optimize this
itemsCount := len(items)
for i := 0; i < itemsCount; i++ {
if err := c.Get(&items[i]); err != nil {
return err
}
}
return nil
}
// See Client.GetDe()
func (c *CachingClient) GetDe(item *Item, graceDuration time.Duration) error {
it, err := c.Cache.GetDeItem(item.Key, graceDuration)
if err == ybc.ErrCacheMiss {
return getDeAndCacheRemoteItem(c.Client, c.Cache, item, graceDuration)
}
if err != nil {
log.Fatalf("Unexpected error returned from Cache.GetDeItem() for key=[%s]: [%s]", item.Key, err)
}
// do not use defer it.Close() for performance reasons.
casid, flags, validateExpiration, validateTtl, ok := readItemMetadata(it)
if !ok {
it.Close()
return getDeAndCacheRemoteItem(c.Client, c.Cache, item, graceDuration)
}
item.Casid = casid
item.Flags = flags
if time.Now().After(validateExpiration) {
err = revalidateDeAndSetItemValue(c.Client, c.Cache, it, item, casid, flags, validateTtl, graceDuration)
} else {
err = setItemValue(it, item)
}
it.Close()
return err
}
func prependValidateTtl(value []byte, validateTtl time.Duration) []byte {
validateTtl32 := uint32(validateTtl / time.Millisecond)
size := len(value) + validateTtlSize
buf := make([]byte, size)
binary.LittleEndian.PutUint32(buf[:], validateTtl32)
copy(buf[validateTtlSize:], value)
return buf
}
// The same as CachingClient.Set(), but sets interval for periodic item
// revalidation on the server.
//
// This means that outdated, locally cached version of the item may be returned
// during validateTtl interval. Use CachingClient.Set() if you don't understand
// how this works.
//
// Setting validateTtl to 0 leads to item re-validation on every get() request.
// This is equivalent to CachingClient.Set() call. Even in this scenario network
// bandiwdth between the client and memcache servers is saved if the average
// item size exceeds ~100 bytes.
func (c *CachingClient) SetWithValidateTtl(item *Item, validateTtl time.Duration) error {
c.Cache.Delete(item.Key)
item.Value = prependValidateTtl(item.Value, validateTtl)
return c.Client.Set(item)
}
// The same as CachingClient.SetWithValidateTtl(), but doesn't wait
// for completion of the operation.
func (c *CachingClient) SetWithValidateTtlNowait(item *Item, validateTtl time.Duration) {
c.Cache.Delete(item.Key)
item.Value = prependValidateTtl(item.Value, validateTtl)
c.Client.SetNowait(item)
}
// See Client.Set()
func (c *CachingClient) Set(item *Item) error {
return c.SetWithValidateTtl(item, 0)
}
// The same as CachingClient.Add(), but sets interval for periodic item
// revalidation on the server.
//
// This means that outdated, locally cached version of the item may be returned
// during validateTtl interval. Use CachingClient.Add() if you don't unserstand
// how this works.
//
// Setting validateTtl to 0 leads to item re-validation on every get() request.
// This is equivalent ot CachingClient.Add() call. Even in this scenario network
// bandwidth between the client and memcache servers is saved if the average
// item size exceeds ~100 bytes.
func (c *CachingClient) AddWithValidateTtl(item *Item, validateTtl time.Duration) error {
c.Cache.Delete(item.Key)
item.Value = prependValidateTtl(item.Value, validateTtl)
return c.Client.Add(item)
}
// See Client.Add()
func (c *CachingClient) Add(item *Item) error {
return c.AddWithValidateTtl(item, 0)
}
// The same as CachingClient.Cas(), but sets interval for periodic item
// revalidation on the server.
//
// This means that outdated, locally cached version of the item may be returned
// during validateTtl interval. Use CachingClient.Cas() if you don't unserstand
// how this works.
//
// Setting validateTtl to 0 leads to item re-validation on every get() request.
// This is equivalent ot CachingClient.Cas() call. Even in this scenario network
// bandwidth between the client and memcache servers is saved if the average
// item size exceeds ~100 bytes.
func (c *CachingClient) CasWithValidateTtl(item *Item, validateTtl time.Duration) error {
c.Cache.Delete(item.Key)
item.Value = prependValidateTtl(item.Value, validateTtl)
return c.Client.Cas(item)
}
// See Client.Cas()
func (c *CachingClient) Cas(item *Item) error {
return c.CasWithValidateTtl(item, 0)
}
// See Client.SetNowait()
func (c *CachingClient) SetNowait(item *Item) {
c.SetWithValidateTtlNowait(item, 0)
}
// See Client.Delete()
func (c *CachingClient) Delete(key []byte) error {
c.Cache.Delete(key)
return c.Client.Delete(key)
}
// See Client.DeleteNowait()
func (c *CachingClient) DeleteNowait(key []byte) {
c.Cache.Delete(key)
c.Client.DeleteNowait(key)
}
// See Client.FlushAll()
func (c *CachingClient) FlushAll() error {
c.Cache.Clear()
return c.Client.FlushAll()
}
// See Client.FlushAllNowait()
func (c *CachingClient) FlushAllNowait() {
c.Cache.Clear()
c.Client.FlushAllNowait()
}
// See Client.FlushAllDelayed()
func (c *CachingClient) FlushAllDelayed(expiration time.Duration) error {
time.AfterFunc(expiration, cacheClearFunc(c.Cache))
return c.Client.FlushAllDelayed(expiration)
}
// See Client.FlushAllDelayedNowait()
func (c *CachingClient) FlushAllDelayedNowait(expiration time.Duration) {
time.AfterFunc(expiration, cacheClearFunc(c.Cache))
c.Client.FlushAllDelayedNowait(expiration)
}