This file is indexed.

/usr/share/perl5/Redis.pm is in libredis-perl 2:1.9910-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
#
# This file is part of Redis
#
# This software is Copyright (c) 2015 by Pedro Melo, Damien Krotkine.
#
# This is free software, licensed under:
#
#   The Artistic License 2.0 (GPL Compatible)
#
package Redis;
$Redis::VERSION = '1.991';
# ABSTRACT: Perl binding for Redis database
# VERSION
# AUTHORITY

use warnings;
use strict;

use IO::Socket::INET;
use IO::Socket::UNIX;
use IO::Socket::Timeout;
use IO::Select;
use IO::Handle;
use Fcntl qw( O_NONBLOCK F_SETFL );
use Errno ();
use Data::Dumper;
use Carp;
use Try::Tiny;
use Scalar::Util ();

use Redis::Sentinel;

use constant WIN32       => $^O =~ /mswin32/i;
use constant EWOULDBLOCK => eval {Errno::EWOULDBLOCK} || -1E9;
use constant EAGAIN      => eval {Errno::EAGAIN} || -1E9;
use constant EINTR       => eval {Errno::EINTR} || -1E9;
use constant ECONNRESET  => eval {Errno::ECONNRESET} || -1E9;
use constant BUFSIZE     => 4096;

sub _maybe_enable_timeouts {
    my ($self, $socket) = @_;
    $socket or return;
    exists $self->{read_timeout} || exists $self->{write_timeout}
      or return $socket;
    IO::Socket::Timeout->enable_timeouts_on($socket);
    defined $self->{read_timeout}
      and $socket->read_timeout($self->{read_timeout});
    defined $self->{write_timeout}
      and $socket->write_timeout($self->{write_timeout});
    $socket;
}

sub new {
  my ($class, %args) = @_;
  my $self = bless {}, $class;

  $self->{__buf} = '';
  $self->{debug} = $args{debug} || $ENV{REDIS_DEBUG};

  ## Deal with REDIS_SERVER ENV
  if ($ENV{REDIS_SERVER} && ! exists $args{sock} && ! exists $args{server} && ! exists $args{sentinel}) {
    if ($ENV{REDIS_SERVER} =~ m!^/!) {
      $args{sock} = $ENV{REDIS_SERVER};
    }
    elsif ($ENV{REDIS_SERVER} =~ m!^unix:(.+)!) {
      $args{sock} = $1;
    }
    elsif ($ENV{REDIS_SERVER} =~ m!^(?:tcp:)?(.+)!) {
      $args{server} = $1;
    }
  }

  defined $args{$_}
    and $self->{$_} = $args{$_} for 
      qw(password on_connect name no_auto_connect_on_new cnx_timeout
         write_timeout read_timeout sentinels_cnx_timeout sentinels_write_timeout
         sentinels_read_timeout no_sentinels_list_update);

  $self->{reconnect}     = $args{reconnect} || 0;
  $self->{conservative_reconnect} = $args{conservative_reconnect} || 0;
  $self->{every}         = $args{every} || 1000;

  if (exists $args{sock}) {
    $self->{server} = $args{sock};
    $self->{builder} = sub {
        my ($self) = @_;
        $self->_maybe_enable_timeouts(
            IO::Socket::UNIX->new(
                Peer => $self->{server},
                ( $self->{cnx_timeout} ? ( Timeout => $self->{cnx_timeout} ): () ),
            )
        );
    };
  } elsif ($args{sentinels}) {
      $self->{sentinels} = $args{sentinels};

      ref $self->{sentinels} eq 'ARRAY'
        or croak("'sentinels' param must be an ArrayRef");

      defined($self->{service} = $args{service})
        or croak("Need 'service' name when using 'sentinels'!");

      $self->{builder} = sub {
          my ($self) = @_;
          # try to connect to a sentinel
          my $status;
          foreach my $sentinel_address (@{$self->{sentinels}}) {
              my $sentinel = eval {
                  Redis::Sentinel->new(
                      server => $sentinel_address,
                      cnx_timeout   => (   exists $self->{sentinels_cnx_timeout}
                                         ? $self->{sentinels_cnx_timeout}   : 0.1),
                      read_timeout  => (   exists $self->{sentinels_read_timeout}
                                         ? $self->{sentinels_read_timeout}  : 1  ),
                      write_timeout => (   exists $self->{sentinels_write_timeout}
                                         ? $self->{sentinels_write_timeout} : 1  ),
                  )
              } or next;
              my $server_address = $sentinel->get_service_address($self->{service});
              defined $server_address
                or $status ||= "Sentinels don't know this service",
                   next;
              $server_address eq 'IDONTKNOW'
                and $status = "service is configured in one Sentinel, but was never reached",
                    next;

              # we found the service, set the server
              $self->{server} = $server_address;

              if (! $self->{no_sentinels_list_update} ) {
                  # move the elected sentinel at the front of the list and add
                  # additional sentinels
                  my $idx = 2;
                  my %h = ( ( map { $_ => $idx++ } @{$self->{sentinels}}),
                            $sentinel_address => 1,
                          );
                  $self->{sentinels} = [
                      ( sort { $h{$a} <=> $h{$b} } keys %h ), # sorted existing sentinels,
                      grep { ! $h{$_}; }                      # list of unknown
                      map { +{ @$_ }->{name}; }               # names of
                      $sentinel->sentinel(                    # sentinels 
                        sentinels => $self->{service}         # for this service
                      )
                  ];
              }
              
              return $self->_maybe_enable_timeouts(
                  IO::Socket::INET->new(
                      PeerAddr => $server_address,
                      Proto    => 'tcp',
                      ( $self->{cnx_timeout} ? ( Timeout => $self->{cnx_timeout} ) : () ),
                  )
              );
          }
          croak($status || "failed to connect to any of the sentinels");
      };
  } else {
    $self->{server} = exists $args{server} ? $args{server} : '127.0.0.1:6379';
    $self->{builder} = sub {
        my ($self) = @_;
        $self->_maybe_enable_timeouts(
            IO::Socket::INET->new(
                PeerAddr => $self->{server},
                Proto    => 'tcp',
                ( $self->{cnx_timeout} ? ( Timeout => $self->{cnx_timeout} ) : () ),
            )
        );
    };
  }

  $self->{is_subscriber} = 0;
  $self->{subscribers}   = {};

  $self->connect unless $args{no_auto_connect_on_new};

  return $self;
}

sub is_subscriber { $_[0]{is_subscriber} }

sub select {
  my $self = shift;
  my $database = shift;
  my $ret = $self->__std_cmd('select', $database, @_);
  $self->{current_database} = $database;
  $ret;
}

### we don't want DESTROY to fallback into AUTOLOAD
sub DESTROY { }


### Deal with common, general case, Redis commands
our $AUTOLOAD;

sub AUTOLOAD {
  my $command = $AUTOLOAD;
  $command =~ s/.*://;

  my $method = sub { shift->__std_cmd($command, @_) };

  # Save this method for future calls
  no strict 'refs';
  *$AUTOLOAD = $method;

  goto $method;
}

sub __std_cmd {
  my $self    = shift;
  my $command = shift;

  $self->__is_valid_command($command);

  my $cb = @_ && ref $_[-1] eq 'CODE' ? pop : undef;

  # If this is an EXEC command, in pipelined mode, and one of the commands
  # executed in the transaction yields an error, we must collect all errors
  # from that command, rather than throwing an exception immediately.
  my $uc_command = uc($command);
  my $collect_errors = $cb && $uc_command eq 'EXEC';

  if ($uc_command eq 'MULTI') {
      $self->{__inside_transaction} = 1;
  } elsif ($uc_command eq 'EXEC' || $uc_command eq 'DISCARD') {
      delete $self->{__inside_transaction};
      delete $self->{__inside_watch};
  } elsif ($uc_command eq 'WATCH') {
      $self->{__inside_watch} = 1;
  } elsif ($uc_command eq 'UNWATCH') {
      delete $self->{__inside_watch};
  }

  ## Fast path, no reconnect;
  $self->{reconnect}
    or return $self->__run_cmd($command, $collect_errors, undef, $cb, @_);

  my @cmd_args = @_;
  $self->__with_reconnect(
    sub {
      $self->__run_cmd($command, $collect_errors, undef, $cb, @cmd_args);
    }
  );
}

sub __with_reconnect {
  my ($self, $cb) = @_;

  ## Fast path, no reconnect
  $self->{reconnect}
    or return $cb->();

  return &try(
    $cb,
    catch {
      ref($_) eq 'Redis::X::Reconnect'
        or die $_;

      $self->{__inside_transaction} || $self->{__inside_watch}
        and croak("reconnect disabled inside transaction or watch");

      scalar @{$self->{queue} || []} && $self->{conservative_reconnect}
        and croak("reconnect disabled while responses are pending and conservative reconnect mode enabled");

      $self->connect;
      $cb->();
    }
  );
}

sub __run_cmd {
  my ($self, $command, $collect_errors, $custom_decode, $cb, @args) = @_;

  my $ret;
  my $wrapper = $cb && $custom_decode
    ? sub {
      my ($reply, $error) = @_;
      $cb->(scalar $custom_decode->($reply), $error);
    }
    : $cb || sub {
      my ($reply, $error) = @_;
      croak "[$command] $error, " if defined $error;
      $ret = $reply;
    };

  $self->__send_command($command, @args);
  push @{ $self->{queue} }, [$command, $wrapper, $collect_errors];

  return 1 if $cb;

  $self->wait_all_responses;
  return
      $custom_decode ? $custom_decode->($ret, !wantarray)
    : wantarray && ref $ret eq 'ARRAY' ? @$ret
    :                                    $ret;
}

sub wait_all_responses {
  my ($self) = @_;

  my $queue = $self->{queue};
  $self->wait_one_response while @$queue;

  return;
}

sub wait_one_response {
  my ($self) = @_;

  my $handler = shift @{ $self->{queue} };
  return unless $handler;

  my ($command, $cb, $collect_errors) = @$handler;
  $cb->($self->__read_response($command, $collect_errors));

  return;
}


### Commands with extra logic
sub quit {
  my ($self) = @_;
  return unless $self->{sock};

  croak "[quit] only works in synchronous mode, "
    if @_ && ref $_[-1] eq 'CODE';

  try {
    $self->wait_all_responses;
    $self->__send_command('QUIT');
  };

  $self->__close_sock();

  return 1;
}

sub shutdown {
  my ($self) = @_;
  $self->__is_valid_command('SHUTDOWN');

  croak "[shutdown] only works in synchronous mode, "
    if @_ && ref $_[-1] eq 'CODE';

  return unless $self->{sock};

  $self->wait_all_responses;
  $self->__send_command('SHUTDOWN');
  $self->__close_sock() || croak("Can't close socket: $!");

  return 1;
}

sub ping {
  my $self = shift;
  $self->__is_valid_command('PING');

  croak "[ping] only works in synchronous mode, "
    if @_ && ref $_[-1] eq 'CODE';

  return unless exists $self->{sock};

  $self->wait_all_responses;
  return scalar try {
    $self->__std_cmd('PING');
  }
  catch {
    $self->__close_sock();
    return;
  };
}

sub info {
  my $self = shift;
  $self->__is_valid_command('INFO');

  my $custom_decode = sub {
    my ($reply) = @_;
    return $reply if !defined $reply || ref $reply;
    return { map { split(/:/, $_, 2) } grep {/^[^#]/} split(/\r\n/, $reply) };
  };

  my $cb = @_ && ref $_[-1] eq 'CODE' ? pop : undef;

  ## Fast path, no reconnect
  return $self->__run_cmd('INFO', 0, $custom_decode, $cb, @_)
    unless $self->{reconnect};

  my @cmd_args = @_;
  $self->__with_reconnect(
    sub {
      $self->__run_cmd('INFO', 0, $custom_decode, $cb, @cmd_args);
    }
  );
}

sub keys {
  my $self = shift;
  $self->__is_valid_command('KEYS');

  my $custom_decode = sub {
    my ($reply, $synchronous_scalar) = @_;

    ## Support redis <= 1.2.6
    $reply = [split(/\s/, $reply)] if defined $reply && !ref $reply;

    return ref $reply && ($synchronous_scalar || wantarray) ? @$reply : $reply;
  };

  my $cb = @_ && ref $_[-1] eq 'CODE' ? pop : undef;

  ## Fast path, no reconnect
  return $self->__run_cmd('KEYS', 0, $custom_decode, $cb, @_)
    unless $self->{reconnect};

  my @cmd_args = @_;
  $self->__with_reconnect(
    sub {
      $self->__run_cmd('KEYS', 0, $custom_decode, $cb, @cmd_args);
    }
  );
}


### PubSub
sub wait_for_messages {
  my ($self, $timeout) = @_;

  my $s = IO::Select->new;

  my $count = 0;


  my $e;

  try {
    $self->__with_reconnect( sub {

      # the socket can be changed due to reconnection, so get it each time
      my $sock = $self->{sock};
      $s->remove($s->handles);
      $s->add($sock);

      while ($s->can_read($timeout)) {      
        my $has_stuff = $self->__try_read_sock($sock);
        # If the socket is ready to read but there is nothing to read, ( so
        # it's an EOF ), try to reconnect.
        defined $has_stuff
          or $self->__throw_reconnect('EOF from server');

        do {
          my ($reply, $error) = $self->__read_response('WAIT_FOR_MESSAGES');
          croak "[WAIT_FOR_MESSAGES] $error, " if defined $error;
          $self->__process_pubsub_msg($reply);
          $count++;

          # if __try_read_sock() return 0 (no data)
          # or undef ( socket became EOF), back to select until timeout
        } while ($self->{__buf} || $self->__try_read_sock($sock));
      }
    
    });

  } catch {
    $e = $_;
};

# if We had an error and it was not an EOF, die
defined $e && $e ne 'EOF from server'
  and die $e;

  return $count;
}

sub __subscription_cmd {
  my $self    = shift;
  my $pr      = shift;
  my $unsub   = shift;
  my $command = shift;
  my $cb      = pop;

  croak("Missing required callback in call to $command(), ")
    unless ref($cb) eq 'CODE';

  $self->wait_all_responses;

  my @subs = @_;
  $self->__with_reconnect(
    sub {
      $self->__throw_reconnect('Not connected to any server')
        unless $self->{sock};

      @subs = $self->__process_unsubscribe_requests($cb, $pr, @subs)
        if $unsub;
      return unless @subs;

      $self->__send_command($command, @subs);

      my %cbs = map { ("${pr}message:$_" => $cb) } @subs;
      return $self->__process_subscription_changes($command, \%cbs);
    }
  );
}

sub subscribe    { shift->__subscription_cmd('',  0, subscribe    => @_) }
sub psubscribe   { shift->__subscription_cmd('p', 0, psubscribe   => @_) }
sub unsubscribe  { shift->__subscription_cmd('',  1, unsubscribe  => @_) }
sub punsubscribe { shift->__subscription_cmd('p', 1, punsubscribe => @_) }

sub __process_unsubscribe_requests {
  my ($self, $cb, $pr, @unsubs) = @_;
  my $subs = $self->{subscribers};

  my @subs_to_unsubscribe;
  for my $sub (@unsubs) {
    my $key = "${pr}message:$sub";
    my $cbs = $subs->{$key} = [grep { $_ ne $cb } @{ $subs->{$key} }];
    next if @$cbs;

    delete $subs->{$key};
    push @subs_to_unsubscribe, $sub;
  }

  return @subs_to_unsubscribe;
}

sub __process_subscription_changes {
  my ($self, $cmd, $expected) = @_;
  my $subs = $self->{subscribers};

  while (%$expected) {
    my ($m, $error) = $self->__read_response($cmd);
    croak "[$cmd] $error, " if defined $error;

    ## Deal with pending PUBLISH'ed messages
    if ($m->[0] =~ /^p?message$/) {
      $self->__process_pubsub_msg($m);
      next;
    }

    my ($key, $unsub) = $m->[0] =~ m/^(p)?(un)?subscribe$/;
    $key .= "message:$m->[1]";
    my $cb = delete $expected->{$key};

    push @{ $subs->{$key} }, $cb unless $unsub;

    $self->{is_subscriber} = $m->[2];
  }
}

sub __process_pubsub_msg {
  my ($self, $m) = @_;
  my $subs = $self->{subscribers};

  my $sub   = $m->[1];
  my $cbid  = "$m->[0]:$sub";
  my $data  = pop @$m;
  my $topic = defined $m->[2] ? $m->[2] : $sub;

  if (!exists $subs->{$cbid}) {
    warn "Message for topic '$topic' ($cbid) without expected callback, ";
    return;
  }

  $_->($data, $topic, $sub) for @{ $subs->{$cbid} };

  return 1;

}


### Mode validation
sub __is_valid_command {
  my ($self, $cmd) = @_;

  croak("Cannot use command '$cmd' while in SUBSCRIBE mode, ")
    if $self->{is_subscriber};
}


### Socket operations
sub connect {
  my ($self) = @_;
  delete $self->{sock};
  delete $self->{__inside_watch};
  delete $self->{__inside_transaction};

  # Suppose we have at least one command response pending, but we're about
  # to reconnect.  The new connection will never get a response to any of
  # the pending commands, so delete all those pending responses now.
  $self->{queue} = [];
  $self->{pid}   = $$;

  ## Fast path, no reconnect
  return $self->__build_sock() unless $self->{reconnect};

  ## Use precise timers on reconnections
  require Time::HiRes;
  my $t0 = [Time::HiRes::gettimeofday()];

  ## Reconnect...
  while (1) {
    eval { $self->__build_sock };

    last unless $@;    ## Connected!
    die if Time::HiRes::tv_interval($t0) > $self->{reconnect};    ## Timeout
    Time::HiRes::usleep($self->{every});                          ## Retry in...
  }

  return;
}

sub __build_sock {
  my ($self) = @_;

  $self->{sock} = $self->{builder}->($self)
    || croak("Could not connect to Redis server at $self->{server}: $!");

  $self->{__buf} = '';

  if (exists $self->{password}) {
    try { $self->auth($self->{password}) }
    catch {
      my $error = $_;
      $self->{reconnect} = 0;
      croak('Redis server authentication error: ' . $error);
    };
  }

  $self->__on_connection;

  return;
}

sub __close_sock {
  my ($self) = @_;
  $self->{__buf} = '';
  delete $self->{__inside_watch};
  delete $self->{__inside_transaction};
  defined $self->{sock} or return 1;
  return close(delete $self->{sock});
}

sub __on_connection {

    my ($self) = @_;

    # If we are in PubSub mode we shouldn't perform any command besides
    # (p)(un)subscribe
    if (! $self->{is_subscriber}) {
      defined $self->{name}
        and try {
            my $n = $self->{name};
            $n = $n->($self) if ref($n) eq 'CODE';
            $self->client_setname($n) if defined $n;
        };
  
      defined $self->{current_database}
        and $self->select($self->{current_database});
    }

    foreach my $topic (CORE::keys(%{$self->{subscribers}})) {
      if ($topic =~ /(p?message):(.*)$/ ) {
        my ($key, $channel) = ($1, $2);
        if ($key eq 'message') {
            $self->__send_command('subscribe', $channel);
            my (undef, $error) = $self->__read_response('subscribe');
            defined $error
              and croak "[subscribe] $error";
        } else {
            $self->__send_command('psubscribe', $channel);
            my (undef, $error) = $self->__read_response('psubscribe');
            defined $error
              and croak "[psubscribe] $error";
        }
      }
    }

    defined $self->{on_connect}
      and $self->{on_connect}->($self);

}


sub __send_command {
  my $self = shift;
  my $cmd  = uc(shift);
  my $deb  = $self->{debug};

  # if already connected but after a fork, reconnect
  if ($self->{sock} && ($self->{pid} || 0) != $$) {
    $self->connect;
  }

  my $sock = $self->{sock}
    || $self->__throw_reconnect('Not connected to any server');

  warn "[SEND] $cmd ", Dumper([@_]) if $deb;

  ## Encode command using multi-bulk format
  my @cmd     = split /_/, $cmd;
  my $n_elems = scalar(@_) + scalar(@cmd);
  my $buf     = "\*$n_elems\r\n";
  for my $bin (@cmd, @_) {
    utf8::downgrade($bin, 1)
      or croak "command sent is not an octet sequence in the native encoding (Latin-1). Consider using debug mode to see the command itself.";
    $buf .= defined($bin) ? '$' . length($bin) . "\r\n$bin\r\n" : "\$-1\r\n";
  }

  ## Check to see if socket was closed: reconnect on EOF
  my $status = $self->__try_read_sock($sock);
  $self->__throw_reconnect('Not connected to any server')
    unless defined $status;

  ## Send command, take care for partial writes
  warn "[SEND RAW] $buf" if $deb;
  while ($buf) {
    my $len = syswrite $sock, $buf, length $buf;
    $self->__throw_reconnect("Could not write to Redis server: $!")
      unless defined $len;
    substr $buf, 0, $len, "";
  }

  return;
}

sub __read_response {
  my ($self, $cmd, $collect_errors) = @_;

  croak("Not connected to any server") unless $self->{sock};

  local $/ = "\r\n";

  ## no debug => fast path
  return $self->__read_response_r($cmd, $collect_errors) unless $self->{debug};

  my ($result, $error) = $self->__read_response_r($cmd, $collect_errors);
  warn "[RECV] $cmd ", Dumper($result, $error);
  return $result, $error;
}

sub __read_response_r {
  my ($self, $command, $collect_errors) = @_;

  my ($type, $result) = $self->__read_line;

  if ($type eq '-') {
    return undef, $result;
  }
  elsif ($type eq '+' || $type eq ':') {
    return $result, undef;
  }
  elsif ($type eq '$') {
    return undef, undef if $result < 0;
    return $self->__read_len($result + 2), undef;
  }
  elsif ($type eq '*') {
    return undef, undef if $result < 0;

    my @list;
    while ($result--) {
      my @nested = $self->__read_response_r($command, $collect_errors);
      if ($collect_errors) {
        push @list, \@nested;
      }
      else {
        croak "[$command] $nested[1], " if defined $nested[1];
        push @list, $nested[0];
      }
    }
    return \@list, undef;
  }
  else {
    croak "unknown answer type: $type ($result), ";
  }
}

sub __read_line {
  my $self = $_[0];
  my $sock = $self->{sock};

  my $data = $self->__read_line_raw;
  if (! defined $data) {
      # In case the caller catches the exception and wants to persist on using
      # the redis connection, let's forbid that.
      $self->__close_sock();
      croak("Error while reading from Redis server: $!")
  }

  chomp $data;
  warn "[RECV RAW] '$data'" if $self->{debug};

  my $type = substr($data, 0, 1, '');
  return ($type, $data);
}

sub __read_line_raw {
  my $self = $_[0];
  my $sock = $self->{sock};
  my $buf = \$self->{__buf};

  if (length $$buf) {
    my $idx = index($$buf, "\r\n");
    $idx >= 0 and return substr($$buf, 0, $idx + 2, '');
  }

  while (1) {
    my $bytes = sysread($sock, $$buf, BUFSIZE, length($$buf));
    next if !defined $bytes && $! == EINTR;
    return unless defined $bytes && $bytes;

    # start looking for \r\n where we stopped last time
    # extracting one is required to handle corner case
    # where \r\n are split and therefore read by two conseqent sysreads
    my $idx = index($$buf, "\r\n", length($$buf) - $bytes - 1);
    $idx >= 0 and return substr($$buf, 0, $idx + 2, '');
  }
}

sub __read_len {
  my ($self, $len) = @_;
  my $buf = \$self->{__buf};
  my $buflen = length($$buf);

  if ($buflen < $len) {
    my $to_read = $len - $buflen;
    while ($to_read > 0) {
      my $bytes = sysread($self->{sock}, $$buf, BUFSIZE, length($$buf));
      next if !defined $bytes && $! == EINTR;
      croak("Error while reading from Redis server: $!") unless defined $bytes;
      croak("Redis server closed connection") unless $bytes;
      $to_read -= $bytes;
    }
  }

  my $data = substr($$buf, 0, $len, '');
  chomp $data;
  warn "[RECV RAW] '$data'" if $self->{debug};

  return $data;
}

sub __try_read_sock {
  my ($self, $sock) = @_;
  my $data = '';

  while (1) {
      # WIN32 doesn't support MSG_DONTWAIT,
      # need to swith fh to nonblockng mode manually.
      # For Unix still use MSG_DONTWAIT because of fewer syscalls
      my ($res, $err);
      if (WIN32) {
          __fh_nonblocking_win32($sock, 1);
          $res = recv($sock, $data, BUFSIZE, 0);
          $err = 0 + $!;
          __fh_nonblocking_win32($sock, 0);
      } else {
          $res = recv($sock, $data, BUFSIZE, MSG_DONTWAIT);
          $err = 0 + $!;
      }

      if (defined $res) {
        ## have read some data
        if (length($data)) {
            $self->{__buf} .= $data;
            return 1;
        }

        ## no data but also no error means EOF
        return;
      }

      next if $err && $err == EINTR;

      ## Keep going if nothing there, but socket is alive
      return 0 if $err and ($err == EWOULDBLOCK or $err == EAGAIN);

      ## on freebsd, if we got ECONNRESET, it's a timeout from the other side
      return 0 if ($err && $err == ECONNRESET && $^O eq 'freebsd');

      ## result is undef but err is 0? should never happen
      return if $err == 0;

      ## For everything else, there is Mastercard...
      croak("Unexpected error condition $err/$^O, please report this as a bug");
  }
}

## Copied from AnyEvent::Util
sub __fh_nonblocking_win32 {
    ioctl $_[0], 0x8004667e, pack "L", $_[1];
}

##########################
# I take exception to that

sub __throw_reconnect {
  my ($self, $m) = @_;
  die bless(\$m, 'Redis::X::Reconnect') if $self->{reconnect};
  die $m;
}


1;    # End of Redis.pm

__END__

=pod

=encoding UTF-8

=head1 NAME

Redis - Perl binding for Redis database

=head1 VERSION

version 1.991

=head1 SYNOPSIS

    ## Defaults to $ENV{REDIS_SERVER} or 127.0.0.1:6379
    my $redis = Redis->new;

    my $redis = Redis->new(server => 'redis.example.com:8080');

    ## Set the connection name (requires Redis 2.6.9)
    my $redis = Redis->new(
      server => 'redis.example.com:8080',
      name => 'my_connection_name',
    );
    my $generation = 0;
    my $redis = Redis->new(
      server => 'redis.example.com:8080',
      name => sub { "cache-$$-".++$generation },
    );

    ## Use UNIX domain socket
    my $redis = Redis->new(sock => '/path/to/socket');

    ## Enable auto-reconnect
    ## Try to reconnect every 1s up to 60 seconds until success
    ## Die if you can't after that
    my $redis = Redis->new(reconnect => 60, every => 1_000_000);

    ## Try each 100ms up to 2 seconds (every is in microseconds)
    my $redis = Redis->new(reconnect => 2, every => 100_000);

    ## Enable connection timeout (in seconds)
    my $redis = Redis->new(cnx_timeout => 60);

    ## Enable read timeout (in seconds)
    my $redis = Redis->new(read_timeout => 0.5);

    ## Enable write timeout (in seconds)
    my $redis = Redis->new(write_timeout => 1.2);

    ## Connect via a list of Sentinels to a given service
    my $redis = Redis->new(sentinels => [ '127.0.0.1:12345' ], service => 'mymaster');

    ## Same, but with connection, read and write timeout on the sentinel hosts
    my $redis = Redis->new( sentinels => [ '127.0.0.1:12345' ], service => 'mymaster',
                            sentinels_cnx_timeout => 0.1,
                            sentinels_read_timeout => 1,
                            sentinels_write_timeout => 1,
                          );

    ## Use all the regular Redis commands, they all accept a list of
    ## arguments
    ## See http://redis.io/commands for full list
    $redis->get('key');
    $redis->set('key' => 'value');
    $redis->sort('list', 'DESC');
    $redis->sort(qw{list LIMIT 0 5 ALPHA DESC});

    ## Add a coderef argument to run a command in the background
    $redis->sort(qw{list LIMIT 0 5 ALPHA DESC}, sub {
      my ($reply, $error) = @_;
      die "Oops, got an error: $error\n" if defined $error;
      print "$_\n" for @$reply;
    });
    long_computation();
    $redis->wait_all_responses;
    ## or
    $redis->wait_one_response();

    ## Or run a large batch of commands in a pipeline
    my %hash = _get_large_batch_of_commands();
    $redis->hset('h', $_, $hash{$_}, sub {}) for keys %hash;
    $redis->wait_all_responses;

    ## Publish/Subscribe
    $redis->subscribe(
      'topic_1',
      'topic_2',
      sub {
        my ($message, $topic, $subscribed_topic) = @_

          ## $subscribed_topic can be different from topic if
          ## you use psubscribe() with wildcards
      }
    );
    $redis->psubscribe('nasdaq.*', sub {...});

    ## Blocks and waits for messages, calls subscribe() callbacks
    ##  ... forever
    my $timeout = 10;
    $redis->wait_for_messages($timeout) while 1;

    ##  ... until some condition
    my $keep_going = 1; ## other code will set to false to quit
    $redis->wait_for_messages($timeout) while $keep_going;

    $redis->publish('topic_1', 'message');

=head1 DESCRIPTION

Pure perl bindings for L<http://redis.io/>

This version supports protocol 2.x (multi-bulk) or later of Redis available at
L<https://github.com/antirez/redis/>.

This documentation lists commands which are exercised in test suite, but
additional commands will work correctly since protocol specifies enough
information to support almost all commands with same piece of code with a
little help of C<AUTOLOAD>.

=head1 PIPELINING

Usually, running a command will wait for a response.  However, if you're doing
large numbers of requests, it can be more efficient to use what Redis calls
I<pipelining>: send multiple commands to Redis without waiting for a response,
then wait for the responses that come in.

To use pipelining, add a coderef argument as the last argument to a command
method call:

  $r->set('foo', 'bar', sub {});

Pending responses to pipelined commands are processed in a single batch, as
soon as at least one of the following conditions holds:

=over

=item *

A non-pipelined (synchronous) command is called on the same connection

=item *

A pub/sub subscription command (one of C<subscribe>, C<unsubscribe>,
C<psubscribe>, or C<punsubscribe>) is about to be called on the same
connection.

=item *

One of L</wait_all_responses> or L</wait_one_response> methods is called
explicitly.

=back

The coderef you supply to a pipelined command method is invoked once the
response is available.  It takes two arguments, C<$reply> and C<$error>.  If
C<$error> is defined, it contains the text of an error reply sent by the Redis
server.  Otherwise, C<$reply> is the non-error reply. For almost all commands,
that means it's C<undef>, or a defined but non-reference scalar, or an array
ref of any of those; but see L</keys>, L</info>, and L</exec>.

Note the contrast with synchronous commands, which throw an exception on
receipt of an error reply, or return a non-error reply directly.

The fact that pipelined commands never throw an exception can be particularly
useful for Redis transactions; see L</exec>.

=head1 ENCODING

There is no encoding feature anymore, it has been deprecated and finally
removed. This module consider that any data sent to the Redis server is a binary data.
And it doesn't do anything when getting data from the Redis server.

So, if you are working with character strings, you should pre-encode or post-decode it if needed !

=head1 CONSTRUCTOR

=head2 new

    my $r = Redis->new; # $ENV{REDIS_SERVER} or 127.0.0.1:6379

    my $r = Redis->new( server => '192.168.0.1:6379', debug => 0 );
    my $r = Redis->new( server => '192.168.0.1:6379', encoding => undef );
    my $r = Redis->new( sock => '/path/to/sock' );
    my $r = Redis->new( reconnect => 60, every => 5000 );
    my $r = Redis->new( password => 'boo' );
    my $r = Redis->new( on_connect => sub { my ($redis) = @_; ... } );
    my $r = Redis->new( name => 'my_connection_name' );
    my $r = Redis->new( name => sub { "cache-for-$$" });

    my $redis = Redis->new(sentinels => [ '127.0.0.1:12345', '127.0.0.1:23456' ],
                           service => 'mymaster');

    ## Connect via a list of Sentinels to a given service
    my $redis = Redis->new(sentinels => [ '127.0.0.1:12345' ], service => 'mymaster');

    ## Same, but with connection, read and write timeout on the sentinel hosts
    my $redis = Redis->new( sentinels => [ '127.0.0.1:12345' ], service => 'mymaster',
                            sentinels_cnx_timeout => 0.1,
                            sentinels_read_timeout => 1,
                            sentinels_write_timeout => 1,
                          );

=head3 C<< server >>

The C<< server >> parameter specifies the Redis server we should connect to,
via TCP. Use the 'IP:PORT' format. If no C<< server >> option is present, we
will attempt to use the C<< REDIS_SERVER >> environment variable. If neither of
those options are present, it defaults to '127.0.0.1:6379'.

Alternatively you can use the C<< sock >> parameter to specify the path of the
UNIX domain socket where the Redis server is listening.

Alternatively you can use the C<< sentinels >> parameter and the C<< service >>
parameter to specify a list of sentinels to contact and try to get the address
of the given service name. C<< sentinels >> must be an ArrayRef and C<< service
>> an Str.

The C<< REDIS_SERVER >> can be used for UNIX domain sockets too. The following
formats are supported:

=over

=item *

/path/to/sock

=item *

unix:/path/to/sock

=item *

127.0.0.1:11011

=item *

tcp:127.0.0.1:11011

=back

=head3 C<< reconnect >>, C<< every >>

The C<< reconnect >> option enables auto-reconnection mode. If we cannot
connect to the Redis server, or if a network write fails, we enter retry mode.
We will try a new connection every C<< every >> microseconds (1 ms by
default), up-to C<< reconnect >> seconds.

Be aware that read errors will always thrown an exception, and will not trigger
a retry until the new command is sent.

If we cannot re-establish a connection after C<< reconnect >> seconds, an
exception will be thrown.

=head3 C<< conservative_reconnect >>

C<< conservative_reconnect >> option makes sure that reconnection is only attempted
when no pending command is ongoing. For instance, if you're doing
C<$redis->incr('key')>, and if the server properly understood and processed the
command, but the network connection is dropped just before the server replies :
the command has been processed but the client doesn't know it. In this
situation, if reconnect is enabled, the Redis client will reconnect and send
the C<incr> command *again*. If it succeeds, at the end the key as been
incremented *two* times. To avoid this issue, you can set the C<conservative_reconnect>
option to a true value. In this case, the client will reconnect only if no
request is pending. Otherwise it will die with the message: C<reconnect
disabled while responses are pending and safe reconnect mode enabled>.

=head3 C<< cnx_timeout >>

The C<< cnx_timeout >> option enables connection timeout. The Redis client will
wait at most that number of seconds (can be fractional) before giving up
connecting to a server.

=head3 C<< sentinels_cnx_timeout >>

The C<< sentinels_cnx_timeout >> option enables sentinel connection timeout.
When using the sentinels feature, Redis client will wait at most that number of
seconds (can be fractional) before giving up connecting to a sentinel.
B<Default>: 0.1

=head3 C<< read_timeout >>

The C<< read_timeout >> option enables read timeout. The Redis client will wait
at most that number of seconds (can be fractional) before giving up when
reading from the server.

=head3 C<< sentinels_read_timeout >>

The C<< sentinels_read_timeout >> option enables sentinel read timeout. When
using the sentinels feature, the Redis client will wait at most that number of
seconds (can be fractional) before giving up when reading from a sentinel
server. B<Default>: 1

=head3 C<< write_timeout >>

The C<< write_timeout >> option enables write timeout. The Redis client will wait
at most that number of seconds (can be fractional) before giving up when
reading from the server.

=head3 C<< sentinels_write_timeout >>

The C<< sentinels_write_timeout >> option enables sentinel write timeout. When
using the sentinels feature, the Redis client will wait at most that number of
seconds (can be fractional) before giving up when reading from a sentinel
server. B<Default>: 1

=head3 C<< password >>

If your Redis server requires authentication, you can use the C<< password >>
attribute. After each established connection (at the start or when
reconnecting), the Redis C<< AUTH >> command will be send to the server. If the
password is wrong, an exception will be thrown and reconnect will be disabled.

=head3 C<< on_connect >>

You can also provide a code reference that will be immediately after each
successful connection. The C<< on_connect >> attribute is used to provide the
code reference, and it will be called with the first parameter being the Redis
object.

=head3 C<< no_auto_connect_on_new >>

You can also provide C<< no_auto_connect_on_new >> in which case C<<
new >> won't call C<< $obj->connect >> for you implicitly, you'll have
to do that yourself. This is useful for figuring out how long
connection setup takes so you can configure the C<< cnx_timeout >>
appropriately.

=head3 C<< no_sentinels_list_update >>

You can also provide C<< no_sentinels_list_update >>. By default (that is,
without this option), when successfully contacting a sentinel server, the Redis
client will ask it for the list of sentinels known for the given service, and
merge it with its list of sentinels (in the C<< sentinels >> attribute). You
can disable this behavior by setting C<< no_sentinels_list_update >> to a true
value.

=head3 C<< name >>

You can also set a name for each connection. This can be very useful for
debugging purposes, using the C<< CLIENT LIST >> command. To set a connection
name, use the C<< name >> parameter. You can use both a scalar value or a
CodeRef. If the latter, it will be called after each connection, with the Redis
object, and it should return the connection name to use. If it returns a
undefined value, Redis will not set the connection name.

Please note that there are restrictions on the name you can set, the most
important of which is, no spaces. See the L<CLIENT SETNAME
documentation|http://redis.io/commands/client-setname> for all the juicy
details. This feature is safe to use with all versions of Redis servers. If C<<
CLIENT SETNAME >> support is not available (Redis servers 2.6.9 and above
only), the name parameter is ignored.

=head3 C<< debug >>

The C<< debug >> parameter enables debug information to STDERR, including all
interactions with the server. You can also enable debug with the C<REDIS_DEBUG>
environment variable.

=head1 CONNECTION HANDLING

=head2 connect

  $r->connect;

Connects to the Redis server. This is done by default when the obect is
constructed using C<new()>, unless C<no_auto_connect_on_new> has been set. See
this option in the C<new()> constructor.

=head2 quit

  $r->quit;

Closes the connection to the server. The C<quit> method does not support
pipelined operation.

=head2 ping

  $r->ping || die "no server?";

The C<ping> method does not support pipelined operation.

=head1 PIPELINE MANAGEMENT

=head2 wait_all_responses

Waits until all pending pipelined responses have been received, and invokes the
pipeline callback for each one.  See L</PIPELINING>.

=head2 wait_one_response

Waits until the first pending pipelined response has been received, and invokes
its callback.  See L</PIPELINING>.

=head1 PUBLISH/SUBSCRIBE COMMANDS

When one of L</subscribe> or L</psubscribe> is used, the Redis object will
enter I<PubSub> mode. When in I<PubSub> mode only commands in this section,
plus L</quit>, will be accepted.

If you plan on using PubSub and other Redis functions, you should use two Redis
objects, one dedicated to PubSub and the other for regular commands.

All Pub/Sub commands receive a callback as the last parameter. This callback
receives three arguments:

=over

=item *

The published message.

=item *

The topic over which the message was sent.

=item *

The subscribed topic that matched the topic for the message. With L</subscribe>
these last two are the same, always. But with L</psubscribe>, this parameter
tells you the pattern that matched.

=back

See the L<Pub-Sub notes|http://redis.io/topics/pubsub> for more information
about the messages you will receive on your callbacks after each L</subscribe>,
L</unsubscribe>, L</psubscribe> and L</punsubscribe>.

=head2 publish

  $r->publish($topic, $message);

Publishes the C<< $message >> to the C<< $topic >>.

=head2 subscribe

  $r->subscribe(
      @topics_to_subscribe_to,
      my $savecallback = sub {
        my ($message, $topic, $subscribed_topic) = @_;
        ...
      },
  );

Subscribe one or more topics. Messages published into one of them will be
received by Redis, and the specified callback will be executed.

=head2 unsubscribe

  $r->unsubscribe(@topic_list, $savecallback);

Stops receiving messages via C<$savecallback> for all the topics in
C<@topic_list>. B<WARNING:> it is important that you give the same calleback
that you used for subscribtion. The value of the CodeRef must be the same, as
this is how internally the code identifies it.

=head2 psubscribe

  my @topic_matches = ('prefix1.*', 'prefix2.*');
  $r->psubscribe(@topic_matches, my $savecallback = sub { my ($m, $t, $s) = @_; ... });

Subscribes a pattern of topics. All messages to topics that match the pattern
will be delivered to the callback.

=head2 punsubscribe

  my @topic_matches = ('prefix1.*', 'prefix2.*');
  $r->punsubscribe(@topic_matches, $savecallback);

Stops receiving messages via C<$savecallback> for all the topics pattern
matches in C<@topic_list>. B<WARNING:> it is important that you give the same
calleback that you used for subscribtion. The value of the CodeRef must be the
same, as this is how internally the code identifies it.

=head2 is_subscriber

  if ($r->is_subscriber) { say "We are in Pub/Sub mode!" }

Returns true if we are in I<Pub/Sub> mode.

=head2 wait_for_messages

  my $keep_going = 1; ## Set to false somewhere to leave the loop
  my $timeout = 5;
  $r->wait_for_messages($timeout) while $keep_going;

Blocks, waits for incoming messages and delivers them to the appropriate
callbacks.

Requires a single parameter, the number of seconds to wait for messages. Use 0
to wait for ever. If a positive non-zero value is used, it will return after
that amount of seconds without a single notification.

Please note that the timeout is not a commitment to return control to the
caller at most each C<timeout> seconds, but more a idle timeout, were control
will return to the caller if Redis is idle (as in no messages were received
during the timeout period) for more than C<timeout> seconds.

The L</wait_for_messages> call returns the number of messages processed during
the run.

=head1 IMPORTANT NOTES ON METHODS

=head2 methods that return multiple values

When a method returns more than one value, it checks the context and returns
either a list of values or an ArrayRef.

=head2 transaction-handling methods

B<Warning:> the behaviour of the TRANSACTIONS commands when combined with
pipelining is still under discussion, and you should B<NOT> use them at the
same time just now.

You can L<follow the discussion to see the open issues with
this|https://github.com/PerlRedis/perl-redis/issues/17>.

=head2 exec

  my @individual_replies = $r->exec;

C<exec> has special behaviour when run in a pipeline: the C<$reply> argument to
the pipeline callback is an array ref whose elements are themselves C<[$reply,
$error]> pairs.  This means that you can accurately detect errors yielded by
any command in the transaction, and without any exceptions being thrown.

=head2 keys

  my @keys = $r->keys( '*glob_pattern*' );
  my $keys = $r->keys( '*glob_pattern*' ); # count of matching keys

Note that synchronous C<keys> calls in a scalar context return the number of
matching keys (not an array ref of matching keys as you might expect).  This
does not apply in pipelined mode: assuming the server returns a list of keys,
as expected, it is always passed to the pipeline callback as an array ref.

=head2 hashes

Hashes in Redis cannot be nested as in perl, if you want to store a nested
hash, you need to serialize the hash first. If you want to have a named
hash, you can use Redis-hashes. You will find an example in the tests
of this module t/01-basic.t

=head2 eval

Note that this commands sends the Lua script every time you call it. See
L</evalsha> and L</script_load> for an alternative.

=head2 info

  my $info_hash = $r->info;

The C<info> method is unique in that it decodes the server's response into a
hashref, if possible. This decoding happens in both synchronous and pipelined
modes.

=head1 KEYS

=head2 del

  $r->del(key [key ...])

Delete a key (see L<http://redis.io/commands/del>)

=head2 dump

  $r->dump(key)

Return a serialized version of the value stored at the specified key. (see L<http://redis.io/commands/dump>)

=head2 exists

  $r->exists(key)

Determine if a key exists (see L<http://redis.io/commands/exists>)

=head2 expire

  $r->expire(key, seconds)

Set a key's time to live in seconds (see L<http://redis.io/commands/expire>)

=head2 expireat

  $r->expireat(key, timestamp)

Set the expiration for a key as a UNIX timestamp (see L<http://redis.io/commands/expireat>)

=head2 keys

  $r->keys(pattern)

Find all keys matching the given pattern (see L<http://redis.io/commands/keys>)

=head2 migrate

  $r->migrate(host, port, key, destination-db, timeout, [COPY], [REPLACE])

Atomically transfer a key from a Redis instance to another one. (see L<http://redis.io/commands/migrate>)

=head2 move

  $r->move(key, db)

Move a key to another database (see L<http://redis.io/commands/move>)

=head2 object

  $r->object(subcommand, [arguments [arguments ...]])

Inspect the internals of Redis objects (see L<http://redis.io/commands/object>)

=head2 persist

  $r->persist(key)

Remove the expiration from a key (see L<http://redis.io/commands/persist>)

=head2 pexpire

  $r->pexpire(key, milliseconds)

Set a key's time to live in milliseconds (see L<http://redis.io/commands/pexpire>)

=head2 pexpireat

  $r->pexpireat(key, milliseconds-timestamp)

Set the expiration for a key as a UNIX timestamp specified in milliseconds (see L<http://redis.io/commands/pexpireat>)

=head2 pttl

  $r->pttl(key)

Get the time to live for a key in milliseconds (see L<http://redis.io/commands/pttl>)

=head2 randomkey

  $r->randomkey()

Return a random key from the keyspace (see L<http://redis.io/commands/randomkey>)

=head2 rename

  $r->rename(key, newkey)

Rename a key (see L<http://redis.io/commands/rename>)

=head2 renamenx

  $r->renamenx(key, newkey)

Rename a key, only if the new key does not exist (see L<http://redis.io/commands/renamenx>)

=head2 restore

  $r->restore(key, ttl, serialized-value)

Create a key using the provided serialized value, previously obtained using DUMP. (see L<http://redis.io/commands/restore>)

=head2 scan

  $r->scan(cursor, [MATCH pattern], [COUNT count])

Incrementally iterate the keys space (see L<http://redis.io/commands/scan>)

=head2 sort

  $r->sort(key, [BY pattern], [LIMIT offset count], [GET pattern [GET pattern ...]], [ASC|DESC], [ALPHA], [STORE destination])

Sort the elements in a list, set or sorted set (see L<http://redis.io/commands/sort>)

=head2 ttl

  $r->ttl(key)

Get the time to live for a key (see L<http://redis.io/commands/ttl>)

=head2 type

  $r->type(key)

Determine the type stored at key (see L<http://redis.io/commands/type>)

=head1 STRINGS

=head2 append

  $r->append(key, value)

Append a value to a key (see L<http://redis.io/commands/append>)

=head2 bitcount

  $r->bitcount(key, [start end])

Count set bits in a string (see L<http://redis.io/commands/bitcount>)

=head2 bitop

  $r->bitop(operation, destkey, key [key ...])

Perform bitwise operations between strings (see L<http://redis.io/commands/bitop>)

=head2 bitpos

  $r->bitpos(key, bit, [start], [end])

Find first bit set or clear in a string (see L<http://redis.io/commands/bitpos>)

=head2 blpop

  $r->blpop(key [key ...], timeout)

Remove and get the first element in a list, or block until one is available (see L<http://redis.io/commands/blpop>)

=head2 brpop

  $r->brpop(key [key ...], timeout)

Remove and get the last element in a list, or block until one is available (see L<http://redis.io/commands/brpop>)

=head2 brpoplpush

  $r->brpoplpush(source, destination, timeout)

Pop a value from a list, push it to another list and return it; or block until one is available (see L<http://redis.io/commands/brpoplpush>)

=head2 decr

  $r->decr(key)

Decrement the integer value of a key by one (see L<http://redis.io/commands/decr>)

=head2 decrby

  $r->decrby(key, decrement)

Decrement the integer value of a key by the given number (see L<http://redis.io/commands/decrby>)

=head2 get

  $r->get(key)

Get the value of a key (see L<http://redis.io/commands/get>)

=head2 getbit

  $r->getbit(key, offset)

Returns the bit value at offset in the string value stored at key (see L<http://redis.io/commands/getbit>)

=head2 getrange

  $r->getrange(key, start, end)

Get a substring of the string stored at a key (see L<http://redis.io/commands/getrange>)

=head2 getset

  $r->getset(key, value)

Set the string value of a key and return its old value (see L<http://redis.io/commands/getset>)

=head2 incr

  $r->incr(key)

Increment the integer value of a key by one (see L<http://redis.io/commands/incr>)

=head2 incrby

  $r->incrby(key, increment)

Increment the integer value of a key by the given amount (see L<http://redis.io/commands/incrby>)

=head2 incrbyfloat

  $r->incrbyfloat(key, increment)

Increment the float value of a key by the given amount (see L<http://redis.io/commands/incrbyfloat>)

=head2 mget

  $r->mget(key [key ...])

Get the values of all the given keys (see L<http://redis.io/commands/mget>)

=head2 mset

  $r->mset(key value [key value ...])

Set multiple keys to multiple values (see L<http://redis.io/commands/mset>)

=head2 msetnx

  $r->msetnx(key value [key value ...])

Set multiple keys to multiple values, only if none of the keys exist (see L<http://redis.io/commands/msetnx>)

=head2 psetex

  $r->psetex(key, milliseconds, value)

Set the value and expiration in milliseconds of a key (see L<http://redis.io/commands/psetex>)

=head2 set

  $r->set(key, value, ['EX',  seconds], ['PX', milliseconds], ['NX'|'XX'])

Set the string value of a key (see L<http://redis.io/commands/set>). Example:

  $r->set('key', 'test', 'EX', 60, 'NX')

=head2 setbit

  $r->setbit(key, offset, value)

Sets or clears the bit at offset in the string value stored at key (see L<http://redis.io/commands/setbit>)

=head2 setex

  $r->setex(key, seconds, value)

Set the value and expiration of a key (see L<http://redis.io/commands/setex>)

=head2 setnx

  $r->setnx(key, value)

Set the value of a key, only if the key does not exist (see L<http://redis.io/commands/setnx>)

=head2 setrange

  $r->setrange(key, offset, value)

Overwrite part of a string at key starting at the specified offset (see L<http://redis.io/commands/setrange>)

=head2 strlen

  $r->strlen(key)

Get the length of the value stored in a key (see L<http://redis.io/commands/strlen>)

=head1 HASHES

=head2 hdel

  $r->hdel(key, field [field ...])

Delete one or more hash fields (see L<http://redis.io/commands/hdel>)

=head2 hexists

  $r->hexists(key, field)

Determine if a hash field exists (see L<http://redis.io/commands/hexists>)

=head2 hget

  $r->hget(key, field)

Get the value of a hash field (see L<http://redis.io/commands/hget>)

=head2 hgetall

  $r->hgetall(key)

Get all the fields and values in a hash (see L<http://redis.io/commands/hgetall>)

=head2 hincrby

  $r->hincrby(key, field, increment)

Increment the integer value of a hash field by the given number (see L<http://redis.io/commands/hincrby>)

=head2 hincrbyfloat

  $r->hincrbyfloat(key, field, increment)

Increment the float value of a hash field by the given amount (see L<http://redis.io/commands/hincrbyfloat>)

=head2 hkeys

  $r->hkeys(key)

Get all the fields in a hash (see L<http://redis.io/commands/hkeys>)

=head2 hlen

  $r->hlen(key)

Get the number of fields in a hash (see L<http://redis.io/commands/hlen>)

=head2 hmget

  $r->hmget(key, field [field ...])

Get the values of all the given hash fields (see L<http://redis.io/commands/hmget>)

=head2 hmset

  $r->hmset(key, field value [field value ...])

Set multiple hash fields to multiple values (see L<http://redis.io/commands/hmset>)

=head2 hscan

  $r->hscan(key, cursor, [MATCH pattern], [COUNT count])

Incrementally iterate hash fields and associated values (see L<http://redis.io/commands/hscan>)

=head2 hset

  $r->hset(key, field, value)

Set the string value of a hash field (see L<http://redis.io/commands/hset>)

=head2 hsetnx

  $r->hsetnx(key, field, value)

Set the value of a hash field, only if the field does not exist (see L<http://redis.io/commands/hsetnx>)

=head2 hvals

  $r->hvals(key)

Get all the values in a hash (see L<http://redis.io/commands/hvals>)

=head1 SETS

=head2 sadd

  $r->sadd(key, member [member ...])

Add one or more members to a set (see L<http://redis.io/commands/sadd>)

=head2 scard

  $r->scard(key)

Get the number of members in a set (see L<http://redis.io/commands/scard>)

=head2 sdiff

  $r->sdiff(key [key ...])

Subtract multiple sets (see L<http://redis.io/commands/sdiff>)

=head2 sdiffstore

  $r->sdiffstore(destination, key [key ...])

Subtract multiple sets and store the resulting set in a key (see L<http://redis.io/commands/sdiffstore>)

=head2 sinter

  $r->sinter(key [key ...])

Intersect multiple sets (see L<http://redis.io/commands/sinter>)

=head2 sinterstore

  $r->sinterstore(destination, key [key ...])

Intersect multiple sets and store the resulting set in a key (see L<http://redis.io/commands/sinterstore>)

=head2 sismember

  $r->sismember(key, member)

Determine if a given value is a member of a set (see L<http://redis.io/commands/sismember>)

=head2 smembers

  $r->smembers(key)

Get all the members in a set (see L<http://redis.io/commands/smembers>)

=head2 smove

  $r->smove(source, destination, member)

Move a member from one set to another (see L<http://redis.io/commands/smove>)

=head2 spop

  $r->spop(key)

Remove and return a random member from a set (see L<http://redis.io/commands/spop>)

=head2 srandmember

  $r->srandmember(key, [count])

Get one or multiple random members from a set (see L<http://redis.io/commands/srandmember>)

=head2 srem

  $r->srem(key, member [member ...])

Remove one or more members from a set (see L<http://redis.io/commands/srem>)

=head2 sscan

  $r->sscan(key, cursor, [MATCH pattern], [COUNT count])

Incrementally iterate Set elements (see L<http://redis.io/commands/sscan>)

=head2 sunion

  $r->sunion(key [key ...])

Add multiple sets (see L<http://redis.io/commands/sunion>)

=head2 sunionstore

  $r->sunionstore(destination, key [key ...])

Add multiple sets and store the resulting set in a key (see L<http://redis.io/commands/sunionstore>)

=head1 SORTED SETS

=head2 zadd

  $r->zadd(key, score member [score member ...])

Add one or more members to a sorted set, or update its score if it already exists (see L<http://redis.io/commands/zadd>)

=head2 zcard

  $r->zcard(key)

Get the number of members in a sorted set (see L<http://redis.io/commands/zcard>)

=head2 zcount

  $r->zcount(key, min, max)

Count the members in a sorted set with scores within the given values (see L<http://redis.io/commands/zcount>)

=head2 zincrby

  $r->zincrby(key, increment, member)

Increment the score of a member in a sorted set (see L<http://redis.io/commands/zincrby>)

=head2 zinterstore

  $r->zinterstore(destination, numkeys, key [key ...], [WEIGHTS weight [weight ...]], [AGGREGATE SUM|MIN|MAX])

Intersect multiple sorted sets and store the resulting sorted set in a new key (see L<http://redis.io/commands/zinterstore>)

=head2 zlexcount

  $r->zlexcount(key, min, max)

Count the number of members in a sorted set between a given lexicographical range (see L<http://redis.io/commands/zlexcount>)

=head2 zrange

  $r->zrange(key, start, stop, [WITHSCORES])

Return a range of members in a sorted set, by index (see L<http://redis.io/commands/zrange>)

=head2 zrangebylex

  $r->zrangebylex(key, min, max, [LIMIT offset count])

Return a range of members in a sorted set, by lexicographical range (see L<http://redis.io/commands/zrangebylex>)

=head2 zrangebyscore

  $r->zrangebyscore(key, min, max, [WITHSCORES], [LIMIT offset count])

Return a range of members in a sorted set, by score (see L<http://redis.io/commands/zrangebyscore>)

=head2 zrank

  $r->zrank(key, member)

Determine the index of a member in a sorted set (see L<http://redis.io/commands/zrank>)

=head2 zrem

  $r->zrem(key, member [member ...])

Remove one or more members from a sorted set (see L<http://redis.io/commands/zrem>)

=head2 zremrangebylex

  $r->zremrangebylex(key, min, max)

Remove all members in a sorted set between the given lexicographical range (see L<http://redis.io/commands/zremrangebylex>)

=head2 zremrangebyrank

  $r->zremrangebyrank(key, start, stop)

Remove all members in a sorted set within the given indexes (see L<http://redis.io/commands/zremrangebyrank>)

=head2 zremrangebyscore

  $r->zremrangebyscore(key, min, max)

Remove all members in a sorted set within the given scores (see L<http://redis.io/commands/zremrangebyscore>)

=head2 zrevrange

  $r->zrevrange(key, start, stop, [WITHSCORES])

Return a range of members in a sorted set, by index, with scores ordered from high to low (see L<http://redis.io/commands/zrevrange>)

=head2 zrevrangebylex

  $r->zrevrangebylex(key, max, min, [LIMIT offset count])

Return a range of members in a sorted set, by lexicographical range, ordered from higher to lower strings. (see L<http://redis.io/commands/zrevrangebylex>)

=head2 zrevrangebyscore

  $r->zrevrangebyscore(key, max, min, [WITHSCORES], [LIMIT offset count])

Return a range of members in a sorted set, by score, with scores ordered from high to low (see L<http://redis.io/commands/zrevrangebyscore>)

=head2 zrevrank

  $r->zrevrank(key, member)

Determine the index of a member in a sorted set, with scores ordered from high to low (see L<http://redis.io/commands/zrevrank>)

=head2 zscan

  $r->zscan(key, cursor, [MATCH pattern], [COUNT count])

Incrementally iterate sorted sets elements and associated scores (see L<http://redis.io/commands/zscan>)

=head2 zscore

  $r->zscore(key, member)

Get the score associated with the given member in a sorted set (see L<http://redis.io/commands/zscore>)

=head2 zunionstore

  $r->zunionstore(destination, numkeys, key [key ...], [WEIGHTS weight [weight ...]], [AGGREGATE SUM|MIN|MAX])

Add multiple sorted sets and store the resulting sorted set in a new key (see L<http://redis.io/commands/zunionstore>)

=head1 HYPERLOGLOG

=head2 pfadd

  $r->pfadd(key, element [element ...])

Adds the specified elements to the specified HyperLogLog. (see L<http://redis.io/commands/pfadd>)

=head2 pfcount

  $r->pfcount(key [key ...])

Return the approximated cardinality of the set(s) observed by the HyperLogLog at key(s). (see L<http://redis.io/commands/pfcount>)

=head2 pfmerge

  $r->pfmerge(destkey, sourcekey [sourcekey ...])

Merge N different HyperLogLogs into a single one. (see L<http://redis.io/commands/pfmerge>)

=head1 PUB/SUB

=head2 pubsub

  $r->pubsub(subcommand, [argument [argument ...]])

Inspect the state of the Pub/Sub subsystem (see L<http://redis.io/commands/pubsub>)

=head1 TRANSACTIONS

=head2 discard

  $r->discard()

Discard all commands issued after MULTI (see L<http://redis.io/commands/discard>)

=head2 exec

  $r->exec()

Execute all commands issued after MULTI (see L<http://redis.io/commands/exec>)

=head2 multi

  $r->multi()

Mark the start of a transaction block (see L<http://redis.io/commands/multi>)

=head2 unwatch

  $r->unwatch()

Forget about all watched keys (see L<http://redis.io/commands/unwatch>)

=head2 watch

  $r->watch(key [key ...])

Watch the given keys to determine execution of the MULTI/EXEC block (see L<http://redis.io/commands/watch>)

=head1 SCRIPTING

=head2 eval

  $r->eval(script, numkeys, key [key ...], arg [arg ...])

Execute a Lua script server side (see L<http://redis.io/commands/eval>)

=head2 evalsha

  $r->evalsha(sha1, numkeys, key [key ...], arg [arg ...])

Execute a Lua script server side (see L<http://redis.io/commands/evalsha>)

=head2 script_exists

  $r->script_exists(script [script ...])

Check existence of scripts in the script cache. (see L<http://redis.io/commands/script-exists>)

=head2 script_flush

  $r->script_flush()

Remove all the scripts from the script cache. (see L<http://redis.io/commands/script-flush>)

=head2 script_kill

  $r->script_kill()

Kill the script currently in execution. (see L<http://redis.io/commands/script-kill>)

=head2 script_load

  $r->script_load(script)

Load the specified Lua script into the script cache. (see L<http://redis.io/commands/script-load>)

=head1 CONNECTION

=head2 auth

  $r->auth(password)

Authenticate to the server (see L<http://redis.io/commands/auth>)

=head2 echo

  $r->echo(message)

Echo the given string (see L<http://redis.io/commands/echo>)

=head2 ping

  $r->ping()

Ping the server (see L<http://redis.io/commands/ping>)

=head2 quit

  $r->quit()

Close the connection (see L<http://redis.io/commands/quit>)

=head2 select

  $r->select(index)

Change the selected database for the current connection (see L<http://redis.io/commands/select>)

=head1 SERVER

=head2 bgrewriteaof

  $r->bgrewriteaof()

Asynchronously rewrite the append-only file (see L<http://redis.io/commands/bgrewriteaof>)

=head2 bgsave

  $r->bgsave()

Asynchronously save the dataset to disk (see L<http://redis.io/commands/bgsave>)

=head2 client_getname

  $r->client_getname()

Get the current connection name (see L<http://redis.io/commands/client-getname>)

=head2 client_kill

  $r->client_kill([ip:port], [ID client-id], [TYPE normal|slave|pubsub], [ADDR ip:port], [SKIPME yes/no])

Kill the connection of a client (see L<http://redis.io/commands/client-kill>)

=head2 client_list

  $r->client_list()

Get the list of client connections (see L<http://redis.io/commands/client-list>)

=head2 client_pause

  $r->client_pause(timeout)

Stop processing commands from clients for some time (see L<http://redis.io/commands/client-pause>)

=head2 client_setname

  $r->client_setname(connection-name)

Set the current connection name (see L<http://redis.io/commands/client-setname>)

=head2 cluster_slots

  $r->cluster_slots()

Get array of Cluster slot to node mappings (see L<http://redis.io/commands/cluster-slots>)

=head2 command

  $r->command()

Get array of Redis command details (see L<http://redis.io/commands/command>)

=head2 command_count

  $r->command_count()

Get total number of Redis commands (see L<http://redis.io/commands/command-count>)

=head2 command_getkeys

  $r->command_getkeys()

Extract keys given a full Redis command (see L<http://redis.io/commands/command-getkeys>)

=head2 command_info

  $r->command_info(command-name [command-name ...])

Get array of specific Redis command details (see L<http://redis.io/commands/command-info>)

=head2 config_get

  $r->config_get(parameter)

Get the value of a configuration parameter (see L<http://redis.io/commands/config-get>)

=head2 config_resetstat

  $r->config_resetstat()

Reset the stats returned by INFO (see L<http://redis.io/commands/config-resetstat>)

=head2 config_rewrite

  $r->config_rewrite()

Rewrite the configuration file with the in memory configuration (see L<http://redis.io/commands/config-rewrite>)

=head2 config_set

  $r->config_set(parameter, value)

Set a configuration parameter to the given value (see L<http://redis.io/commands/config-set>)

=head2 dbsize

  $r->dbsize()

Return the number of keys in the selected database (see L<http://redis.io/commands/dbsize>)

=head2 debug_object

  $r->debug_object(key)

Get debugging information about a key (see L<http://redis.io/commands/debug-object>)

=head2 debug_segfault

  $r->debug_segfault()

Make the server crash (see L<http://redis.io/commands/debug-segfault>)

=head2 flushall

  $r->flushall()

Remove all keys from all databases (see L<http://redis.io/commands/flushall>)

=head2 flushdb

  $r->flushdb()

Remove all keys from the current database (see L<http://redis.io/commands/flushdb>)

=head2 info

  $r->info([section])

Get information and statistics about the server (see L<http://redis.io/commands/info>)

=head2 lastsave

  $r->lastsave()

Get the UNIX time stamp of the last successful save to disk (see L<http://redis.io/commands/lastsave>)

=head2 lindex

  $r->lindex(key, index)

Get an element from a list by its index (see L<http://redis.io/commands/lindex>)

=head2 linsert

  $r->linsert(key, BEFORE|AFTER, pivot, value)

Insert an element before or after another element in a list (see L<http://redis.io/commands/linsert>)

=head2 llen

  $r->llen(key)

Get the length of a list (see L<http://redis.io/commands/llen>)

=head2 lpop

  $r->lpop(key)

Remove and get the first element in a list (see L<http://redis.io/commands/lpop>)

=head2 lpush

  $r->lpush(key, value [value ...])

Prepend one or multiple values to a list (see L<http://redis.io/commands/lpush>)

=head2 lpushx

  $r->lpushx(key, value)

Prepend a value to a list, only if the list exists (see L<http://redis.io/commands/lpushx>)

=head2 lrange

  $r->lrange(key, start, stop)

Get a range of elements from a list (see L<http://redis.io/commands/lrange>)

=head2 lrem

  $r->lrem(key, count, value)

Remove elements from a list (see L<http://redis.io/commands/lrem>)

=head2 lset

  $r->lset(key, index, value)

Set the value of an element in a list by its index (see L<http://redis.io/commands/lset>)

=head2 ltrim

  $r->ltrim(key, start, stop)

Trim a list to the specified range (see L<http://redis.io/commands/ltrim>)

=head2 monitor

  $r->monitor()

Listen for all requests received by the server in real time (see L<http://redis.io/commands/monitor>)

=head2 role

  $r->role()

Return the role of the instance in the context of replication (see L<http://redis.io/commands/role>)

=head2 rpop

  $r->rpop(key)

Remove and get the last element in a list (see L<http://redis.io/commands/rpop>)

=head2 rpoplpush

  $r->rpoplpush(source, destination)

Remove the last element in a list, append it to another list and return it (see L<http://redis.io/commands/rpoplpush>)

=head2 rpush

  $r->rpush(key, value [value ...])

Append one or multiple values to a list (see L<http://redis.io/commands/rpush>)

=head2 rpushx

  $r->rpushx(key, value)

Append a value to a list, only if the list exists (see L<http://redis.io/commands/rpushx>)

=head2 save

  $r->save()

Synchronously save the dataset to disk (see L<http://redis.io/commands/save>)

=head2 shutdown

  $r->shutdown([NOSAVE], [SAVE])

Synchronously save the dataset to disk and then shut down the server (see L<http://redis.io/commands/shutdown>)

=head2 slaveof

  $r->slaveof(host, port)

Make the server a slave of another instance, or promote it as master (see L<http://redis.io/commands/slaveof>)

=head2 slowlog

  $r->slowlog(subcommand, [argument])

Manages the Redis slow queries log (see L<http://redis.io/commands/slowlog>)

=head2 sync

  $r->sync()

Internal command used for replication (see L<http://redis.io/commands/sync>)

=head2 time

  $r->time()

Return the current server time (see L<http://redis.io/commands/time>)

=head1 ACKNOWLEDGEMENTS

The following persons contributed to this project (random order):

=over

=item *

Aaron Crane (pipelining and AUTOLOAD caching support)

=item *

Dirk Vleugels

=item *

Flavio Poletti

=item *

Jeremy Zawodny

=item *

sunnavy at bestpractical.com

=item *

Thiago Berlitz Rondon

=item *

Ulrich Habel

=item *

Ivan Kruglov

=item *

Steffen Mueller <smueller@cpan.org>

=back

=head1 AUTHORS

=over 4

=item *

Pedro Melo <melo@cpan.org>

=item *

Damien Krotkine <dams@cpan.org>

=back

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2015 by Pedro Melo, Damien Krotkine.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)

=cut