-
Notifications
You must be signed in to change notification settings - Fork 17
/
redis.go
3700 lines (3461 loc) · 117 KB
/
redis.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
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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package godis
import (
"sync"
"time"
)
// Option connect options
type Option struct {
Host string // redis host
Port int // redis port
ConnectionTimeout time.Duration // connect timeout
SoTimeout time.Duration // read timeout
Password string // redis password,if empty,then without auth
Db int // which db to connect
}
// Redis redis client tool
type Redis struct {
client *client
pipeline *Pipeline
transaction *Transaction
dataSource *Pool
activeTime time.Time
mu sync.RWMutex
}
//NewRedis constructor for creating new redis
func NewRedis(option *Option) *Redis {
client := newClient(option)
return &Redis{client: client}
}
//Connect connect to redis
func (r *Redis) Connect() error {
return r.client.connect()
}
//Close close redis connection
func (r *Redis) Close() error {
if r == nil {
return nil
}
r.mu.Lock()
defer r.mu.Unlock()
if r.dataSource != nil {
if r.client.broken {
return r.dataSource.returnBrokenResourceObject(r)
}
return r.dataSource.returnResourceObject(r)
}
if r.client != nil {
return r.client.close()
}
return nil
}
func (r *Redis) setDataSource(pool *Pool) {
r.mu.Lock()
r.dataSource = pool
r.mu.Unlock()
}
// Send send command to redis
func (r *Redis) Send(command protocolCommand, args ...[]byte) error {
return r.client.sendCommand(command, args...)
}
// SendByStr send command to redis
func (r *Redis) SendByStr(command string, args ...[]byte) error {
return r.client.sendCommandByStr(command, args...)
}
// Receive receive reply from redis
func (r *Redis) Receive() (interface{}, error) {
return r.client.getOne()
}
// check current redis is in transaction or pipeline mode
// if yes,then cannot execute command in redis mode
func (r *Redis) checkIsInMultiOrPipeline() error {
if r.client.isInMulti {
return newDataError("cannot use Redis when in Multi. Please use Transaction or reset redis state")
}
if r.pipeline != nil && len(r.pipeline.pipelinedResponses) > 0 {
return newDataError("cannot use Redis when in Pipeline. Please use Pipeline or reset redis state")
}
return nil
}
//<editor-fold desc="rediscommands">
// Set the string value as value of the key. The string can't be longer than 1073741824 bytes (1 GB)
// return Status code reply
func (r *Redis) Set(key, value string) (string, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return "", err
}
err = r.client.set(key, value)
if err != nil {
return "", err
}
return r.client.getStatusCodeReply()
}
//SetWithParamsAndTime Set the string value as value of the key. The string can't be longer than 1073741824 bytes (1 GB).
// param nxxx NX|XX, NX -- Only set the key if it does not already exist. XX -- Only set the key if it already exist.
// param expx EX|PX, expire time units: EX = seconds; PX = milliseconds
// param time expire time in the units of <code>expx</code>
//return Status code reply
func (r *Redis) SetWithParamsAndTime(key, value, nxxx, expx string, time int64) (string, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return "", err
}
err = r.client.setWithParamsAndTime(key, value, nxxx, expx, time)
if err != nil {
return "", err
}
return r.client.getStatusCodeReply()
}
//Get the value of the specified key. If the key does not exist null is returned. If the value
//stored at key is not a string an error is returned because GET can only handle string values.
//param key
//return Bulk reply
func (r *Redis) Get(key string) (string, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return "", err
}
err = r.client.get(key)
if err != nil {
return "", err
}
return r.client.getBulkReply()
}
//Type Return the type of the value stored at key in form of a string. The type can be one of "none",
//"string", "list", "set". "none" is returned if the key does not exist. Time complexity: O(1)
//param key
//return Status code reply, specifically: "none" if the key does not exist "string" if the key
// contains a String value "list" if the key contains a List value "set" if the key
// contains a Set value "zset" if the key contains a Sorted Set value "hash" if the key
// contains a Hash value
func (r *Redis) Type(key string) (string, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return "", err
}
err = r.client.typeKey(key)
if err != nil {
return "", err
}
return r.client.getBulkReply()
}
//Expire Set a timeout on the specified key. After the timeout the key will be automatically deleted by
//the server. A key with an associated timeout is said to be volatile in Redis terminology.
//
//Voltile keys are stored on disk like the other keys, the timeout is persistent too like all the
//other aspects of the dataset. Saving a dataset containing expires and stopping the server does
//not stop the flow of time as Redis stores on disk the time when the key will no longer be
//available as Unix time, and not the remaining seconds.
//
//Since Redis 2.1.3 you can update the value of the timeout of a key already having an expire
//set. It is also possible to undo the expire at all turning the key into a normal key using the
//{@link #persist(String) PERSIST} command.
//
//return Integer reply, specifically: 1: the timeout was set. 0: the timeout was not set since
// the key already has an associated timeout (this may happen only in Redis versions <
// 2.1.3, Redis >= 2.1.3 will happily update the timeout), or the key does not exist.
func (r *Redis) Expire(key string, seconds int) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.expire(key, seconds)
if err != nil {
return 0, err
}
return r.client.getIntegerReply()
}
//ExpireAt EXPIREAT works exctly like {@link #expire(String, int) EXPIRE} but instead to get the number of
//seconds representing the Time To Live of the key as a second argument (that is a relative way
//of specifying the TTL), it takes an absolute one in the form of a UNIX timestamp (Number of
//seconds elapsed since 1 Gen 1970).
//
//EXPIREAT was introduced in order to implement the Append Only File persistence mode so that
//EXPIRE commands are automatically translated into EXPIREAT commands for the append only file.
//Of course EXPIREAT can also used by programmers that need a way to simply specify that a given
//key should expire at a given time in the future.
//
//Since Redis 2.1.3 you can update the value of the timeout of a key already having an expire
//set. It is also possible to undo the expire at all turning the key into a normal key using the
//{@link #persist(String) PERSIST} command.
//
//return Integer reply, specifically: 1: the timeout was set. 0: the timeout was not set since
// the key already has an associated timeout (this may happen only in Redis versions <
// 2.1.3, Redis >= 2.1.3 will happily update the timeout), or the key does not exist.
func (r *Redis) ExpireAt(key string, unixTimeSeconds int64) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.expireAt(key, unixTimeSeconds)
if err != nil {
return 0, err
}
return r.client.getIntegerReply()
}
//TTL The TTL command returns the remaining time to live in seconds of a key that has an
//{@link #expire(String, int) EXPIRE} set. This introspection capability allows a Redis client to
//check how many seconds a given key will continue to be part of the dataset.
//return Integer reply, returns the remaining time to live in seconds of a key that has an
// EXPIRE. In Redis 2.6 or older, if the Key does not exists or does not have an
// associated expire, -1 is returned. In Redis 2.8 or newer, if the Key does not have an
// associated expire, -1 is returned or if the Key does not exists, -2 is returned.
func (r *Redis) TTL(key string) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.ttl(key)
if err != nil {
return 0, err
}
return r.client.getIntegerReply()
}
// PTTL Like TTL this command returns the remaining time to live of a key that has an expire set,
// with the sole difference that TTL returns the amount of remaining time in seconds while PTTL returns it in milliseconds.
//In Redis 2.6 or older the command returns -1 if the key does not exist or if the key exist but has no associated expire.
//Starting with Redis 2.8 the return value in case of error changed:
//The command returns -2 if the key does not exist.
//The command returns -1 if the key exists but has no associated expire.
//
//Integer reply: TTL in milliseconds, or a negative value in order to signal an error (see the description above).
func (r *Redis) PTTL(key string) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.pttl(key)
if err != nil {
return 0, err
}
return r.client.getIntegerReply()
}
// SetRange Overwrites part of the string stored at key, starting at the specified offset,
// for the entire length of value. If the offset is larger than the current length of the string at key,
// the string is padded with zero-bytes to make offset fit. Non-existing keys are considered as empty strings,
// so this command will make sure it holds a string large enough to be able to set value at offset.
// Note that the maximum offset that you can set is 229 -1 (536870911), as Redis Strings are limited to 512 megabytes.
// If you need to grow beyond this size, you can use multiple keys.
//
// Warning: When setting the last possible byte and the string value stored at key does not yet hold a string value,
// or holds a small string value, Redis needs to allocate all intermediate memory which can block the server for some time.
// On a 2010 MacBook Pro, setting byte number 536870911 (512MB allocation) takes ~300ms,
// setting byte number 134217728 (128MB allocation) takes ~80ms,
// setting bit number 33554432 (32MB allocation) takes ~30ms and setting bit number 8388608 (8MB allocation) takes ~8ms.
// Note that once this first allocation is done,
// subsequent calls to SETRANGE for the same key will not have the allocation overhead.
//
// Return value
// Integer reply: the length of the string after it was modified by the command.
func (r *Redis) SetRange(key string, offset int64, value string) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.setrange(key, offset, value)
if err != nil {
return 0, err
}
return r.client.getIntegerReply()
}
// GetRange Warning: this command was renamed to GETRANGE, it is called SUBSTR in Redis versions <= 2.0.
// Returns the substring of the string value stored at key,
// determined by the offsets start and end (both are inclusive).
// Negative offsets can be used in order to provide an offset starting from the end of the string.
// So -1 means the last character, -2 the penultimate and so forth.
//
// The function handles out of range requests by limiting the resulting range to the actual length of the string.
//
// Return value
// Bulk string reply
func (r *Redis) GetRange(key string, start, end int64) (string, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return "", err
}
err = r.client.getrange(key, start, end)
if err != nil {
return "", err
}
return r.client.getBulkReply()
}
//GetSet GETSET is an atomic set this value and return the old value command. Set key to the string
//value and return the old value stored at key. The string can't be longer than 1073741824 bytes (1 GB).
//
//return Bulk reply
func (r *Redis) GetSet(key, value string) (string, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return "", err
}
err = r.client.getSet(key, value)
if err != nil {
return "", err
}
return r.client.getBulkReply()
}
//SetNx SETNX works exactly like {@link #set(String, String) SET} with the only difference that if the
//key already exists no operation is performed. SETNX actually means "SET if Not eXists".
//
//return Integer reply, specifically: 1 if the key was set 0 if the key was not set
func (r *Redis) SetNx(key, value string) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.setnx(key, value)
if err != nil {
return 0, err
}
return r.client.getIntegerReply()
}
//SetEx The command is exactly equivalent to the following group of commands:
//{@link #set(String, String) SET} + {@link #expire(String, int) EXPIRE}. The operation is atomic.
//
//return Status code reply
func (r *Redis) SetEx(key string, seconds int, value string) (string, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return "", err
}
err = r.client.setex(key, seconds, value)
if err != nil {
return "", err
}
return r.client.getBulkReply()
}
//DecrBy work just like {@link #decr(String) INCR} but instead to decrement by 1 the decrement
//is integer.
//
//INCR commands are limited to 64 bit signed integers.
//
//Note: this is actually a string operation, that is, in Redis there are not "integer" types.
//Simply the string stored at the key is parsed as a base 10 64 bit signed integer, incremented,
//and then converted back as a string.
//
//return Integer reply, this commands will reply with the new value of key after the increment.
func (r *Redis) DecrBy(key string, decrement int64) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.decrBy(key, decrement)
if err != nil {
return 0, err
}
return r.client.getIntegerReply()
}
//Decr Decrement the number stored at key by one. If the key does not exist or contains a value of a
//wrong type, set the key to the value of "0" before to perform the decrement operation.
//
//INCR commands are limited to 64 bit signed integers.
//
//Note: this is actually a string operation, that is, in Redis there are not "integer" types.
//Simply the string stored at the key is parsed as a base 10 64 bit signed integer, incremented,
//and then converted back as a string.
//
//return Integer reply, this commands will reply with the new value of key after the increment.
func (r *Redis) Decr(key string) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.decr(key)
if err != nil {
return 0, err
}
return r.client.getIntegerReply()
}
//IncrBy work just like {@link #incr(String) INCR} but instead to increment by 1 the increment is integer.
//
//INCR commands are limited to 64 bit signed integers.
//
//Note: this is actually a string operation, that is, in Redis there are not "integer" types.
//Simply the string stored at the key is parsed as a base 10 64 bit signed integer, incremented,
//and then converted back as a string.
//
//return Integer reply, this commands will reply with the new value of key after the increment.
func (r *Redis) IncrBy(key string, increment int64) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.incrBy(key, increment)
if err != nil {
return 0, err
}
return r.client.getIntegerReply()
}
//IncrByFloat commands are limited to double precision floating point values.
//
//Note: this is actually a string operation, that is, in Redis there are not "double" types.
//Simply the string stored at the key is parsed as a base double precision floating point value,
//incremented, and then converted back as a string. There is no DECRYBYFLOAT but providing a
//negative value will work as expected.
//
//return Double reply, this commands will reply with the new value of key after the increment.
func (r *Redis) IncrByFloat(key string, increment float64) (float64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.incrByFloat(key, increment)
if err != nil {
return 0, err
}
return StrToFloat64Reply(r.client.getBulkReply())
}
//Incr Increment the number stored at key by one. If the key does not exist or contains a value of a
//wrong type, set the key to the value of "0" before to perform the increment operation.
//
//INCR commands are limited to 64 bit signed integers.
//
//Note: this is actually a string operation, that is, in Redis there are not "integer" types.
//Simply the string stored at the key is parsed as a base 10 64 bit signed integer, incremented,
//and then converted back as a string.
//
//return Integer reply, this commands will reply with the new value of key after the increment.
func (r *Redis) Incr(key string) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.incr(key)
if err != nil {
return 0, err
}
return r.client.getIntegerReply()
}
//Append If the key already exists and is a string, this command appends the provided value at the end
//of the string. If the key does not exist it is created and set as an empty string, so APPEND
//will be very similar to SET in this special case.
//
//Time complexity: O(1). The amortized time complexity is O(1) assuming the appended value is
//small and the already present value is of any size, since the dynamic string library used by
//Redis will double the free space available on every reallocation.
//
//return Integer reply, specifically the total length of the string after the append operation.
func (r *Redis) Append(key, value string) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.append(key, value)
if err != nil {
return 0, err
}
return r.client.getIntegerReply()
}
//SubStr Return a subset of the string from offset start to offset end (both offsets are inclusive).
//Negative offsets can be used in order to provide an offset starting from the end of the string.
//So -1 means the last char, -2 the penultimate and so forth.
//
//The function handles out of range requests without raising an error, but just limiting the
//resulting range to the actual length of the string.
//
//Time complexity: O(start+n) (with start being the start index and n the total length of the
//requested range). Note that the lookup part of this command is O(1) so for small strings this
//is actually an O(1) command.
//
//return Bulk reply
func (r *Redis) SubStr(key string, start, end int) (string, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return "", err
}
err = r.client.substr(key, start, end)
if err != nil {
return "", err
}
return r.client.getBulkReply()
}
//HSet Set the specified hash field to the specified value.
//
//If key does not exist, a new key holding a hash is created.
//
//return If the field already exists, and the HSET just produced an update of the value, 0 is
// returned, otherwise if a new field is created 1 is returned.
func (r *Redis) HSet(key, field, value string) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.hset(key, field, value)
if err != nil {
return 0, err
}
return r.client.getIntegerReply()
}
//HGet If key holds a hash, retrieve the value associated to the specified field.
//
//If the field is not found or the key does not exist, a special 'nil' value is returned.
//
//return Bulk reply
func (r *Redis) HGet(key, field string) (string, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return "", err
}
err = r.client.hget(key, field)
if err != nil {
return "", err
}
return r.client.getBulkReply()
}
//HSetNx Set the specified hash field to the specified value if the field not exists.
//
//return If the field already exists, 0 is returned, otherwise if a new field is created 1 is returned.
func (r *Redis) HSetNx(key, field, value string) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.hsetnx(key, field, value)
if err != nil {
return 0, err
}
return r.client.getIntegerReply()
}
//HMSet Set the respective fields to the respective values.
// HMSET replaces old values with new values.
//
//If key does not exist, a new key holding a hash is created.
//
//return Return OK or Exception if hash is empty
func (r *Redis) HMSet(key string, hash map[string]string) (string, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return "", err
}
err = r.client.hmset(key, hash)
if err != nil {
return "", err
}
return r.client.getBulkReply()
}
//HMGet Retrieve the values associated to the specified fields.
//
//If some of the specified fields do not exist, nil values are returned. Non existing keys are
//considered like empty hashes.
//
//return Multi Bulk Reply specifically a list of all the values associated with the specified
// fields, in the same order of the request.
func (r *Redis) HMGet(key string, fields ...string) ([]string, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return nil, err
}
err = r.client.hmget(key, fields...)
if err != nil {
return nil, err
}
return r.client.getMultiBulkReply()
}
//HIncrBy Increment the number stored at field in the hash at key by value.
// If key does not exist, a new key holding a hash is created.
// If field does not exist or holds a string,
// the value is set to 0 before applying the operation.
// Since the value argument is signed you can use this command to
// perform both increments and decrements.
//
// The range of values supported by HINCRBY is limited to 64 bit signed integers.
//
// return Integer reply The new value at field after the increment operation.
func (r *Redis) HIncrBy(key, field string, value int64) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.hincrBy(key, field, value)
if err != nil {
return 0, err
}
return r.client.getIntegerReply()
}
//HIncrByFloat Increment the number stored at field in the hash at key by a double precision floating point
//value. If key does not exist, a new key holding a hash is created. If field does not exist or
//holds a string, the value is set to 0 before applying the operation. Since the value argument
//is signed you can use this command to perform both increments and decrements.
//
//The range of values supported by HINCRBYFLOAT is limited to double precision floating point
//values.
//
//return Double precision floating point reply The new value at field after the increment
// operation.
func (r *Redis) HIncrByFloat(key, field string, increment float64) (float64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.hincrByFloat(key, field, increment)
if err != nil {
return 0, err
}
return StrToFloat64Reply(r.client.getBulkReply())
}
//HExists Test for existence of a specified field in a hash.
//
// Return 1 if the hash stored at key contains the specified field.
// Return 0 if the key is not found or the field is not present.
func (r *Redis) HExists(key, field string) (bool, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return false, err
}
err = r.client.hexists(key, field)
if err != nil {
return false, err
}
return Int64ToBoolReply(r.client.getIntegerReply())
}
// HDel Remove the specified field from an hash stored at key.
//
// return If the field was present in the hash it is deleted and 1 is returned,
// otherwise 0 is returned and no operation is performed.
func (r *Redis) HDel(key string, fields ...string) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.hdel(key, fields...)
if err != nil {
return 0, err
}
return r.client.getIntegerReply()
}
// HLen Return the number of items in a hash.
//
// return The number of entries (fields) contained in the hash stored at key.
// If the specified key does not exist, 0 is returned assuming an empty hash.
func (r *Redis) HLen(key string) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.hlen(key)
if err != nil {
return 0, err
}
return r.client.getIntegerReply()
}
//HKeys Return all the fields in a hash.
//
//return All the fields names contained into a hash.
func (r *Redis) HKeys(key string) ([]string, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return nil, err
}
err = r.client.hkeys(key)
if err != nil {
return nil, err
}
return r.client.getMultiBulkReply()
}
//HVals Return all the values in a hash.
//
//return All the fields values contained into a hash.
func (r *Redis) HVals(key string) ([]string, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return nil, err
}
err = r.client.hvals(key)
if err != nil {
return nil, err
}
return r.client.getMultiBulkReply()
}
//HGetAll Return all the fields and associated values in a hash.
//
//return All the fields and values contained into a hash.
func (r *Redis) HGetAll(key string) (map[string]string, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return nil, err
}
err = r.client.hgetAll(key)
if err != nil {
return nil, err
}
return StrArrToMapReply(r.client.getMultiBulkReply())
}
//RPush Add the string value to the head (LPUSH) or tail (RPUSH) of the list stored at key. If the key
//does not exist an empty list is created just before the append operation. If the key exists but
//is not a List an error is returned.
//
//return Integer reply, specifically, the number of elements inside the list after the push
// operation.
func (r *Redis) RPush(key string, members ...string) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.rpush(key, members...)
if err != nil {
return 0, err
}
return r.client.getIntegerReply()
}
//LPush Add the string value to the head (LPUSH) or tail (RPUSH) of the list stored at key. If the key
//does not exist an empty list is created just before the append operation. If the key exists but
//is not a List an error is returned.
//
//return Integer reply, specifically, the number of elements inside the list after the push
// operation.
func (r *Redis) LPush(key string, members ...string) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.lpush(key, members...)
if err != nil {
return 0, err
}
return r.client.getIntegerReply()
}
//LLen Return the length of the list stored at the specified key. If the key does not exist zero is
//returned (the same behaviour as for empty lists). If the value stored at key is not a list an
//error is returned.
//
//return The length of the list.
func (r *Redis) LLen(key string) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.llen(key)
if err != nil {
return 0, err
}
return r.client.getIntegerReply()
}
//LRange Return the specified elements of the list stored at the specified key. Start and end are
//zero-based indexes. 0 is the first element of the list (the list head), 1 the next element and
//so on.
//
//For example LRANGE foobar 0 2 will return the first three elements of the list.
//
//start and end can also be negative numbers indicating offsets from the end of the list. For
//example -1 is the last element of the list, -2 the penultimate element and so on.
//
//<b>Consistency with range functions in various programming languages</b>
//
//Note that if you have a list of numbers from 0 to 100, LRANGE 0 10 will return 11 elements,
//that is, rightmost item is included. This may or may not be consistent with behavior of
//range-related functions in your programming language of choice (think Ruby's Range.new,
//Array#slice or Python's range() function).
//
//LRANGE behavior is consistent with one of Tcl.
//
//Indexes out of range will not produce an error: if start is over the end of the list, or start
//> end, an empty list is returned. If end is over the end of the list Redis will threat it
//just like the last element of the list.
//
//return Multi bulk reply, specifically a list of elements in the specified range.
func (r *Redis) LRange(key string, start, stop int64) ([]string, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return nil, err
}
err = r.client.lrange(key, start, stop)
if err != nil {
return nil, err
}
return r.client.getMultiBulkReply()
}
//LTrim Trim an existing list so that it will contain only the specified range of elements specified.
//Start and end are zero-based indexes. 0 is the first element of the list (the list head), 1 the
//next element and so on.
//
//For example LTRIM foobar 0 2 will modify the list stored at foobar key so that only the first
//three elements of the list will remain.
//
//start and end can also be negative numbers indicating offsets from the end of the list. For
//example -1 is the last element of the list, -2 the penultimate element and so on.
//
//Indexes out of range will not produce an error: if start is over the end of the list, or start
//> end, an empty list is left as value. If end over the end of the list Redis will threat it
//just like the last element of the list.
//
//Hint: the obvious use of LTRIM is together with LPUSH/RPUSH. For example:
//
//{@code lpush("mylist", "someelement"); ltrim("mylist", 0, 99); //}
//
//The above two commands will push elements in the list taking care that the list will not grow
//without limits. This is very useful when using Redis to store logs for example. It is important
//to note that when used in this way LTRIM is an O(1) operation because in the average case just
//one element is removed from the tail of the list.
//
//return Status code reply
func (r *Redis) LTrim(key string, start, stop int64) (string, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return "", err
}
err = r.client.ltrim(key, start, stop)
if err != nil {
return "", err
}
return r.client.getBulkReply()
}
//LIndex Return the specified element of the list stored at the specified key. 0 is the first element, 1
//the second and so on. Negative indexes are supported, for example -1 is the last element, -2
//the penultimate and so on.
//
//If the value stored at key is not of list type an error is returned. If the index is out of
//range a 'nil' reply is returned.
//
//return Bulk reply, specifically the requested element
func (r *Redis) LIndex(key string, index int64) (string, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return "", err
}
err = r.client.lindex(key, index)
if err != nil {
return "", err
}
return r.client.getBulkReply()
}
//LSet Set a new value as the element at index position of the List at key.
//
//Out of range indexes will generate an error.
//
//Similarly to other list commands accepting indexes, the index can be negative to access
//elements starting from the end of the list. So -1 is the last element, -2 is the penultimate,
//and so forth.
//
//return Status code reply
func (r *Redis) LSet(key string, index int64, value string) (string, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return "", err
}
err = r.client.lset(key, index, value)
if err != nil {
return "", err
}
return r.client.getBulkReply()
}
//LRem Remove the first count occurrences of the value element from the list. If count is zero all the
//elements are removed. If count is negative elements are removed from tail to head, instead to
//go from head to tail that is the normal behaviour. So for example LREM with count -2 and hello
//as value to remove against the list (a,b,c,hello,x,hello,hello) will lave the list
//(a,b,c,hello,x). The number of removed elements is returned as an integer, see below for more
//information about the returned value. Note that non existing keys are considered like empty
//lists by LREM, so LREM against non existing keys will always return 0.
//
//return Integer Reply, specifically: The number of removed elements if the operation succeeded
func (r *Redis) LRem(key string, count int64, value string) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.lrem(key, count, value)
if err != nil {
return 0, err
}
return r.client.getIntegerReply()
}
//LPop Atomically return and remove the first (LPOP) or last (RPOP) element of the list. For example
//if the list contains the elements "a","b","c" LPOP will return "a" and the list will become
//"b","c".
//
//If the key does not exist or the list is already empty the special value 'nil' is returned.
//
//return Bulk reply
func (r *Redis) LPop(key string) (string, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return "", err
}
err = r.client.lpop(key)
if err != nil {
return "", err
}
return r.client.getBulkReply()
}
//RPop Atomically return and remove the first (LPOP) or last (RPOP) element of the list. For example
//if the list contains the elements "a","b","c" RPOP will return "c" and the list will become
//"a","b".
//
//If the key does not exist or the list is already empty the special value 'nil' is returned.
//
//return Bulk reply
func (r *Redis) RPop(key string) (string, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return "", err
}
err = r.client.rPop(key)
if err != nil {
return "", err
}
return r.client.getBulkReply()
}
//SAdd Add the specified member to the set value stored at key. If member is already a member of the
//set no operation is performed. If key does not exist a new set with the specified member as
//sole member is created. If the key exists but does not hold a set value an error is returned.
//
//return Integer reply, specifically: 1 if the new element was added 0 if the element was
// already a member of the set
func (r *Redis) SAdd(key string, members ...string) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.sAdd(key, members...)
if err != nil {
return 0, err
}
return r.client.getIntegerReply()
}
//SMembers Return all the members (elements) of the set value stored at key.
//
//return Multi bulk reply
func (r *Redis) SMembers(key string) ([]string, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return nil, err
}
err = r.client.sMembers(key)
if err != nil {
return nil, err
}
return r.client.getMultiBulkReply()
}
//SRem Remove the specified member from the set value stored at key. If member was not a member of the
//set no operation is performed. If key does not hold a set value an error is returned.
//
//return Integer reply, specifically: 1 if the new element was removed 0 if the new element was
// not a member of the set
func (r *Redis) SRem(key string, members ...string) (int64, error) {
err := r.checkIsInMultiOrPipeline()
if err != nil {
return 0, err
}
err = r.client.sRem(key, members...)
if err != nil {
return 0, err