-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
828 lines (787 loc) · 21.5 KB
/
cache.go
File metadata and controls
828 lines (787 loc) · 21.5 KB
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
package ignite
import (
"context"
"fmt"
"time"
)
// CachePeekMode define cache peek modes.
type CachePeekMode uint8
const (
PeekAll CachePeekMode = iota // Peeks into all available cache storages.
PeekNear // Peek into near cache only.
PeekPrimary // Peek value from primary copy of partitioned cache only (skip near cache).
PeekBackup // Peek value from backup copies of partitioned cache only (skip near cache).
PeekOnHeap // Peeks value from the on-heap storage only.
PeekOffHeap // Peeks value from the off-heap storage only, without loading off-heap value into cache.
)
const (
DurationUnchanged time.Duration = -2 // Skip setting and leave as is a duration of specific expiry policy.
DurationEternal time.Duration = -1 // Set specific expiry policy as eternal.
DurationZero time.Duration = 0 // Set zero duration of specific expiry policy
)
type KeyValue struct {
Key interface{}
Value interface{}
}
const (
keepBinaryMask uint8 = 0x01
transactionalMask uint8 = 0x02
expiryPolicyMask uint8 = 0x04
)
const (
opCacheGet int16 = 1000
opCachePut int16 = 1001
opCachePutIfAbsent int16 = 1002
opCacheGetAll int16 = 1003
opCachePutAll int16 = 1004
opCacheGetAndPut int16 = 1005
opCacheGetAndReplace int16 = 1006
opCacheGetAndRemove int16 = 1007
opCacheGetAndPutIfAbsent int16 = 1008
opCacheReplace int16 = 1009
opCacheReplaceIfEquals int16 = 1010
opCacheContainsKey int16 = 1011
opCacheContainsKeys int16 = 1012
opCacheClear int16 = 1013
opCacheClearKey int16 = 1014
opCacheClearKeys int16 = 1015
opCacheRemoveKey int16 = 1016
opCacheRemoveIfEquals int16 = 1017
opCacheRemoveKeys int16 = 1018
opCacheRemoveAll int16 = 1019
opCacheGetSize int16 = 1020
opCacheGetConfiguration int16 = 1055
)
// ExpiryPolicy determines when entries will expire based on creation, access and modification operations.
type ExpiryPolicy struct {
creation time.Duration
access time.Duration
update time.Duration
}
// Creation returns the duration before a newly created cache entry is considered expired.
func (e *ExpiryPolicy) Creation() time.Duration {
return e.creation
}
// Access returns the duration before an accessed cache entry is considered expired.
func (e *ExpiryPolicy) Access() time.Duration {
return e.access
}
// Update returns the duration before an updated cache entry is considered expired.
func (e *ExpiryPolicy) Update() time.Duration {
return e.update
}
// Cache is a facade for all Apache Ignite cache operations:
type Cache struct {
cli *Client
expiryPolicy *ExpiryPolicy
name string
id int32
}
// Get gets an entry from the cache by key
func (cache *Cache) Get(ctx context.Context, key interface{}) (interface{}, error) {
if key == nil {
return nil, fmt.Errorf("nil key")
}
var err error = nil
var ret interface{}
cache.cli.ch.send(ctx, opCacheGet, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, key)
if err != nil {
return err
}
return nil
}, func(input BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
return
}
ret, err = cache.cli.marsh.unmarshal(ctx, input)
})
return ret, err
}
// GetAndPut puts a new value to the cache by key, returns old value if exists or nil.
func (cache *Cache) GetAndPut(ctx context.Context, key interface{}, value interface{}) (interface{}, error) {
if key == nil {
return nil, fmt.Errorf("nil key")
}
if value == nil {
return nil, fmt.Errorf("nil value")
}
var ret interface{}
var err error = nil
cache.cli.ch.send(ctx, opCacheGetAndPut, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, key)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, value)
if err != nil {
return err
}
return nil
}, func(input BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
return
}
ret, err = cache.cli.marsh.unmarshal(ctx, input)
})
return ret, err
}
// GetAndReplace atomically replaces the value if and only if an old mapping exists, returns old value if mapping was set or nil otherwise.
func (cache *Cache) GetAndReplace(ctx context.Context, key interface{}, value interface{}) (interface{}, error) {
if key == nil {
return nil, fmt.Errorf("nil key")
}
if value == nil {
return nil, fmt.Errorf("nil value")
}
var ret interface{} = nil
var err error = nil
cache.cli.ch.send(ctx, opCacheGetAndReplace, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, key)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, value)
if err != nil {
return err
}
return nil
}, func(input BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
return
}
ret, err = cache.cli.marsh.unmarshal(ctx, input)
})
return ret, err
}
// Put puts the value with the specified key.
func (cache *Cache) Put(ctx context.Context, key interface{}, value interface{}) error {
if key == nil {
return fmt.Errorf("nil key")
}
if value == nil {
return fmt.Errorf("nil value")
}
var err error = nil
cache.cli.ch.send(ctx, opCachePut, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, key)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, value)
if err != nil {
return err
}
return nil
}, func(input BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
return
}
})
return err
}
// PutIfAbsent puts the value if there is no mapping by the key, returns true if mapping was set and false otherwise.
func (cache *Cache) PutIfAbsent(ctx context.Context, key interface{}, value interface{}) (bool, error) {
if key == nil {
return false, fmt.Errorf("nil key")
}
if value == nil {
return false, fmt.Errorf("nil value")
}
var ret bool
var err error = nil
cache.cli.ch.send(ctx, opCachePutIfAbsent, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, key)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, value)
if err != nil {
return err
}
return nil
}, func(input BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
return
}
ret = input.ReadBool()
})
return ret, err
}
// GetAndPutIfAbsent puts the value if there is no mapping by the key, returns the old value if mapping was set or nil otherwise.
func (cache *Cache) GetAndPutIfAbsent(ctx context.Context, key interface{}, value interface{}) (interface{}, error) {
if key == nil {
return nil, fmt.Errorf("nil key")
}
if value == nil {
return nil, fmt.Errorf("nil value")
}
var ret interface{}
var err error = nil
cache.cli.ch.send(ctx, opCacheGetAndPutIfAbsent, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, key)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, value)
if err != nil {
return err
}
return nil
}, func(input BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
return
}
ret, err = cache.cli.marsh.unmarshal(ctx, input)
})
return ret, err
}
// ContainsKey returns true if key exists, false otherwise.
func (cache *Cache) ContainsKey(ctx context.Context, key interface{}) (bool, error) {
if key == nil {
return false, fmt.Errorf("nil key")
}
var err error = nil
var ret bool
cache.cli.ch.send(ctx, opCacheContainsKey, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, key)
if err != nil {
return err
}
return nil
}, func(input BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
return
}
ret = input.ReadBool()
})
return ret, err
}
// ContainsKeys returns true if all keys exists, false otherwise.
func (cache *Cache) ContainsKeys(ctx context.Context, keys ...interface{}) (bool, error) {
if len(keys) == 0 {
return true, nil
}
var err error
var ret bool
cache.cli.ch.send(ctx, opCacheContainsKeys, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
err = writeSequence(output, len(keys), func(output0 BinaryOutputStream, idx int) error {
key := keys[idx]
if key == nil {
return fmt.Errorf("nil key passed to ContainsKeys")
}
return cache.cli.marsh.marshal(ctx, output0, key)
})
if err != nil {
return err
}
return nil
}, func(input BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
return
}
ret = input.ReadBool()
})
return ret, err
}
// GetAll returns key-value pairs for specified keys.
func (cache *Cache) GetAll(ctx context.Context, keys ...interface{}) ([]KeyValue, error) {
ret := make([]KeyValue, 0)
if len(keys) == 0 {
return ret, nil
}
var err error
cache.cli.ch.send(ctx, opCacheGetAll, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
err = writeSequence(output, len(keys), func(output0 BinaryOutputStream, idx int) error {
key := keys[idx]
if key == nil {
return fmt.Errorf("nil key passed to GetAll")
}
return cache.cli.marsh.marshal(ctx, output0, key)
})
if err != nil {
return err
}
return nil
}, func(input BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
return
}
sz := input.ReadInt32()
if sz == 0 {
return
}
for i := 0; i < int(sz); i++ {
var key, value interface{}
key, err0 = cache.cli.marsh.unmarshal(ctx, input)
if err0 != nil {
err = err0
return
}
value, err0 = cache.cli.marsh.unmarshal(ctx, input)
if err0 != nil {
err = err0
return
}
ret = append(ret, KeyValue{key, value})
}
})
return ret, err
}
// PutAll puts key-value pairs.
func (cache *Cache) PutAll(ctx context.Context, keysAndValues ...KeyValue) error {
lenData := len(keysAndValues)
if lenData == 0 {
return nil
}
var err error
cache.cli.ch.send(ctx, opCachePutAll, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
err = writeSequence(output, len(keysAndValues), func(output0 BinaryOutputStream, idx int) error {
kv := keysAndValues[idx]
if kv.Key == nil {
return fmt.Errorf("nil key passed to PutAll")
}
if kv.Value == nil {
return fmt.Errorf("nil value passed to PutAll")
}
var err0 error
err0 = cache.cli.marsh.marshal(ctx, output0, kv.Key)
if err0 != nil {
return err0
}
err0 = cache.cli.marsh.marshal(ctx, output0, kv.Value)
if err0 != nil {
return err0
}
return nil
})
if err != nil {
return err
}
return nil
}, func(input BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
return
}
})
return err
}
// ReplaceIfEquals replaces old value with new value if and only if the old value equals to specified oldValue, returns true if successful.
func (cache *Cache) ReplaceIfEquals(ctx context.Context, key interface{}, oldValue interface{}, newValue interface{}) (bool, error) {
if key == nil {
return false, fmt.Errorf("nil key")
}
if oldValue == nil {
return false, fmt.Errorf("nil oldValue")
}
if newValue == nil {
return false, fmt.Errorf("nil newValue")
}
var ret bool
var err error = nil
cache.cli.ch.send(ctx, opCacheReplaceIfEquals, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, key)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, oldValue)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, newValue)
if err != nil {
return err
}
return nil
}, func(input BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
return
}
ret = input.ReadBool()
})
return ret, err
}
// Replace replaces old value with specified value if mapping was set, returns true if successful.
func (cache *Cache) Replace(ctx context.Context, key interface{}, value interface{}) (bool, error) {
if key == nil {
return false, fmt.Errorf("nil key")
}
if value == nil {
return false, fmt.Errorf("nil value")
}
var ret bool
var err error = nil
cache.cli.ch.send(ctx, opCacheReplace, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, key)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, value)
if err != nil {
return err
}
return nil
}, func(input BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
return
}
ret = input.ReadBool()
})
return ret, err
}
// Remove removes mapping if it was set, return true if successful.
func (cache *Cache) Remove(ctx context.Context, key interface{}) (bool, error) {
if key == nil {
return false, fmt.Errorf("nil key")
}
var ret bool
var err error = nil
cache.cli.ch.send(ctx, opCacheRemoveKey, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, key)
if err != nil {
return err
}
return nil
}, func(input BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
return
}
ret = input.ReadBool()
})
return ret, err
}
// GetAndRemove removes mapping if it was set, return old value if the mapping was set or nil otherwise.
func (cache *Cache) GetAndRemove(ctx context.Context, key interface{}) (interface{}, error) {
if key == nil {
return false, fmt.Errorf("nil key")
}
var ret interface{} = nil
var err error = nil
cache.cli.ch.send(ctx, opCacheGetAndRemove, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, key)
if err != nil {
return err
}
return nil
}, func(input BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
return
}
ret, err = cache.cli.marsh.unmarshal(ctx, input)
})
return ret, err
}
// RemoveIfEquals removes old value with new value if and only if the old value equals to specified oldValue, returns true if successful.
func (cache *Cache) RemoveIfEquals(ctx context.Context, key interface{}, oldValue interface{}) (bool, error) {
if key == nil {
return false, fmt.Errorf("nil key")
}
if oldValue == nil {
return false, fmt.Errorf("nil oldValue")
}
var ret bool
var err error = nil
cache.cli.ch.send(ctx, opCacheRemoveIfEquals, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, key)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, oldValue)
if err != nil {
return err
}
return nil
}, func(input BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
return
}
ret = input.ReadBool()
})
return ret, err
}
// RemoveKeys removes mappings to specified keys.
func (cache *Cache) RemoveKeys(ctx context.Context, keys ...interface{}) error {
if len(keys) == 0 {
return nil
}
var err error
cache.cli.ch.send(ctx, opCacheRemoveKeys, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
err = writeSequence(output, len(keys), func(output0 BinaryOutputStream, idx int) error {
key := keys[idx]
if key == nil {
return fmt.Errorf("nil key passed to RemoveKeys")
}
return cache.cli.marsh.marshal(ctx, output0, key)
})
if err != nil {
return err
}
return nil
}, func(input BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
return
}
})
return err
}
// RemoveAll removes all cache entries.
func (cache *Cache) RemoveAll(ctx context.Context) error {
var err error
cache.cli.ch.send(ctx, opCacheRemoveAll, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
return nil
}, func(input BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
return
}
})
return err
}
// Clear clears mapping to specified key.
func (cache *Cache) Clear(ctx context.Context, key interface{}) error {
if key == nil {
return fmt.Errorf("nil key")
}
var err error = nil
cache.cli.ch.send(ctx, opCacheClearKey, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
err = cache.cli.marsh.marshal(ctx, output, key)
if err != nil {
return err
}
return nil
}, func(input BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
return
}
})
return err
}
// ClearKeys clears mappings to specified keys.
func (cache *Cache) ClearKeys(ctx context.Context, keys ...interface{}) error {
if len(keys) == 0 {
return nil
}
var err error
cache.cli.ch.send(ctx, opCacheClearKeys, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
err = writeSequence(output, len(keys), func(output0 BinaryOutputStream, idx int) error {
key := keys[idx]
if key == nil {
return fmt.Errorf("nil key passed to ClearKeys")
}
return cache.cli.marsh.marshal(ctx, output0, key)
})
if err != nil {
return err
}
return nil
}, func(input BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
return
}
})
return err
}
// ClearAll clears all cache entries.
func (cache *Cache) ClearAll(ctx context.Context) error {
var err error
cache.cli.ch.send(ctx, opCacheClear, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
return nil
}, func(input BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
return
}
})
return err
}
// Name returns name of the cache.
func (cache *Cache) Name() string {
return cache.name
}
// WithExpiryPolicy returns wrapper that apply expiry policy on all cache operations. NOTE! Does not create a brand-new cache.
func (cache *Cache) WithExpiryPolicy(creation time.Duration, access time.Duration, update time.Duration) *Cache {
return &Cache{
cli: cache.cli,
expiryPolicy: &ExpiryPolicy{
creation: creation,
access: access,
update: update,
},
name: cache.name,
id: cache.id,
}
}
// Size returns cache size according to specified peek mode.
func (cache *Cache) Size(ctx context.Context, peekModes ...CachePeekMode) (uint64, error) {
var size uint64 = 0
var err error = nil
cache.cli.ch.send(ctx, opCacheGetSize, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
peekModesSz := int32(len(peekModes))
output.WriteInt32(peekModesSz)
for _, peekMode := range peekModes {
output.WriteInt8(int8(peekMode))
}
return nil
}, func(input BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
return
}
size = input.ReadUInt64()
})
return size, err
}
// Configuration returns cache [CacheConfiguration] of this cache.
func (cache *Cache) Configuration(ctx context.Context) (CacheConfiguration, error) {
var err error
var cfg CacheConfiguration
cache.cli.ch.send(ctx, opCacheGetConfiguration, func(output BinaryOutputStream) error {
err = cache.writeCacheInfo(cache.cli.ch.protocolContext(), output)
if err != nil {
return err
}
return nil
}, func(output BinaryInputStream, err0 error) {
if err0 != nil {
err = err0
} else {
cfg, err = unmarshalCacheConfiguration(ctx, cache.cli.marsh, output)
}
})
return cfg, err
}
func (cache *Cache) writeCacheInfo(protoCtx *ProtocolContext, output BinaryOutputStream) error {
output.WriteInt32(cache.id)
var flag = keepBinaryMask
if cache.expiryPolicy != nil {
if !protoCtx.SupportsExpiryPolicy() {
return fmt.Errorf("expiry policies are not supported for protocol %v", protoCtx.Version())
}
flag |= expiryPolicyMask
}
output.WriteUInt8(flag)
if flag&expiryPolicyMask != 0 {
output.WriteInt64(durationToMillis(cache.expiryPolicy.Creation()))
output.WriteInt64(durationToMillis(cache.expiryPolicy.Update()))
output.WriteInt64(durationToMillis(cache.expiryPolicy.Access()))
}
return nil
}
func durationToMillis(dur time.Duration) int64 {
if dur > 0 {
return dur.Milliseconds()
} else if dur <= DurationUnchanged {
return int64(DurationUnchanged)
} else {
return int64(dur)
}
}
func millisToDuration(millis int64) time.Duration {
if millis > 0 {
return time.Duration(millis) * time.Millisecond
} else {
return time.Duration(millis)
}
}