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 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080
|
# PhpRedis
[](https://travis-ci.org/phpredis/phpredis)
[](https://scan.coverity.com/projects/phpredis-phpredis)
The phpredis extension provides an API for communicating with the [Redis](http://redis.io/) key-value store. It is released under the [PHP License, version 3.01](http://www.php.net/license/3_01.txt).
This code has been developed and maintained by Owlient from November 2009 to March 2011.
You can send comments, patches, questions [here on github](https://github.com/phpredis/phpredis/issues), to n.favrefelix@gmail.com ([@yowgi](https://twitter.com/yowgi)), to michael.grunder@gmail.com ([@grumi78](https://twitter.com/grumi78)) or to p.yatsukhnenko@gmail.com ([@yatsukhnenko](https://twitter.com/yatsukhnenko)).
## Donating to the project
If you've found phpredis useful and would like to buy the maintainers a coffee (or a Tesla, we're not picky), feel free to do so.
[](https://www.paypal.me/michaelgrunder/5)
[](https://en.cryptobadges.io/donate/1FXkYHBo5uoaztxFbajiPfbnkgKCbF3ykG)
[](https://en.cryptobadges.io/donate/0x43D54E32357B96f68dFF0a6B46976d014Bd603E1)
# Table of contents
-----
1. [Installing/Configuring](#installingconfiguring)
* [Installation](#installation)
* [PHP Session handler](#php-session-handler)
* [Distributed Redis Array](#distributed-redis-array)
* [Redis Cluster support](#redis-cluster-support)
* [Running the unit tests](#running-the-unit-tests)
1. [Classes and methods](#classes-and-methods)
* [Usage](#usage)
* [Connection](#connection)
* [Server](#server)
* [Keys and strings](#keys-and-strings)
* [Hashes](#hashes)
* [Lists](#lists)
* [Sets](#sets)
* [Sorted sets](#sorted-sets)
* [Geocoding](#geocoding)
* [Streams](#streams)
* [Pub/sub](#pubsub)
* [Transactions](#transactions)
* [Scripting](#scripting)
* [Introspection](#introspection)
-----
# Installing/Configuring
-----
## Installation
For everything you should need to install PhpRedis on your system,
see the [INSTALL.markdown](./INSTALL.markdown) page.
## PHP Session handler
phpredis can be used to store PHP sessions. To do this, configure `session.save_handler` and `session.save_path` in your php.ini to tell phpredis where to store the sessions:
~~~
session.save_handler = redis
session.save_path = "tcp://host1:6379?weight=1, tcp://host2:6379?weight=2&timeout=2.5, tcp://host3:6379?weight=2&read_timeout=2.5"
~~~
`session.save_path` can have a simple `host:port` format too, but you need to provide the `tcp://` scheme if you want to use the parameters. The following parameters are available:
* weight (integer): the weight of a host is used in comparison with the others in order to customize the session distribution on several hosts. If host A has twice the weight of host B, it will get twice the amount of sessions. In the example, *host1* stores 20% of all the sessions (1/(1+2+2)) while *host2* and *host3* each store 40% (2/(1+2+2)). The target host is determined once and for all at the start of the session, and doesn't change. The default weight is 1.
* timeout (float): the connection timeout to a redis host, expressed in seconds. If the host is unreachable in that amount of time, the session storage will be unavailable for the client. The default timeout is very high (86400 seconds).
* persistent (integer, should be 1 or 0): defines if a persistent connection should be used. **(experimental setting)**
* prefix (string, defaults to "PHPREDIS_SESSION:"): used as a prefix to the Redis key in which the session is stored. The key is composed of the prefix followed by the session ID.
* auth (string, empty by default): used to authenticate with the server prior to sending commands.
* database (integer): selects a different database.
Sessions have a lifetime expressed in seconds and stored in the INI variable "session.gc_maxlifetime". You can change it with [`ini_set()`](http://php.net/ini_set).
The session handler requires a version of Redis supporting `EX` and `NX` options of `SET` command (at least 2.6.12).
phpredis can also connect to a unix domain socket: `session.save_path = "unix:///var/run/redis/redis.sock?persistent=1&weight=1&database=0`.
### Session locking
**Support**: Locking feature is currently only supported for Redis setup with single master instance (e.g. classic master/slave Sentinel environment).
So locking may not work properly in RedisArray or RedisCluster environments.
Following INI variables can be used to configure session locking:
~~~
; Should the locking be enabled? Defaults to: 0.
redis.session.locking_enabled = 1
; How long should the lock live (in seconds)? Defaults to: value of max_execution_time.
redis.session.lock_expire = 60
; How long to wait between attempts to acquire lock, in microseconds (µs)?. Defaults to: 2000
redis.session.lock_wait_time = 50000
; Maximum number of times to retry (-1 means infinite). Defaults to: 10
redis.session.lock_retries = 10
~~~
## Distributed Redis Array
See [dedicated page](./arrays.markdown#readme).
## Redis Cluster support
See [dedicated page](./cluster.markdown#readme).
## Running the unit tests
phpredis uses a small custom unit test suite for testing functionality of the various classes. To run tests, simply do the following:
~~~
# Run tests for Redis class (note this is the default)
php tests/TestRedis.php --class Redis
# Run tests for RedisArray class
tests/mkring.sh start
php tests/TestRedis.php --class RedisArray
tests/mkring.sh stop
# Run tests for the RedisCluster class
tests/make-cluster.sh start
php tests/TestRedis.php --class RedisCluster
tests/make-cluster.sh stop
~~~
Note that it is possible to run only tests which match a substring of the test itself by passing the additional argument '--test <str>' when invoking.
~~~
# Just run the 'echo' test
php tests/TestRedis.php --class Redis --test echo
~~~
# Classes and methods
-----
## Usage
1. [Class Redis](#class-redis)
1. [Class RedisException](#class-redisexception)
1. [Predefined constants](#predefined-constants)
### Class Redis
-----
_**Description**_: Creates a Redis client
##### *Example*
~~~php
$redis = new Redis();
~~~
### Class RedisException
-----
phpredis throws a [RedisException](#class-redisexception) object if it can't reach the Redis server. That can happen in case of connectivity issues,
if the Redis service is down, or if the redis host is overloaded. In any other problematic case that does not involve an
unreachable server (such as a key not existing, an invalid command, etc), phpredis will return `FALSE`.
### Predefined constants
-----
_**Description**_: Available Redis Constants
Redis data types, as returned by [type](#type)
~~~
Redis::REDIS_STRING - String
Redis::REDIS_SET - Set
Redis::REDIS_LIST - List
Redis::REDIS_ZSET - Sorted set
Redis::REDIS_HASH - Hash
Redis::REDIS_NOT_FOUND - Not found / other
~~~
@TODO: OPT_SERIALIZER, AFTER, BEFORE,...
## Connection
1. [connect, open](#connect-open) - Connect to a server
1. [pconnect, popen](#pconnect-popen) - Connect to a server (persistent)
1. [auth](#auth) - Authenticate to the server
1. [select](#select) - Change the selected database for the current connection
1. [swapdb](#swapdb) - Swaps two Redis databases
1. [close](#close) - Close the connection
1. [setOption](#setoption) - Set client option
1. [getOption](#getoption) - Get client option
1. [ping](#ping) - Ping the server
1. [echo](#echo) - Echo the given string
### connect, open
-----
_**Description**_: Connects to a Redis instance.
##### *Parameters*
*host*: string. can be a host, or the path to a unix domain socket
*port*: int, optional
*timeout*: float, value in seconds (optional, default is 0 meaning unlimited)
*reserved*: should be NULL if retry_interval is specified
*retry_interval*: int, value in milliseconds (optional)
*read_timeout*: float, value in seconds (optional, default is 0 meaning unlimited)
##### *Return value*
*BOOL*: `TRUE` on success, `FALSE` on error.
##### *Example*
~~~php
$redis->connect('127.0.0.1', 6379);
$redis->connect('127.0.0.1'); // port 6379 by default
$redis->connect('127.0.0.1', 6379, 2.5); // 2.5 sec timeout.
$redis->connect('/tmp/redis.sock'); // unix domain socket.
$redis->connect('127.0.0.1', 6379, 1, NULL, 100); // 1 sec timeout, 100ms delay between reconnection attempts.
~~~
### pconnect, popen
-----
_**Description**_: Connects to a Redis instance or reuse a connection already established with `pconnect`/`popen`.
The connection will not be closed on end of request until the php process ends.
So be prepared for too many open FD's errors (specially on redis server side) when using persistent
connections on many servers connecting to one redis server.
Also more than one persistent connection can be made identified by either host + port + timeout
or host + persistent_id or unix socket + timeout.
Starting from version 4.2.1, it became possible to use connection pooling by setting INI variable `redis.pconnect.pooling_enabled` to 1.
This feature is not available in threaded versions. `pconnect` and `popen` then working like their non
persistent equivalents.
##### *Parameters*
*host*: string. can be a host, or the path to a unix domain socket
*port*: int, optional
*timeout*: float, value in seconds (optional, default is 0 meaning unlimited)
*persistent_id*: string. identity for the requested persistent connection
*retry_interval*: int, value in milliseconds (optional)
*read_timeout*: float, value in seconds (optional, default is 0 meaning unlimited)
##### *Return value*
*BOOL*: `TRUE` on success, `FALSE` on error.
##### *Example*
~~~php
$redis->pconnect('127.0.0.1', 6379);
$redis->pconnect('127.0.0.1'); // port 6379 by default - same connection like before.
$redis->pconnect('127.0.0.1', 6379, 2.5); // 2.5 sec timeout and would be another connection than the two before.
$redis->pconnect('127.0.0.1', 6379, 2.5, 'x'); // x is sent as persistent_id and would be another connection than the three before.
$redis->pconnect('/tmp/redis.sock'); // unix domain socket - would be another connection than the four before.
~~~
### auth
-----
_**Description**_: Authenticate the connection using a password.
*Warning*: The password is sent in plain-text over the network.
##### *Parameters*
*STRING*: password
##### *Return value*
*BOOL*: `TRUE` if the connection is authenticated, `FALSE` otherwise.
##### *Example*
~~~php
$redis->auth('foobared');
~~~
### select
-----
_**Description**_: Change the selected database for the current connection.
##### *Parameters*
*INTEGER*: dbindex, the database number to switch to.
##### *Return value*
`TRUE` in case of success, `FALSE` in case of failure.
##### *Example*
See method for example: [move](#move)
### swapdb
-----
_**Description**_: Swap one Redis database with another atomically
##### *Parameters*
*INTEGER*: db1
*INTEGER*: db2
##### *Return value*
`TRUE` on success and `FALSE` on failure.
*Note*: Requires Redis >= 4.0.0
##### *Example*
~~~php
$redis->swapdb(0, 1); /* Swaps DB 0 with DB 1 atomically */
~~~
### close
-----
_**Description**_: Disconnects from the Redis instance.
*Note*: Closing a persistent connection requires PhpRedis >= 4.2.0.
##### *Parameters*
None.
##### *Return value*
*BOOL*: `TRUE` on success, `FALSE` on failure.
### setOption
-----
_**Description**_: Set client option.
##### *Parameters*
*parameter name*
*parameter value*
##### *Return value*
*BOOL*: `TRUE` on success, `FALSE` on error.
##### *Example*
~~~php
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE); // don't serialize data
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); // use built-in serialize/unserialize
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); // use igBinary serialize/unserialize
$redis->setOption(Redis::OPT_PREFIX, 'myAppName:'); // use custom prefix on all keys
/* Options for the SCAN family of commands, indicating whether to abstract
empty results from the user. If set to SCAN_NORETRY (the default), phpredis
will just issue one SCAN command at a time, sometimes returning an empty
array of results. If set to SCAN_RETRY, phpredis will retry the scan command
until keys come back OR Redis returns an iterator of zero
*/
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_NORETRY);
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
~~~
### getOption
-----
_**Description**_: Get client option.
##### *Parameters*
*parameter name*
##### *Return value*
Parameter value.
##### *Example*
~~~php
$redis->getOption(Redis::OPT_SERIALIZER); // return Redis::SERIALIZER_NONE, Redis::SERIALIZER_PHP, or Redis::SERIALIZER_IGBINARY.
~~~
### ping
-----
_**Description**_: Check the current connection status
##### *Parameters*
(none)
##### *Return value*
*STRING*: `+PONG` on success. Throws a [RedisException](#class-redisexception) object on connectivity error, as described above.
### echo
-----
_**Description**_: Sends a string to Redis, which replies with the same string
##### *Parameters*
*STRING*: The message to send.
##### *Return value*
*STRING*: the same message.
## Server
1. [bgRewriteAOF](#bgrewriteaof) - Asynchronously rewrite the append-only file
1. [bgSave](#bgsave) - Asynchronously save the dataset to disk (in background)
1. [config](#config) - Get or Set the Redis server configuration parameters
1. [dbSize](#dbsize) - Return the number of keys in selected database
1. [flushAll](#flushall) - Remove all keys from all databases
1. [flushDb](#flushdb) - Remove all keys from the current database
1. [info](#info) - Get information and statistics about the server
1. [lastSave](#lastsave) - Get the timestamp of the last disk save
1. [resetStat](#resetstat) - Reset the stats returned by [info](#info) method.
1. [save](#save) - Synchronously save the dataset to disk (wait to complete)
1. [slaveOf](#slaveof) - Make the server a slave of another instance, or promote it to master
1. [time](#time) - Return the current server time
1. [slowLog](#slowlog) - Access the Redis slowLog entries
### bgRewriteAOF
-----
_**Description**_: Start the background rewrite of AOF (Append-Only File)
##### *Parameters*
None.
##### *Return value*
*BOOL*: `TRUE` in case of success, `FALSE` in case of failure.
##### *Example*
~~~php
$redis->bgRewriteAOF();
~~~
### bgSave
-----
_**Description**_: Asynchronously save the dataset to disk (in background)
##### *Parameters*
None.
##### *Return value*
*BOOL*: `TRUE` in case of success, `FALSE` in case of failure. If a save is already running, this command will fail and return `FALSE`.
##### *Example*
~~~php
$redis->bgSave();
~~~
### config
-----
_**Description**_: Get or Set the Redis server configuration parameters.
##### *Parameters*
*operation* (string) either `GET` or `SET`
*key* string for `SET`, glob-pattern for `GET`. See http://redis.io/commands/config-get for examples.
*value* optional string (only for `SET`)
##### *Return value*
*Associative array* for `GET`, key -> value
*bool* for `SET`
##### *Examples*
~~~php
$redis->config("GET", "*max-*-entries*");
$redis->config("SET", "dir", "/var/run/redis/dumps/");
~~~
### dbSize
-----
_**Description**_: Return the number of keys in selected database.
##### *Parameters*
None.
##### *Return value*
*INTEGER*: DB size, in number of keys.
##### *Example*
~~~php
$count = $redis->dbSize();
echo "Redis has $count keys\n";
~~~
### flushAll
-----
_**Description**_: Remove all keys from all databases.
##### *Parameters*
*async* (bool) requires server version 4.0.0 or greater
##### *Return value*
*BOOL*: Always `TRUE`.
##### *Example*
~~~php
$redis->flushAll();
~~~
### flushDb
-----
_**Description**_: Remove all keys from the current database.
##### *Parameters*
*async* (bool) requires server version 4.0.0 or greater
##### *Return value*
*BOOL*: Always `TRUE`.
##### *Example*
~~~php
$redis->flushDb();
~~~
### info
-----
_**Description**_: Get information and statistics about the server
Returns an associative array that provides information about the server. Passing no arguments to
INFO will call the standard REDIS INFO command, which returns information such as the following:
* redis_version
* arch_bits
* uptime_in_seconds
* uptime_in_days
* connected_clients
* connected_slaves
* used_memory
* changes_since_last_save
* bgsave_in_progress
* last_save_time
* total_connections_received
* total_commands_processed
* role
You can pass a variety of options to INFO ([per the Redis documentation](http://redis.io/commands/info)),
which will modify what is returned.
##### *Parameters*
*option*: The option to provide redis (e.g. "COMMANDSTATS", "CPU")
##### *Example*
~~~php
$redis->info(); /* standard redis INFO command */
$redis->info("COMMANDSTATS"); /* Information on the commands that have been run (>=2.6 only)
$redis->info("CPU"); /* just CPU information from Redis INFO */
~~~
### lastSave
-----
_**Description**_: Returns the timestamp of the last disk save.
##### *Parameters*
None.
##### *Return value*
*INT*: timestamp.
##### *Example*
~~~php
$redis->lastSave();
~~~
### resetStat
-----
_**Description**_: Reset the stats returned by [info](#info) method.
These are the counters that are reset:
* Keyspace hits
* Keyspace misses
* Number of commands processed
* Number of connections received
* Number of expired keys
##### *Parameters*
None.
##### *Return value*
*BOOL*: `TRUE` in case of success, `FALSE` in case of failure.
##### *Example*
~~~php
$redis->resetStat();
~~~
### save
-----
_**Description**_: Synchronously save the dataset to disk (wait to complete)
##### *Parameters*
None.
##### *Return value*
*BOOL*: `TRUE` in case of success, `FALSE` in case of failure. If a save is already running, this command will fail and return `FALSE`.
##### *Example*
~~~php
$redis->save();
~~~
### slaveOf
-----
_**Description**_: Changes the slave status
##### *Parameters*
Either host (string) and port (int), or no parameter to stop being a slave.
##### *Return value*
*BOOL*: `TRUE` in case of success, `FALSE` in case of failure.
##### *Example*
~~~php
$redis->slaveOf('10.0.1.7', 6379);
/* ... */
$redis->slaveOf();
~~~
### time
-----
_**Description**_: Return the current server time.
##### *Parameters*
(none)
##### *Return value*
If successful, the time will come back as an associative array with element zero being
the unix timestamp, and element one being microseconds.
##### *Examples*
~~~php
$redis->time();
~~~
### slowLog
-----
_**Description**_: Access the Redis slowLog
##### *Parameters*
*Operation* (string): This can be either `GET`, `LEN`, or `RESET`
*Length* (integer), optional: If executing a `SLOWLOG GET` command, you can pass an optional length.
#####
##### *Return value*
The return value of SLOWLOG will depend on which operation was performed.
SLOWLOG GET: Array of slowLog entries, as provided by Redis
SLOGLOG LEN: Integer, the length of the slowLog
SLOWLOG RESET: Boolean, depending on success
#####
##### *Examples*
~~~php
// Get ten slowLog entries
$redis->slowLog('get', 10);
// Get the default number of slowLog entries
$redis->slowLog('get');
// Reset our slowLog
$redis->slowLog('reset');
// Retrieve slowLog length
$redis->slowLog('len');
~~~
## Keys and Strings
### Strings
-----
* [append](#append) - Append a value to a key
* [bitCount](#bitcount) - Count set bits in a string
* [bitOp](#bitop) - Perform bitwise operations between strings
* [decr, decrBy](#decr-decrby) - Decrement the value of a key
* [get](#get) - Get the value of a key
* [getBit](#getbit) - Returns the bit value at offset in the string value stored at key
* [getRange](#getrange) - Get a substring of the string stored at a key
* [getSet](#getset) - Set the string value of a key and return its old value
* [incr, incrBy](#incr-incrby) - Increment the value of a key
* [incrByFloat](#incrbyfloat) - Increment the float value of a key by the given amount
* [mGet, getMultiple](#mget-getmultiple) - Get the values of all the given keys
* [mSet, mSetNX](#mset-msetnx) - Set multiple keys to multiple values
* [set](#set) - Set the string value of a key
* [setBit](#setbit) - Sets or clears the bit at offset in the string value stored at key
* [setEx, pSetEx](#setex-psetex) - Set the value and expiration of a key
* [setNx](#setnx) - Set the value of a key, only if the key does not exist
* [setRange](#setrange) - Overwrite part of a string at key starting at the specified offset
* [strLen](#strlen) - Get the length of the value stored in a key
### Keys
-----
* [del, delete, unlink](#del-delete-unlink) - Delete a key
* [dump](#dump) - Return a serialized version of the value stored at the specified key.
* [exists](#exists) - Determine if a key exists
* [expire, setTimeout, pexpire](#expire-settimeout-pexpire) - Set a key's time to live in seconds
* [expireAt, pexpireAt](#expireat-pexpireat) - Set the expiration for a key as a UNIX timestamp
* [keys, getKeys](#keys-getkeys) - Find all keys matching the given pattern
* [scan](#scan) - Scan for keys in the keyspace (Redis >= 2.8.0)
* [migrate](#migrate) - Atomically transfer a key from a Redis instance to another one
* [move](#move) - Move a key to another database
* [object](#object) - Inspect the internals of Redis objects
* [persist](#persist) - Remove the expiration from a key
* [randomKey](#randomkey) - Return a random key from the keyspace
* [rename, renameKey](#rename-renamekey) - Rename a key
* [renameNx](#renamenx) - Rename a key, only if the new key does not exist
* [type](#type) - Determine the type stored at key
* [sort](#sort) - Sort the elements in a list, set or sorted set
* [ttl, pttl](#ttl-pttl) - Get the time to live for a key
* [restore](#restore) - Create a key using the provided serialized value, previously obtained with [dump](#dump).
-----
### get
-----
_**Description**_: Get the value related to the specified key
##### *Parameters*
*key*
##### *Return value*
*String* or *Bool*: If key didn't exist, `FALSE` is returned. Otherwise, the value related to this key is returned.
##### *Examples*
~~~php
$redis->get('key');
~~~
### set
-----
_**Description**_: Set the string value in argument as value of the key. If you're using Redis >= 2.6.12, you can pass extended options as explained below
##### *Parameters*
*Key*
*Value*
*Timeout or Options Array* (optional). If you pass an integer, phpredis will redirect to SETEX, and will try to use Redis >= 2.6.12 extended options if you pass an array with valid values
##### *Return value*
*Bool* `TRUE` if the command is successful.
##### *Examples*
~~~php
// Simple key -> value set
$redis->set('key', 'value');
// Will redirect, and actually make an SETEX call
$redis->set('key','value', 10);
// Will set the key, if it doesn't exist, with a ttl of 10 seconds
$redis->set('key', 'value', Array('nx', 'ex'=>10));
// Will set a key, if it does exist, with a ttl of 1000 miliseconds
$redis->set('key', 'value', Array('xx', 'px'=>1000));
~~~
### setEx, pSetEx
-----
_**Description**_: Set the string value in argument as value of the key, with a time to live. PSETEX uses a TTL in milliseconds.
##### *Parameters*
*Key*
*TTL*
*Value*
##### *Return value*
*Bool* `TRUE` if the command is successful.
##### *Examples*
~~~php
$redis->setEx('key', 3600, 'value'); // sets key → value, with 1h TTL.
$redis->pSetEx('key', 100, 'value'); // sets key → value, with 0.1 sec TTL.
~~~
### setNx
-----
_**Description**_: Set the string value in argument as value of the key if the key doesn't already exist in the database.
##### *Parameters*
*key*
*value*
##### *Return value*
*Bool* `TRUE` in case of success, `FALSE` in case of failure.
##### *Examples*
~~~php
$redis->setNx('key', 'value'); /* return TRUE */
$redis->setNx('key', 'value'); /* return FALSE */
~~~
### del, delete, unlink
-----
_**Description**_: Remove specified keys.
##### *Parameters*
An array of keys, or an undefined number of parameters, each a key: *key1* *key2* *key3* ... *keyN*
*Note*: If you are connecting to Redis server >= 4.0.0 you can remove a key with the `unlink` method in the exact same way you would use `del`. The Redis [unlink](https://redis.io/commands/unlink) command is non-blocking and will perform the actual deletion asynchronously.
##### *Return value*
*Long* Number of keys deleted.
##### *Examples*
~~~php
$redis->set('key1', 'val1');
$redis->set('key2', 'val2');
$redis->set('key3', 'val3');
$redis->set('key4', 'val4');
$redis->delete('key1', 'key2'); /* return 2 */
$redis->delete(array('key3', 'key4')); /* return 2 */
/* If using Redis >= 4.0.0 you can call unlink */
$redis->unlink('key1', 'key2');
$redis->unlink(Array('key1', 'key2'));
~~~
### exists
-----
_**Description**_: Verify if the specified key exists.
##### *Parameters*
*key*
##### *Return value*
*long*: The number of keys tested that do exist.
##### *Examples*
~~~php
$redis->set('key', 'value');
$redis->exists('key'); /* 1 */
$redis->exists('NonExistingKey'); /* 0 */
$redis->mset(['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz']);
$redis->exists(['foo', 'bar', 'baz]); /* 3 */
$redis->exists('foo', 'bar', 'baz'); /* 3 */
~~~
**Note**: This function took a single argument and returned TRUE or FALSE in phpredis versions < 4.0.0.
### incr, incrBy
-----
_**Description**_: Increment the number stored at key by one. If the second argument is filled, it will be used as the integer value of the increment.
##### *Parameters*
*key*
*value*: value that will be added to key (only for incrBy)
##### *Return value*
*INT* the new value
##### *Examples*
~~~php
$redis->incr('key1'); /* key1 didn't exists, set to 0 before the increment */
/* and now has the value 1 */
$redis->incr('key1'); /* 2 */
$redis->incr('key1'); /* 3 */
$redis->incr('key1'); /* 4 */
// Will redirect, and actually make an INCRBY call
$redis->incr('key1', 10); /* 14 */
$redis->incrBy('key1', 10); /* 24 */
~~~
### incrByFloat
-----
_**Description**_: Increment the key with floating point precision.
##### *Parameters*
*key*
*value*: (float) value that will be added to the key
##### *Return value*
*FLOAT* the new value
##### *Examples*
~~~php
$redis->incrByFloat('key1', 1.5); /* key1 didn't exist, so it will now be 1.5 */
$redis->incrByFloat('key1', 1.5); /* 3 */
$redis->incrByFloat('key1', -1.5); /* 1.5 */
$redis->incrByFloat('key1', 2.5); /* 4 */
~~~
### decr, decrBy
-----
_**Description**_: Decrement the number stored at key by one. If the second argument is filled, it will be used as the integer value of the decrement.
##### *Parameters*
*key*
*value*: value that will be subtracted to key (only for decrBy)
##### *Return value*
*INT* the new value
##### *Examples*
~~~php
$redis->decr('key1'); /* key1 didn't exists, set to 0 before the increment */
/* and now has the value -1 */
$redis->decr('key1'); /* -2 */
$redis->decr('key1'); /* -3 */
// Will redirect, and actually make an DECRBY call
$redis->decr('key1', 10); /* -13 */
$redis->decrBy('key1', 10); /* -23 */
~~~
### mGet, getMultiple
-----
_**Description**_: Get the values of all the specified keys. If one or more keys don't exist, the array will contain `FALSE` at the position of the key.
##### *Parameters*
*Array*: Array containing the list of the keys
##### *Return value*
*Array*: Array containing the values related to keys in argument
##### *Examples*
~~~php
$redis->set('key1', 'value1');
$redis->set('key2', 'value2');
$redis->set('key3', 'value3');
$redis->mGet(array('key1', 'key2', 'key3')); /* array('value1', 'value2', 'value3');
$redis->mGet(array('key0', 'key1', 'key5')); /* array(`FALSE`, 'value1', `FALSE`);
~~~
### getSet
-----
_**Description**_: Sets a value and returns the previous entry at that key.
##### *Parameters*
*Key*: key
*STRING*: value
##### *Return value*
A string, the previous value located at this key.
##### *Example*
~~~php
$redis->set('x', '42');
$exValue = $redis->getSet('x', 'lol'); // return '42', replaces x by 'lol'
$newValue = $redis->get('x')' // return 'lol'
~~~
### randomKey
-----
_**Description**_: Returns a random key.
##### *Parameters*
None.
##### *Return value*
*STRING*: an existing key in redis.
##### *Example*
~~~php
$key = $redis->randomKey();
$surprise = $redis->get($key); // who knows what's in there.
~~~
### move
-----
_**Description**_: Moves a key to a different database.
##### *Parameters*
*Key*: key, the key to move.
*INTEGER*: dbindex, the database number to move the key to.
##### *Return value*
*BOOL*: `TRUE` in case of success, `FALSE` in case of failure.
##### *Example*
~~~php
$redis->select(0); // switch to DB 0
$redis->set('x', '42'); // write 42 to x
$redis->move('x', 1); // move to DB 1
$redis->select(1); // switch to DB 1
$redis->get('x'); // will return 42
~~~
### rename, renameKey
-----
_**Description**_: Renames a key.
##### *Parameters*
*STRING*: srckey, the key to rename.
*STRING*: dstkey, the new name for the key.
##### *Return value*
*BOOL*: `TRUE` in case of success, `FALSE` in case of failure.
##### *Example*
~~~php
$redis->set('x', '42');
$redis->rename('x', 'y');
$redis->get('y'); // → 42
$redis->get('x'); // → `FALSE`
~~~
### renameNx
-----
_**Description**_: Same as rename, but will not replace a key if the destination already exists. This is the same behaviour as setNx.
### expire, setTimeout, pexpire
-----
_**Description**_: Sets an expiration date (a timeout) on an item. pexpire requires a TTL in milliseconds.
##### *Parameters*
*Key*: key. The key that will disappear.
*Integer*: ttl. The key's remaining Time To Live, in seconds.
##### *Return value*
*BOOL*: `TRUE` in case of success, `FALSE` in case of failure.
##### *Example*
~~~php
$redis->set('x', '42');
$redis->setTimeout('x', 3); // x will disappear in 3 seconds.
sleep(5); // wait 5 seconds
$redis->get('x'); // will return `FALSE`, as 'x' has expired.
~~~
### expireAt, pexpireAt
-----
_**Description**_: Sets an expiration date (a timestamp) on an item. pexpireAt requires a timestamp in milliseconds.
##### *Parameters*
*Key*: key. The key that will disappear.
*Integer*: Unix timestamp. The key's date of death, in seconds from Epoch time.
##### *Return value*
*BOOL*: `TRUE` in case of success, `FALSE` in case of failure.
##### *Example*
~~~php
$redis->set('x', '42');
$now = time(NULL); // current timestamp
$redis->expireAt('x', $now + 3); // x will disappear in 3 seconds.
sleep(5); // wait 5 seconds
$redis->get('x'); // will return `FALSE`, as 'x' has expired.
~~~
### keys, getKeys
-----
_**Description**_: Returns the keys that match a certain pattern.
##### *Parameters*
*STRING*: pattern, using '*' as a wildcard.
##### *Return value*
*Array of STRING*: The keys that match a certain pattern.
##### *Example*
~~~php
$allKeys = $redis->keys('*'); // all keys will match this.
$keyWithUserPrefix = $redis->keys('user*');
~~~
### scan
-----
_**Description**_: Scan the keyspace for keys
##### *Parameters*
*LONG (reference)*: Iterator, initialized to NULL
*STRING, Optional*: Pattern to match
*LONG, Optional*: Count of keys per iteration (only a suggestion to Redis)
##### *Return value*
*Array, boolean*: This function will return an array of keys or FALSE if Redis returned zero keys
*Note*: SCAN is a "directed node" command in [RedisCluster](cluster.markdown#directed-node-commands)
##### *Example*
~~~php
/* Without enabling Redis::SCAN_RETRY (default condition) */
$it = NULL;
do {
// Scan for some keys
$arr_keys = $redis->scan($it);
// Redis may return empty results, so protect against that
if ($arr_keys !== FALSE) {
foreach($arr_keys as $str_key) {
echo "Here is a key: $str_key\n";
}
}
} while ($it > 0);
echo "No more keys to scan!\n";
/* With Redis::SCAN_RETRY enabled */
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
$it = NULL;
/* phpredis will retry the SCAN command if empty results are returned from the
server, so no empty results check is required. */
while ($arr_keys = $redis->scan($it)) {
foreach ($arr_keys as $str_key) {
echo "Here is a key: $str_key\n";
}
}
echo "No more keys to scan!\n";
~~~
### object
-----
_**Description**_: Describes the object pointed to by a key.
##### *Parameters*
The information to retrieve (string) and the key (string). Info can be one of the following:
* "encoding"
* "refcount"
* "idletime"
##### *Return value*
*STRING* for "encoding", *LONG* for "refcount" and "idletime", `FALSE` if the key doesn't exist.
##### *Example*
~~~php
$redis->object("encoding", "l"); // → ziplist
$redis->object("refcount", "l"); // → 1
$redis->object("idletime", "l"); // → 400 (in seconds, with a precision of 10 seconds).
~~~
### type
-----
_**Description**_: Returns the type of data pointed by a given key.
##### *Parameters*
*Key*: key
##### *Return value*
Depending on the type of the data pointed by the key, this method will return the following value:
string: Redis::REDIS_STRING
set: Redis::REDIS_SET
list: Redis::REDIS_LIST
zset: Redis::REDIS_ZSET
hash: Redis::REDIS_HASH
other: Redis::REDIS_NOT_FOUND
##### *Example*
~~~php
$redis->type('key');
~~~
### append
-----
_**Description**_: Append specified string to the string stored in specified key.
##### *Parameters*
*Key*
*Value*
##### *Return value*
*INTEGER*: Size of the value after the append
##### *Example*
~~~php
$redis->set('key', 'value1');
$redis->append('key', 'value2'); /* 12 */
$redis->get('key'); /* 'value1value2' */
~~~
### getRange
-----
_**Description**_: Return a substring of a larger string
*Note*: substr also supported but deprecated in redis.
##### *Parameters*
*key*
*start*
*end*
##### *Return value*
*STRING*: the substring
##### *Example*
~~~php
$redis->set('key', 'string value');
$redis->getRange('key', 0, 5); /* 'string' */
$redis->getRange('key', -5, -1); /* 'value' */
~~~
### setRange
-----
_**Description**_: Changes a substring of a larger string.
##### *Parameters*
*key*
*offset*
*value*
##### *Return value*
*STRING*: the length of the string after it was modified.
##### *Example*
~~~php
$redis->set('key', 'Hello world');
$redis->setRange('key', 6, "redis"); /* returns 11 */
$redis->get('key'); /* "Hello redis" */
~~~
### strLen
-----
_**Description**_: Get the length of a string value.
##### *Parameters*
*key*
##### *Return value*
*INTEGER*
##### *Example*
~~~php
$redis->set('key', 'value');
$redis->strlen('key'); /* 5 */
~~~
### getBit
-----
_**Description**_: Return a single bit out of a larger string
##### *Parameters*
*key*
*offset*
##### *Return value*
*LONG*: the bit value (0 or 1)
##### *Example*
~~~php
$redis->set('key', "\x7f"); // this is 0111 1111
$redis->getBit('key', 0); /* 0 */
$redis->getBit('key', 1); /* 1 */
~~~
### setBit
-----
_**Description**_: Changes a single bit of a string.
##### *Parameters*
*key*
*offset*
*value*: bool or int (1 or 0)
##### *Return value*
*LONG*: 0 or 1, the value of the bit before it was set.
##### *Example*
~~~php
$redis->set('key', "*"); // ord("*") = 42 = 0x2f = "0010 1010"
$redis->setBit('key', 5, 1); /* returns 0 */
$redis->setBit('key', 7, 1); /* returns 0 */
$redis->get('key'); /* chr(0x2f) = "/" = b("0010 1111") */
~~~
### bitOp
-----
_**Description**_: Bitwise operation on multiple keys.
##### *Parameters*
*operation*: either "AND", "OR", "NOT", "XOR"
*ret_key*: return key
*key1*
*key2...*
##### *Return value*
*LONG*: The size of the string stored in the destination key.
### bitCount
-----
_**Description**_: Count bits in a string.
##### *Parameters*
*key*
##### *Return value*
*LONG*: The number of bits set to 1 in the value behind the input key.
### sort
-----
_**Description**_: Sort the elements in a list, set or sorted set.
##### *Parameters*
*Key*: key
*Options*: array(key => value, ...) - optional, with the following keys and values:
~~~
'by' => 'some_pattern_*',
'limit' => array(0, 1),
'get' => 'some_other_pattern_*' or an array of patterns,
'sort' => 'asc' or 'desc',
'alpha' => TRUE,
'store' => 'external-key'
~~~
##### *Return value*
An array of values, or a number corresponding to the number of elements stored if that was used.
##### *Example*
~~~php
$redis->delete('s');
$redis->sAdd('s', 5);
$redis->sAdd('s', 4);
$redis->sAdd('s', 2);
$redis->sAdd('s', 1);
$redis->sAdd('s', 3);
var_dump($redis->sort('s')); // 1,2,3,4,5
var_dump($redis->sort('s', array('sort' => 'desc'))); // 5,4,3,2,1
var_dump($redis->sort('s', array('sort' => 'desc', 'store' => 'out'))); // (int)5
~~~
### ttl, pttl
-----
_**Description**_: Returns the time to live left for a given key in seconds (ttl), or milliseconds (pttl).
##### *Parameters*
*Key*: key
##### *Return value*
*LONG*: The time to live in seconds. If the key has no ttl, `-1` will be returned, and `-2` if the key doesn't exist.
##### *Example*
~~~php
$redis->ttl('key');
~~~
### persist
-----
_**Description**_: Remove the expiration timer from a key.
##### *Parameters*
*Key*: key
##### *Return value*
*BOOL*: `TRUE` if a timeout was removed, `FALSE` if the key didn’t exist or didn’t have an expiration timer.
##### *Example*
~~~php
$redis->persist('key');
~~~
### mSet, mSetNx
-----
_**Description**_: Sets multiple key-value pairs in one atomic command. MSETNX only returns TRUE if all the keys were set (see SETNX).
##### *Parameters*
*Pairs*: array(key => value, ...)
##### *Return value*
*Bool* `TRUE` in case of success, `FALSE` in case of failure.
##### *Example*
~~~php
$redis->mSet(array('key0' => 'value0', 'key1' => 'value1'));
var_dump($redis->get('key0'));
var_dump($redis->get('key1'));
~~~
Output:
~~~
string(6) "value0"
string(6) "value1"
~~~
### dump
-----
_**Description**_: Dump a key out of a redis database, the value of which can later be passed into redis using the RESTORE command. The data
that comes out of DUMP is a binary representation of the key as Redis stores it.
##### *Parameters*
*key* string
##### *Return value*
The Redis encoded value of the key, or FALSE if the key doesn't exist
##### *Examples*
~~~php
$redis->set('foo', 'bar');
$val = $redis->dump('foo'); // $val will be the Redis encoded key value
~~~
### restore
-----
_**Description**_: Restore a key from the result of a DUMP operation.
##### *Parameters*
*key* string. The key name
*ttl* integer. How long the key should live (if zero, no expire will be set on the key)
*value* string (binary). The Redis encoded key value (from DUMP)
##### *Examples*
~~~php
$redis->set('foo', 'bar');
$val = $redis->dump('foo');
$redis->restore('bar', 0, $val); // The key 'bar', will now be equal to the key 'foo'
~~~
### migrate
-----
_**Description**_: Migrates a key to a different Redis instance.
**Note:**: Redis introduced migrating multiple keys in 3.0.6, so you must have at least
that version in order to call `migrate` with an array of keys.
##### *Parameters*
*host* string. The destination host
*port* integer. The TCP port to connect to.
*key(s)* string or array.
*destination-db* integer. The target DB.
*timeout* integer. The maximum amount of time given to this transfer.
*copy* boolean, optional. Should we send the COPY flag to redis.
*replace* boolean, optional. Should we send the REPLACE flag to redis
##### *Examples*
~~~php
$redis->migrate('backup', 6379, 'foo', 0, 3600);
$redis->migrate('backup', 6379, 'foo', 0, 3600, true, true); /* copy and replace */
$redis->migrate('backup', 6379, 'foo', 0, 3600, false, true); /* just REPLACE flag */
/* Migrate multiple keys (requires Redis >= 3.0.6)
$redis->migrate('backup', 6379, ['key1', 'key2', 'key3'], 0, 3600);
~~~
## Hashes
* [hDel](#hdel) - Delete one or more hash fields
* [hExists](#hexists) - Determine if a hash field exists
* [hGet](#hget) - Get the value of a hash field
* [hGetAll](#hgetall) - Get all the fields and values in a hash
* [hIncrBy](#hincrby) - Increment the integer value of a hash field by the given number
* [hIncrByFloat](#hincrbyfloat) - Increment the float value of a hash field by the given amount
* [hKeys](#hkeys) - Get all the fields in a hash
* [hLen](#hlen) - Get the number of fields in a hash
* [hMGet](#hmget) - Get the values of all the given hash fields
* [hMSet](#hmset) - Set multiple hash fields to multiple values
* [hSet](#hset) - Set the string value of a hash field
* [hSetNx](#hsetnx) - Set the value of a hash field, only if the field does not exist
* [hVals](#hvals) - Get all the values in a hash
* [hScan](#hscan) - Scan a hash key for members
* [hStrLen](#hstrlen) - Get the string length of the value associated with field in the hash
### hSet
-----
_**Description**_: Adds a value to the hash stored at key.
##### *Parameters*
*key*
*hashKey*
*value*
##### *Return value*
*LONG* `1` if value didn't exist and was added successfully, `0` if the value was already present and was replaced, `FALSE` if there was an error.
##### *Example*
~~~php
$redis->delete('h')
$redis->hSet('h', 'key1', 'hello'); /* 1, 'key1' => 'hello' in the hash at "h" */
$redis->hGet('h', 'key1'); /* returns "hello" */
$redis->hSet('h', 'key1', 'plop'); /* 0, value was replaced. */
$redis->hGet('h', 'key1'); /* returns "plop" */
~~~
### hSetNx
-----
_**Description**_: Adds a value to the hash stored at key only if this field isn't already in the hash.
##### *Return value*
*BOOL* `TRUE` if the field was set, `FALSE` if it was already present.
##### *Example*
~~~php
$redis->delete('h')
$redis->hSetNx('h', 'key1', 'hello'); /* TRUE, 'key1' => 'hello' in the hash at "h" */
$redis->hSetNx('h', 'key1', 'world'); /* FALSE, 'key1' => 'hello' in the hash at "h". No change since the field wasn't replaced. */
~~~
### hGet
-----
_**Description**_: Gets a value from the hash stored at key. If the hash table doesn't exist, or the key doesn't exist, `FALSE` is returned.
##### *Parameters*
*key*
*hashKey*
##### *Return value*
*STRING* The value, if the command executed successfully
*BOOL* `FALSE` in case of failure
### hLen
-----
_**Description**_: Returns the length of a hash, in number of items
##### *Parameters*
*key*
##### *Return value*
*LONG* the number of items in a hash, `FALSE` if the key doesn't exist or isn't a hash.
##### *Example*
~~~php
$redis->delete('h')
$redis->hSet('h', 'key1', 'hello');
$redis->hSet('h', 'key2', 'plop');
$redis->hLen('h'); /* returns 2 */
~~~
### hDel
-----
_**Description**_: Removes a value from the hash stored at key. If the hash table doesn't exist, or the key doesn't exist, `FALSE` is returned.
##### *Parameters*
*key*
*hashKey1*
*hashKey2*
...
##### *Return value*
*LONG* the number of deleted keys, 0 if the key doesn't exist, `FALSE` if the key isn't a hash.
### hKeys
-----
_**Description**_: Returns the keys in a hash, as an array of strings.
##### *Parameters*
*Key*: key
##### *Return value*
An array of elements, the keys of the hash. This works like PHP's array_keys().
##### *Example*
~~~php
$redis->delete('h');
$redis->hSet('h', 'a', 'x');
$redis->hSet('h', 'b', 'y');
$redis->hSet('h', 'c', 'z');
$redis->hSet('h', 'd', 't');
var_dump($redis->hKeys('h'));
~~~
Output:
~~~
array(4) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
[3]=>
string(1) "d"
}
~~~
The order is random and corresponds to redis' own internal representation of the set structure.
### hVals
-----
_**Description**_: Returns the values in a hash, as an array of strings.
##### *Parameters*
*Key*: key
##### *Return value*
An array of elements, the values of the hash. This works like PHP's array_values().
##### *Example*
~~~php
$redis->delete('h');
$redis->hSet('h', 'a', 'x');
$redis->hSet('h', 'b', 'y');
$redis->hSet('h', 'c', 'z');
$redis->hSet('h', 'd', 't');
var_dump($redis->hVals('h'));
~~~
Output:
~~~
array(4) {
[0]=>
string(1) "x"
[1]=>
string(1) "y"
[2]=>
string(1) "z"
[3]=>
string(1) "t"
}
~~~
The order is random and corresponds to redis' own internal representation of the set structure.
### hGetAll
-----
_**Description**_: Returns the whole hash, as an array of strings indexed by strings.
##### *Parameters*
*Key*: key
##### *Return value*
An array of elements, the contents of the hash.
##### *Example*
~~~php
$redis->delete('h');
$redis->hSet('h', 'a', 'x');
$redis->hSet('h', 'b', 'y');
$redis->hSet('h', 'c', 'z');
$redis->hSet('h', 'd', 't');
var_dump($redis->hGetAll('h'));
~~~
Output:
~~~
array(4) {
["a"]=>
string(1) "x"
["b"]=>
string(1) "y"
["c"]=>
string(1) "z"
["d"]=>
string(1) "t"
}
~~~
The order is random and corresponds to redis' own internal representation of the set structure.
### hExists
-----
_**Description**_: Verify if the specified member exists in a key.
##### *Parameters*
*key*
*memberKey*
##### *Return value*
*BOOL*: If the member exists in the hash table, return `TRUE`, otherwise return `FALSE`.
##### *Examples*
~~~php
$redis->hSet('h', 'a', 'x');
$redis->hExists('h', 'a'); /* TRUE */
$redis->hExists('h', 'NonExistingKey'); /* FALSE */
~~~
### hIncrBy
-----
_**Description**_: Increments the value of a member from a hash by a given amount.
##### *Parameters*
*key*
*member*
*value*: (integer) value that will be added to the member's value
##### *Return value*
*LONG* the new value
##### *Examples*
~~~php
$redis->delete('h');
$redis->hIncrBy('h', 'x', 2); /* returns 2: h[x] = 2 now. */
$redis->hIncrBy('h', 'x', 1); /* h[x] ← 2 + 1. Returns 3 */
~~~
### hIncrByFloat
-----
_**Description**_: Increments the value of a hash member by the provided float value
##### *Parameters*
*key*
*member*
*value*: (float) value that will be added to the member's value
##### *Return value*
*FLOAT* the new value
##### *Examples*
~~~php
$redis->delete('h');
$redis->hIncrByFloat('h','x', 1.5); /* returns 1.5: h[x] = 1.5 now */
$redis->hIncrByFloat('h', 'x', 1.5); /* returns 3.0: h[x] = 3.0 now */
$redis->hIncrByFloat('h', 'x', -3.0); /* returns 0.0: h[x] = 0.0 now */
~~~
### hMSet
-----
_**Description**_: Fills in a whole hash. Non-string values are converted to string, using the standard `(string)` cast. NULL values are stored as empty strings.
##### *Parameters*
*key*
*members*: key → value array
##### *Return value*
*BOOL*
##### *Examples*
~~~php
$redis->delete('user:1');
$redis->hMSet('user:1', array('name' => 'Joe', 'salary' => 2000));
$redis->hIncrBy('user:1', 'salary', 100); // Joe earns 100 more now.
~~~
### hMGet
-----
_**Description**_: Retrieve the values associated to the specified fields in the hash.
##### *Parameters*
*key*
*memberKeys* Array
##### *Return value*
*Array* An array of elements, the values of the specified fields in the hash, with the hash keys as array keys.
##### *Examples*
~~~php
$redis->delete('h');
$redis->hSet('h', 'field1', 'value1');
$redis->hSet('h', 'field2', 'value2');
$redis->hMGet('h', array('field1', 'field2')); /* returns array('field1' => 'value1', 'field2' => 'value2') */
~~~
### hScan
-----
_**Description**_: Scan a HASH value for members, with an optional pattern and count
##### *Parameters*
*key*: String
*iterator*: Long (reference)
*pattern*: Optional pattern to match against
*count*: How many keys to return in a go (only a suggestion to Redis)
##### *Return value*
*Array* An array of members that match our pattern
##### *Examples*
~~~php
$it = NULL;
/* Don't ever return an empty array until we're done iterating */
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
while($arr_keys = $redis->hScan('hash', $it)) {
foreach($arr_keys as $str_field => $str_value) {
echo "$str_field => $str_value\n"; /* Print the hash member and value */
}
}
~~~
### hStrLen
-----
_**Description**_: Get the string length of the value associated with field in the hash stored at key.
##### *Parameters*
*key*: String
*field*: String
##### *Return value*
*LONG* the string length of the value associated with field, or zero when field is not present in the hash or key does not exist at all.
## Lists
* [blPop, brPop](#blpop-brpop) - Remove and get the first/last element in a list
* [bRPopLPush](#brpoplpush) - Pop a value from a list, push it to another list and return it
* [lIndex, lGet](#lindex-lget) - Get an element from a list by its index
* [lInsert](#linsert) - Insert an element before or after another element in a list
* [lLen, lSize](#llen-lsize) - Get the length/size of a list
* [lPop](#lpop) - Remove and get the first element in a list
* [lPush](#lpush) - Prepend one or multiple values to a list
* [lPushx](#lpushx) - Prepend a value to a list, only if the list exists
* [lRange, lGetRange](#lrange-lgetrange) - Get a range of elements from a list
* [lRem, lRemove](#lrem-lremove) - Remove elements from a list
* [lSet](#lset) - Set the value of an element in a list by its index
* [lTrim, listTrim](#ltrim-listtrim) - Trim a list to the specified range
* [rPop](#rpop) - Remove and get the last element in a list
* [rPopLPush](#rpoplpush) - Remove the last element in a list, append it to another list and return it (redis >= 1.1)
* [rPush](#rpush) - Append one or multiple values to a list
* [rPushX](#rpushx) - Append a value to a list, only if the list exists
### blPop, brPop
-----
_**Description**_: Is a blocking lPop(rPop) primitive. If at least one of the lists contains at least one element, the element will be popped from the head of the list and returned to the caller.
If all the list identified by the keys passed in arguments are empty, blPop will block during the specified timeout until an element is pushed to one of those lists. This element will be popped.
##### *Parameters*
*ARRAY* Array containing the keys of the lists
*INTEGER* Timeout
Or
*STRING* Key1
*STRING* Key2
*STRING* Key3
...
*STRING* Keyn
*INTEGER* Timeout
##### *Return value*
*ARRAY* array('listName', 'element')
##### *Example*
~~~php
/* Non blocking feature */
$redis->lPush('key1', 'A');
$redis->delete('key2');
$redis->blPop('key1', 'key2', 10); /* array('key1', 'A') */
/* OR */
$redis->blPop(array('key1', 'key2'), 10); /* array('key1', 'A') */
$redis->brPop('key1', 'key2', 10); /* array('key1', 'A') */
/* OR */
$redis->brPop(array('key1', 'key2'), 10); /* array('key1', 'A') */
/* Blocking feature */
/* process 1 */
$redis->delete('key1');
$redis->blPop('key1', 10);
/* blocking for 10 seconds */
/* process 2 */
$redis->lPush('key1', 'A');
/* process 1 */
/* array('key1', 'A') is returned*/
~~~
### bRPopLPush
-----
_**Description**_: A blocking version of `rPopLPush`, with an integral timeout in the third parameter.
##### *Parameters*
*Key*: srckey
*Key*: dstkey
*Long*: timeout
##### *Return value*
*STRING* The element that was moved in case of success, `FALSE` in case of timeout.
### lIndex, lGet
-----
_**Description**_: Return the specified element of the list stored at the specified key.
0 the first element, 1 the second ...
-1 the last element, -2 the penultimate ...
Return `FALSE` in case of a bad index or a key that doesn't point to a list.
##### *Parameters*
*key*
*index*
##### *Return value*
*String* the element at this index
*Bool* `FALSE` if the key identifies a non-string data type, or no value corresponds to this index in the list `Key`.
##### *Example*
~~~php
$redis->rPush('key1', 'A');
$redis->rPush('key1', 'B');
$redis->rPush('key1', 'C'); /* key1 => [ 'A', 'B', 'C' ] */
$redis->lGet('key1', 0); /* 'A' */
$redis->lGet('key1', -1); /* 'C' */
$redis->lGet('key1', 10); /* `FALSE` */
~~~
### lInsert
-----
_**Description**_: Insert value in the list before or after the pivot value.
The parameter options specify the position of the insert (before or after).
If the list didn't exists, or the pivot didn't exists, the value is not inserted.
##### *Parameters*
*key*
*position* Redis::BEFORE | Redis::AFTER
*pivot*
*value*
##### *Return value*
The number of the elements in the list, -1 if the pivot didn't exists.
##### *Example*
~~~php
$redis->delete('key1');
$redis->lInsert('key1', Redis::AFTER, 'A', 'X'); /* 0 */
$redis->lPush('key1', 'A');
$redis->lPush('key1', 'B');
$redis->lPush('key1', 'C');
$redis->lInsert('key1', Redis::BEFORE, 'C', 'X'); /* 4 */
$redis->lRange('key1', 0, -1); /* array('A', 'B', 'X', 'C') */
$redis->lInsert('key1', Redis::AFTER, 'C', 'Y'); /* 5 */
$redis->lRange('key1', 0, -1); /* array('A', 'B', 'X', 'C', 'Y') */
$redis->lInsert('key1', Redis::AFTER, 'W', 'value'); /* -1 */
~~~
### lPop
-----
_**Description**_: Return and remove the first element of the list.
##### *Parameters*
*key*
##### *Return value*
*STRING* if command executed successfully
*BOOL* `FALSE` in case of failure (empty list)
##### *Example*
~~~php
$redis->rPush('key1', 'A');
$redis->rPush('key1', 'B');
$redis->rPush('key1', 'C'); /* key1 => [ 'A', 'B', 'C' ] */
$redis->lPop('key1'); /* key1 => [ 'B', 'C' ] */
~~~
### lPush
-----
_**Description**_: Adds the string value to the head (left) of the list. Creates the list if the key didn't exist. If the key exists and is not a list, `FALSE` is returned.
##### *Parameters*
*key*
*value* String, value to push in key
##### *Return value*
*LONG* The new length of the list in case of success, `FALSE` in case of Failure.
##### *Examples*
~~~php
$redis->delete('key1');
$redis->lPush('key1', 'C'); // returns 1
$redis->lPush('key1', 'B'); // returns 2
$redis->lPush('key1', 'A'); // returns 3
/* key1 now points to the following list: [ 'A', 'B', 'C' ] */
~~~
### lPushx
-----
_**Description**_: Adds the string value to the head (left) of the list if the list exists.
##### *Parameters*
*key*
*value* String, value to push in key
##### *Return value*
*LONG* The new length of the list in case of success, `FALSE` in case of Failure.
##### *Examples*
~~~php
$redis->delete('key1');
$redis->lPushx('key1', 'A'); // returns 0
$redis->lPush('key1', 'A'); // returns 1
$redis->lPushx('key1', 'B'); // returns 2
$redis->lPushx('key1', 'C'); // returns 3
/* key1 now points to the following list: [ 'A', 'B', 'C' ] */
~~~
### lRange, lGetRange
-----
_**Description**_: Returns the specified elements of the list stored at the specified key in the range [start, end]. start and stop are interpreted as indices:
0 the first element, 1 the second ...
-1 the last element, -2 the penultimate ...
##### *Parameters*
*key*
*start*
*end*
##### *Return value*
*Array* containing the values in specified range.
##### *Example*
~~~php
$redis->rPush('key1', 'A');
$redis->rPush('key1', 'B');
$redis->rPush('key1', 'C');
$redis->lRange('key1', 0, -1); /* array('A', 'B', 'C') */
~~~
### lRem, lRemove
-----
_**Description**_: Removes the first `count` occurrences of the value element from the list. If count is zero, all the matching elements are removed. If count is negative, elements are removed from tail to head.
**Note**: The argument order is not the same as in the Redis documentation. This difference is kept for compatibility reasons.
##### *Parameters*
*key*
*value*
*count*
##### *Return value*
*LONG* the number of elements to remove
*BOOL* `FALSE` if the value identified by key is not a list.
##### *Example*
~~~php
$redis->lPush('key1', 'A');
$redis->lPush('key1', 'B');
$redis->lPush('key1', 'C');
$redis->lPush('key1', 'A');
$redis->lPush('key1', 'A');
$redis->lRange('key1', 0, -1); /* array('A', 'A', 'C', 'B', 'A') */
$redis->lRem('key1', 'A', 2); /* 2 */
$redis->lRange('key1', 0, -1); /* array('C', 'B', 'A') */
~~~
### lSet
-----
_**Description**_: Set the list at index with the new value.
##### *Parameters*
*key*
*index*
*value*
##### *Return value*
*BOOL* `TRUE` if the new value was set. `FALSE` if the index is out of range, or data type identified by key is not a list.
##### *Example*
~~~php
$redis->rPush('key1', 'A');
$redis->rPush('key1', 'B');
$redis->rPush('key1', 'C'); /* key1 => [ 'A', 'B', 'C' ] */
$redis->lGet('key1', 0); /* 'A' */
$redis->lSet('key1', 0, 'X');
$redis->lGet('key1', 0); /* 'X' */
~~~
### lTrim, listTrim
-----
_**Description**_: Trims an existing list so that it will contain only a specified range of elements.
##### *Parameters*
*key*
*start*
*stop*
##### *Return value*
*Array*
*Bool* return `FALSE` if the key identify a non-list value.
##### *Example*
~~~php
$redis->rPush('key1', 'A');
$redis->rPush('key1', 'B');
$redis->rPush('key1', 'C');
$redis->lRange('key1', 0, -1); /* array('A', 'B', 'C') */
$redis->lTrim('key1', 0, 1);
$redis->lRange('key1', 0, -1); /* array('A', 'B') */
~~~
### rPop
-----
_**Description**_: Returns and removes the last element of the list.
##### *Parameters*
*key*
##### *Return value*
*STRING* if command executed successfully
*BOOL* `FALSE` in case of failure (empty list)
##### *Example*
~~~php
$redis->rPush('key1', 'A');
$redis->rPush('key1', 'B');
$redis->rPush('key1', 'C'); /* key1 => [ 'A', 'B', 'C' ] */
$redis->rPop('key1'); /* key1 => [ 'A', 'B' ] */
~~~
### rPopLPush
-----
_**Description**_: Pops a value from the tail of a list, and pushes it to the front of another list. Also return this value. (redis >= 1.1)
##### *Parameters*
*Key*: srckey
*Key*: dstkey
##### *Return value*
*STRING* The element that was moved in case of success, `FALSE` in case of failure.
##### *Example*
~~~php
$redis->delete('x', 'y');
$redis->lPush('x', 'abc');
$redis->lPush('x', 'def');
$redis->lPush('y', '123');
$redis->lPush('y', '456');
// move the last of x to the front of y.
var_dump($redis->rPopLPush('x', 'y'));
var_dump($redis->lRange('x', 0, -1));
var_dump($redis->lRange('y', 0, -1));
~~~
Output:
~~~
string(3) "abc"
array(1) {
[0]=>
string(3) "def"
}
array(3) {
[0]=>
string(3) "abc"
[1]=>
string(3) "456"
[2]=>
string(3) "123"
}
~~~
### rPush
-----
_**Description**_: Adds the string value to the tail (right) of the list. Creates the list if the key didn't exist. If the key exists and is not a list, `FALSE` is returned.
##### *Parameters*
*key*
*value* String, value to push in key
##### *Return value*
*LONG* The new length of the list in case of success, `FALSE` in case of Failure.
##### *Examples*
~~~php
$redis->delete('key1');
$redis->rPush('key1', 'A'); // returns 1
$redis->rPush('key1', 'B'); // returns 2
$redis->rPush('key1', 'C'); // returns 3
/* key1 now points to the following list: [ 'A', 'B', 'C' ] */
~~~
### rPushX
-----
_**Description**_: Adds the string value to the tail (right) of the list if the ist exists. `FALSE` in case of Failure.
##### *Parameters*
*key*
*value* String, value to push in key
##### *Return value*
*LONG* The new length of the list in case of success, `FALSE` in case of Failure.
##### *Examples*
~~~php
$redis->delete('key1');
$redis->rPushX('key1', 'A'); // returns 0
$redis->rPush('key1', 'A'); // returns 1
$redis->rPushX('key1', 'B'); // returns 2
$redis->rPushX('key1', 'C'); // returns 3
/* key1 now points to the following list: [ 'A', 'B', 'C' ] */
~~~
### lLen, lSize
-----
_**Description**_: Returns the size of a list identified by Key.
If the list didn't exist or is empty, the command returns 0. If the data type identified by Key is not a list, the command return `FALSE`.
##### *Parameters*
*Key*
##### *Return value*
*LONG* The size of the list identified by Key exists.
*BOOL* `FALSE` if the data type identified by Key is not list
##### *Example*
~~~php
$redis->rPush('key1', 'A');
$redis->rPush('key1', 'B');
$redis->rPush('key1', 'C'); /* key1 => [ 'A', 'B', 'C' ] */
$redis->lSize('key1');/* 3 */
$redis->rPop('key1');
$redis->lSize('key1');/* 2 */
~~~
## Sets
* [sAdd](#sadd) - Add one or more members to a set
* [sCard, sSize](#scard-ssize) - Get the number of members in a set
* [sDiff](#sdiff) - Subtract multiple sets
* [sDiffStore](#sdiffstore) - Subtract multiple sets and store the resulting set in a key
* [sInter](#sinter) - Intersect multiple sets
* [sInterStore](#sinterstore) - Intersect multiple sets and store the resulting set in a key
* [sIsMember, sContains](#sismember-scontains) - Determine if a given value is a member of a set
* [sMembers, sGetMembers](#smembers-sgetmembers) - Get all the members in a set
* [sMove](#smove) - Move a member from one set to another
* [sPop](#spop) - Remove and return one or more members of a set at random
* [sRandMember](#srandmember) - Get one or multiple random members from a set
* [sRem, sRemove](#srem-sremove) - Remove one or more members from a set
* [sUnion](#sunion) - Add multiple sets
* [sUnionStore](#sunionstore) - Add multiple sets and store the resulting set in a key
* [sScan](#sscan) - Scan a set for members
### sAdd
-----
_**Description**_: Adds a value to the set value stored at key. If this value is already in the set, `FALSE` is returned.
##### *Parameters*
*key*
*value*
##### *Return value*
*LONG* the number of elements added to the set.
##### *Example*
~~~php
$redis->sAdd('key1' , 'member1'); /* 1, 'key1' => {'member1'} */
$redis->sAdd('key1' , 'member2', 'member3'); /* 2, 'key1' => {'member1', 'member2', 'member3'}*/
$redis->sAdd('key1' , 'member2'); /* 0, 'key1' => {'member1', 'member2', 'member3'}*/
~~~
### sCard, sSize
-----
_**Description**_: Returns the cardinality of the set identified by key.
##### *Parameters*
*key*
##### *Return value*
*LONG* the cardinality of the set identified by key, 0 if the set doesn't exist.
##### *Example*
~~~php
$redis->sAdd('key1' , 'member1');
$redis->sAdd('key1' , 'member2');
$redis->sAdd('key1' , 'member3'); /* 'key1' => {'member1', 'member2', 'member3'}*/
$redis->sCard('key1'); /* 3 */
$redis->sCard('keyX'); /* 0 */
~~~
### sDiff
-----
_**Description**_: Performs the difference between N sets and returns it.
##### *Parameters*
*Keys*: key1, key2, ... , keyN: Any number of keys corresponding to sets in redis.
##### *Return value*
*Array of strings*: The difference of the first set will all the others.
##### *Example*
~~~php
$redis->delete('s0', 's1', 's2');
$redis->sAdd('s0', '1');
$redis->sAdd('s0', '2');
$redis->sAdd('s0', '3');
$redis->sAdd('s0', '4');
$redis->sAdd('s1', '1');
$redis->sAdd('s2', '3');
var_dump($redis->sDiff('s0', 's1', 's2'));
~~~
Return value: all elements of s0 that are neither in s1 nor in s2.
~~~
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "2"
}
~~~
### sDiffStore
-----
_**Description**_: Performs the same action as sDiff, but stores the result in the first key
##### *Parameters*
*Key*: dstkey, the key to store the diff into.
*Keys*: key1, key2, ... , keyN: Any number of keys corresponding to sets in redis
##### *Return value*
*INTEGER*: The cardinality of the resulting set, or `FALSE` in case of a missing key.
##### *Example*
~~~php
$redis->delete('s0', 's1', 's2');
$redis->sAdd('s0', '1');
$redis->sAdd('s0', '2');
$redis->sAdd('s0', '3');
$redis->sAdd('s0', '4');
$redis->sAdd('s1', '1');
$redis->sAdd('s2', '3');
var_dump($redis->sDiffStore('dst', 's0', 's1', 's2'));
var_dump($redis->sMembers('dst'));
~~~
Return value: the number of elements of s0 that are neither in s1 nor in s2.
~~~
int(2)
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "2"
}
~~~
### sInter
-----
_**Description**_: Returns the members of a set resulting from the intersection of all the sets held at the specified keys.
If just a single key is specified, then this command produces the members of this set. If one of the keys
is missing, `FALSE` is returned.
##### *Parameters*
key1, key2, keyN: keys identifying the different sets on which we will apply the intersection.
##### *Return value*
Array, contain the result of the intersection between those keys. If the intersection between the different sets is empty, the return value will be empty array.
##### *Examples*
~~~php
$redis->sAdd('key1', 'val1');
$redis->sAdd('key1', 'val2');
$redis->sAdd('key1', 'val3');
$redis->sAdd('key1', 'val4');
$redis->sAdd('key2', 'val3');
$redis->sAdd('key2', 'val4');
$redis->sAdd('key3', 'val3');
$redis->sAdd('key3', 'val4');
var_dump($redis->sInter('key1', 'key2', 'key3'));
~~~
Output:
~~~
array(2) {
[0]=>
string(4) "val4"
[1]=>
string(4) "val3"
}
~~~
### sInterStore
-----
_**Description**_: Performs a sInter command and stores the result in a new set.
##### *Parameters*
*Key*: dstkey, the key to store the diff into.
*Keys*: key1, key2... keyN. key1..keyN are intersected as in sInter.
##### *Return value*
*INTEGER*: The cardinality of the resulting set, or `FALSE` in case of a missing key.
##### *Example*
~~~php
$redis->sAdd('key1', 'val1');
$redis->sAdd('key1', 'val2');
$redis->sAdd('key1', 'val3');
$redis->sAdd('key1', 'val4');
$redis->sAdd('key2', 'val3');
$redis->sAdd('key2', 'val4');
$redis->sAdd('key3', 'val3');
$redis->sAdd('key3', 'val4');
var_dump($redis->sInterStore('output', 'key1', 'key2', 'key3'));
var_dump($redis->sMembers('output'));
~~~
Output:
~~~
int(2)
array(2) {
[0]=>
string(4) "val4"
[1]=>
string(4) "val3"
}
~~~
### sIsMember, sContains
-----
_**Description**_: Checks if `value` is a member of the set stored at the key `key`.
##### *Parameters*
*key*
*value*
##### *Return value*
*BOOL* `TRUE` if `value` is a member of the set at key `key`, `FALSE` otherwise.
##### *Example*
~~~php
$redis->sAdd('key1' , 'member1');
$redis->sAdd('key1' , 'member2');
$redis->sAdd('key1' , 'member3'); /* 'key1' => {'member1', 'member2', 'member3'}*/
$redis->sIsMember('key1', 'member1'); /* TRUE */
$redis->sIsMember('key1', 'memberX'); /* FALSE */
~~~
### sMembers, sGetMembers
-----
_**Description**_: Returns the contents of a set.
##### *Parameters*
*Key*: key
##### *Return value*
An array of elements, the contents of the set.
##### *Example*
~~~php
$redis->delete('s');
$redis->sAdd('s', 'a');
$redis->sAdd('s', 'b');
$redis->sAdd('s', 'a');
$redis->sAdd('s', 'c');
var_dump($redis->sMembers('s'));
~~~
Output:
~~~
array(3) {
[0]=>
string(1) "c"
[1]=>
string(1) "a"
[2]=>
string(1) "b"
}
~~~
The order is random and corresponds to redis' own internal representation of the set structure.
### sMove
-----
_**Description**_: Moves the specified member from the set at srcKey to the set at dstKey.
##### *Parameters*
*srcKey*
*dstKey*
*member*
##### *Return value*
*BOOL* If the operation is successful, return `TRUE`. If the srcKey and/or dstKey didn't exist, and/or the member didn't exist in srcKey, `FALSE` is returned.
##### *Example*
~~~php
$redis->sAdd('key1' , 'member11');
$redis->sAdd('key1' , 'member12');
$redis->sAdd('key1' , 'member13'); /* 'key1' => {'member11', 'member12', 'member13'}*/
$redis->sAdd('key2' , 'member21');
$redis->sAdd('key2' , 'member22'); /* 'key2' => {'member21', 'member22'}*/
$redis->sMove('key1', 'key2', 'member13'); /* 'key1' => {'member11', 'member12'} */
/* 'key2' => {'member21', 'member22', 'member13'} */
~~~
### sPop
-----
_**Description**_: Removes and returns a random element from the set value at Key.
##### *Parameters*
*key*
*count*: Integer, optional
##### *Return value (without count argument)*
*String* "popped" value
*Bool* `FALSE` if set identified by key is empty or doesn't exist.
##### *Return value (with count argument)*
*Array*: Member(s) returned or an empty array if the set doesn't exist
*Bool*: `FALSE` on error if the key is not a set
##### *Example*
~~~php
$redis->sAdd('key1' , 'member1');
$redis->sAdd('key1' , 'member2');
$redis->sAdd('key1' , 'member3'); /* 'key1' => {'member3', 'member1', 'member2'}*/
$redis->sPop('key1'); /* 'member1', 'key1' => {'member3', 'member2'} */
$redis->sPop('key1'); /* 'member3', 'key1' => {'member2'} */
/* With count */
$redis->sAdd('key2', 'member1', 'member2', 'member3');
$redis->sPop('key2', 3); /* Will return all members but in no particular order */
~~~
### sRandMember
-----
_**Description**_: Returns a random element from the set value at Key, without removing it.
##### *Parameters*
*key*
*count* (Integer, optional)
##### *Return value*
If no count is provided, a random *String* value from the set will be returned. If a count
is provided, an array of values from the set will be returned. Read about the different
ways to use the count here: [SRANDMEMBER](http://redis.io/commands/srandmember)
*Bool* `FALSE` if set identified by key is empty or doesn't exist.
##### *Example*
~~~php
$redis->sAdd('key1' , 'member1');
$redis->sAdd('key1' , 'member2');
$redis->sAdd('key1' , 'member3'); /* 'key1' => {'member3', 'member1', 'member2'}*/
// No count
$redis->sRandMember('key1'); /* 'member1', 'key1' => {'member3', 'member1', 'member2'} */
$redis->sRandMember('key1'); /* 'member3', 'key1' => {'member3', 'member1', 'member2'} */
// With a count
$redis->sRandMember('key1', 3); // Will return an array with all members from the set
$redis->sRandMember('key1', 2); // Will an array with 2 members of the set
$redis->sRandMember('key1', -100); // Will return an array of 100 elements, picked from our set (with dups)
$redis->sRandMember('empty-set', 100); // Will return an empty array
$redis->sRandMember('not-a-set', 100); // Will return FALSE
~~~
### sRem, sRemove
-----
_**Description**_: Removes the specified member from the set value stored at key.
##### *Parameters*
*key*
*member*
##### *Return value*
*LONG* The number of elements removed from the set.
##### *Example*
~~~php
$redis->sAdd('key1' , 'member1');
$redis->sAdd('key1' , 'member2');
$redis->sAdd('key1' , 'member3'); /* 'key1' => {'member1', 'member2', 'member3'}*/
$redis->sRem('key1', 'member2', 'member3'); /*return 2. 'key1' => {'member1'} */
~~~
### sUnion
-----
_**Description**_: Performs the union between N sets and returns it.
##### *Parameters*
*Keys*: key1, key2, ... , keyN: Any number of keys corresponding to sets in redis.
##### *Return value*
*Array of strings*: The union of all these sets.
##### *Example*
~~~php
$redis->delete('s0', 's1', 's2');
$redis->sAdd('s0', '1');
$redis->sAdd('s0', '2');
$redis->sAdd('s1', '3');
$redis->sAdd('s1', '1');
$redis->sAdd('s2', '3');
$redis->sAdd('s2', '4');
var_dump($redis->sUnion('s0', 's1', 's2'));
~~~
Return value: all elements that are either in s0 or in s1 or in s2.
~~~
array(4) {
[0]=>
string(1) "3"
[1]=>
string(1) "4"
[2]=>
string(1) "1"
[3]=>
string(1) "2"
}
~~~
### sUnionStore
-----
_**Description**_: Performs the same action as sUnion, but stores the result in the first key
##### *Parameters*
*Key*: dstkey, the key to store the diff into.
*Keys*: key1, key2, ... , keyN: Any number of keys corresponding to sets in redis.
##### *Return value*
*INTEGER*: The cardinality of the resulting set, or `FALSE` in case of a missing key.
##### *Example*
~~~php
$redis->delete('s0', 's1', 's2');
$redis->sAdd('s0', '1');
$redis->sAdd('s0', '2');
$redis->sAdd('s1', '3');
$redis->sAdd('s1', '1');
$redis->sAdd('s2', '3');
$redis->sAdd('s2', '4');
var_dump($redis->sUnionStore('dst', 's0', 's1', 's2'));
var_dump($redis->sMembers('dst'));
~~~
Return value: the number of elements that are either in s0 or in s1 or in s2.
~~~
int(4)
array(4) {
[0]=>
string(1) "3"
[1]=>
string(1) "4"
[2]=>
string(1) "1"
[3]=>
string(1) "2"
}
~~~
### sScan
-----
_**Description**_: Scan a set for members
##### *Parameters*
*Key*: The set to search
*iterator*: LONG (reference) to the iterator as we go
*pattern*: String, optional pattern to match against
*count*: How many members to return at a time (Redis might return a different amount)
##### *Return value*
*Array, boolean*: PHPRedis will return an array of keys or FALSE when we're done iterating
##### *Example*
~~~php
$it = NULL;
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY); /* don't return empty results until we're done */
while($arr_mems = $redis->sScan('set', $it, "*pattern*")) {
foreach($arr_mems as $str_mem) {
echo "Member: $str_mem\n";
}
}
$it = NULL;
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_NORETRY); /* return after each iteration, even if empty */
while(($arr_mems = $redis->sScan('set', $it, "*pattern*"))!==FALSE) {
if(count($arr_mems) > 0) {
foreach($arr_mems as $str_mem) {
echo "Member found: $str_mem\n";
}
} else {
echo "No members in this iteration, iterator value: $it\n";
}
}
~~~
## Sorted sets
* [zAdd](#zadd) - Add one or more members to a sorted set or update its score if it already exists
* [zCard, zSize](#zcard-zsize) - Get the number of members in a sorted set
* [zCount](#zcount) - Count the members in a sorted set with scores within the given values
* [zIncrBy](#zincrby) - Increment the score of a member in a sorted set
* [zInter](#zinter) - Intersect multiple sorted sets and store the resulting sorted set in a new key
* [zRange](#zrange) - Return a range of members in a sorted set, by index
* [zRangeByScore, zRevRangeByScore](#zrangebyscore-zrevrangebyscore) - Return a range of members in a sorted set, by score
* [zRangeByLex](#zrangebylex) - Return a lexicographical range from members that share the same score
* [zRank, zRevRank](#zrank-zrevrank) - Determine the index of a member in a sorted set
* [zRem, zDelete](#zrem-zdelete) - Remove one or more members from a sorted set
* [zRemRangeByRank, zDeleteRangeByRank](#zremrangebyrank-zdeleterangebyrank) - Remove all members in a sorted set within the given indexes
* [zRemRangeByScore, zDeleteRangeByScore](#zremrangebyscore-zdeleterangebyscore) - Remove all members in a sorted set within the given scores
* [zRevRange](#zrevrange) - Return a range of members in a sorted set, by index, with scores ordered from high to low
* [zScore](#zscore) - Get the score associated with the given member in a sorted set
* [zUnion](#zunion) - Add multiple sorted sets and store the resulting sorted set in a new key
* [zScan](#zscan) - Scan a sorted set for members
### zAdd
-----
_**Description**_: Add one or more members to a sorted set or update its score if it already exists
##### *Parameters*
*key*
*score*: double
*value*: string
##### *Return value*
*Long* 1 if the element is added. 0 otherwise.
##### *Example*
~~~php
$redis->zAdd('key', 1, 'val1');
$redis->zAdd('key', 0, 'val0');
$redis->zAdd('key', 5, 'val5');
$redis->zRange('key', 0, -1); // array(val0, val1, val5)
~~~
### zCard, zSize
-----
_**Description**_: Returns the cardinality of an ordered set.
##### *Parameters*
*key*
##### *Return value*
*Long*, the set's cardinality
##### *Example*
~~~php
$redis->zAdd('key', 0, 'val0');
$redis->zAdd('key', 2, 'val2');
$redis->zAdd('key', 10, 'val10');
$redis->zSize('key'); /* 3 */
~~~
### zCount
-----
_**Description**_: Returns the *number* of elements of the sorted set stored at the specified key which have scores in the range [start,end]. Adding a parenthesis before `start` or `end` excludes it from the range. +inf and -inf are also valid limits.
##### *Parameters*
*key*
*start*: string
*end*: string
##### *Return value*
*LONG* the size of a corresponding zRangeByScore.
##### *Example*
~~~php
$redis->zAdd('key', 0, 'val0');
$redis->zAdd('key', 2, 'val2');
$redis->zAdd('key', 10, 'val10');
$redis->zCount('key', 0, 3); /* 2, corresponding to array('val0', 'val2') */
~~~
### zIncrBy
-----
_**Description**_: Increments the score of a member from a sorted set by a given amount.
##### *Parameters*
*key*
*value*: (double) value that will be added to the member's score
*member*
##### *Return value*
*DOUBLE* the new value
##### *Examples*
~~~php
$redis->delete('key');
$redis->zIncrBy('key', 2.5, 'member1'); /* key or member1 didn't exist, so member1's score is to 0 before the increment */
/* and now has the value 2.5 */
$redis->zIncrBy('key', 1, 'member1'); /* 3.5 */
~~~
### zInter
-----
_**Description**_: Creates an intersection of sorted sets given in second argument. The result of the union will be stored in the sorted set defined by the first argument.
The third optional argument defines `weights` to apply to the sorted sets in input. In this case, the `weights` will be multiplied by the score of each element in the sorted set before applying the aggregation.
The forth argument defines the `AGGREGATE` option which specify how the results of the union are aggregated.
##### *Parameters*
*keyOutput*
*arrayZSetKeys*
*arrayWeights*
*aggregateFunction* Either "SUM", "MIN", or "MAX": defines the behaviour to use on duplicate entries during the zInter.
##### *Return value*
*LONG* The number of values in the new sorted set.
##### *Example*
~~~php
$redis->delete('k1');
$redis->delete('k2');
$redis->delete('k3');
$redis->delete('ko1');
$redis->delete('ko2');
$redis->delete('ko3');
$redis->delete('ko4');
$redis->zAdd('k1', 0, 'val0');
$redis->zAdd('k1', 1, 'val1');
$redis->zAdd('k1', 3, 'val3');
$redis->zAdd('k2', 2, 'val1');
$redis->zAdd('k2', 3, 'val3');
$redis->zInter('ko1', array('k1', 'k2')); /* 2, 'ko1' => array('val1', 'val3') */
$redis->zInter('ko2', array('k1', 'k2'), array(1, 1)); /* 2, 'ko2' => array('val1', 'val3') */
/* Weighted zInter */
$redis->zInter('ko3', array('k1', 'k2'), array(1, 5), 'min'); /* 2, 'ko3' => array('val1', 'val3') */
$redis->zInter('ko4', array('k1', 'k2'), array(1, 5), 'max'); /* 2, 'ko4' => array('val3', 'val1') */
~~~
### zRange
-----
_**Description**_: Returns a range of elements from the ordered set stored at the specified key, with values in the range [start, end].
Start and stop are interpreted as zero-based indices:
`0` the first element, `1` the second ...
`-1` the last element, `-2` the penultimate ...
##### *Parameters*
*key*
*start*: long
*end*: long
*withscores*: bool = false
##### *Return value*
*Array* containing the values in specified range.
##### *Example*
~~~php
$redis->zAdd('key1', 0, 'val0');
$redis->zAdd('key1', 2, 'val2');
$redis->zAdd('key1', 10, 'val10');
$redis->zRange('key1', 0, -1); /* array('val0', 'val2', 'val10') */
// with scores
$redis->zRange('key1', 0, -1, true); /* array('val0' => 0, 'val2' => 2, 'val10' => 10) */
~~~
### zRangeByScore, zRevRangeByScore
-----
_**Description**_: Returns the elements of the sorted set stored at the specified key which have scores in the range [start,end]. Adding a parenthesis before `start` or `end` excludes it from the range. +inf and -inf are also valid limits. zRevRangeByScore returns the same items in reverse order, when the `start` and `end` parameters are swapped.
##### *Parameters*
*key*
*start*: string
*end*: string
*options*: array
Two options are available: `withscores => TRUE`, and `limit => array($offset, $count)`
##### *Return value*
*Array* containing the values in specified range.
##### *Example*
~~~php
$redis->zAdd('key', 0, 'val0');
$redis->zAdd('key', 2, 'val2');
$redis->zAdd('key', 10, 'val10');
$redis->zRangeByScore('key', 0, 3); /* array('val0', 'val2') */
$redis->zRangeByScore('key', 0, 3, array('withscores' => TRUE)); /* array('val0' => 0, 'val2' => 2) */
$redis->zRangeByScore('key', 0, 3, array('limit' => array(1, 1))); /* array('val2') */
$redis->zRangeByScore('key', 0, 3, array('withscores' => TRUE, 'limit' => array(1, 1))); /* array('val2' => 2) */
~~~
### zRangeByLex
-----
_**Description**_: Returns a lexicographical range of members in a sorted set, assuming the members have the same score. The min and max values are required to start with '(' (exclusive), '[' (inclusive), or be exactly the values '-' (negative inf) or '+' (positive inf). The command must be called with either three *or* five arguments or will return FALSE.
##### *Parameters*
*key*: The ZSET you wish to run against
*min*: The minimum alphanumeric value you wish to get
*max*: The maximum alphanumeric value you wish to get
*offset*: Optional argument if you wish to start somewhere other than the first element.
*limit*: Optional argument if you wish to limit the number of elements returned.
##### *Return value*
*Array* containing the values in the specified range.
##### *Example*
~~~php
foreach(Array('a','b','c','d','e','f','g') as $c)
$redis->zAdd('key',0,$c);
$redis->zRangeByLex('key','-','[c') /* Array('a','b','c'); */
$redis->zRangeByLex('key','-','(c') /* Array('a','b') */
$redis->zRangeByLex('key','-','[c',1,2) /* Array('b','c') */
~~~
### zRank, zRevRank
-----
_**Description**_: Returns the rank of a given member in the specified sorted set, starting at 0 for the item with the smallest score. zRevRank starts at 0 for the item with the *largest* score.
##### *Parameters*
*key*
*member*
##### *Return value*
*Long*, the item's score.
##### *Example*
~~~php
$redis->delete('z');
$redis->zAdd('key', 1, 'one');
$redis->zAdd('key', 2, 'two');
$redis->zRank('key', 'one'); /* 0 */
$redis->zRank('key', 'two'); /* 1 */
$redis->zRevRank('key', 'one'); /* 1 */
$redis->zRevRank('key', 'two'); /* 0 */
~~~
### zRem, zDelete
-----
_**Description**_: Deletes a specified member from the ordered set.
##### *Parameters*
*key*
*member*
##### *Return value*
*LONG* 1 on success, 0 on failure.
##### *Example*
~~~php
$redis->zAdd('key', 0, 'val0');
$redis->zAdd('key', 2, 'val2');
$redis->zAdd('key', 10, 'val10');
$redis->zDelete('key', 'val2');
$redis->zRange('key', 0, -1); /* array('val0', 'val10') */
~~~
### zRemRangeByRank, zDeleteRangeByRank
-----
_**Description**_: Deletes the elements of the sorted set stored at the specified key which have rank in the range [start,end].
##### *Parameters*
*key*
*start*: LONG
*end*: LONG
##### *Return value*
*LONG* The number of values deleted from the sorted set
##### *Example*
~~~php
$redis->zAdd('key', 1, 'one');
$redis->zAdd('key', 2, 'two');
$redis->zAdd('key', 3, 'three');
$redis->zRemRangeByRank('key', 0, 1); /* 2 */
$redis->zRange('key', 0, -1, array('withscores' => TRUE)); /* array('three' => 3) */
~~~
### zRemRangeByScore, zDeleteRangeByScore
-----
_**Description**_: Deletes the elements of the sorted set stored at the specified key which have scores in the range [start,end].
##### *Parameters*
*key*
*start*: double or "+inf" or "-inf" string
*end*: double or "+inf" or "-inf" string
##### *Return value*
*LONG* The number of values deleted from the sorted set
##### *Example*
~~~php
$redis->zAdd('key', 0, 'val0');
$redis->zAdd('key', 2, 'val2');
$redis->zAdd('key', 10, 'val10');
$redis->zRemRangeByScore('key', 0, 3); /* 2 */
~~~
### zRevRange
-----
_**Description**_: Returns the elements of the sorted set stored at the specified key in the range [start, end] in reverse order. start and stop are interpreted as zero-based indices:
`0` the first element, `1` the second ...
`-1` the last element, `-2` the penultimate ...
##### *Parameters*
*key*
*start*: long
*end*: long
*withscores*: bool = false
##### *Return value*
*Array* containing the values in specified range.
##### *Example*
~~~php
$redis->zAdd('key', 0, 'val0');
$redis->zAdd('key', 2, 'val2');
$redis->zAdd('key', 10, 'val10');
$redis->zRevRange('key', 0, -1); /* array('val10', 'val2', 'val0') */
// with scores
$redis->zRevRange('key', 0, -1, true); /* array('val10' => 10, 'val2' => 2, 'val0' => 0) */
~~~
### zScore
-----
_**Description**_: Returns the score of a given member in the specified sorted set.
##### *Parameters*
*key*
*member*
##### *Return value*
*Double*
##### *Example*
~~~php
$redis->zAdd('key', 2.5, 'val2');
$redis->zScore('key', 'val2'); /* 2.5 */
~~~
### zUnion
-----
_**Description**_: Creates an union of sorted sets given in second argument. The result of the union will be stored in the sorted set defined by the first argument.
The third optional argument defines `weights` to apply to the sorted sets in input. In this case, the `weights` will be multiplied by the score of each element in the sorted set before applying the aggregation.
The forth argument defines the `AGGREGATE` option which specify how the results of the union are aggregated.
##### *Parameters*
*keyOutput*
*arrayZSetKeys*
*arrayWeights*
*aggregateFunction* Either "SUM", "MIN", or "MAX": defines the behaviour to use on duplicate entries during the zUnion.
##### *Return value*
*LONG* The number of values in the new sorted set.
##### *Example*
~~~php
$redis->delete('k1');
$redis->delete('k2');
$redis->delete('k3');
$redis->delete('ko1');
$redis->delete('ko2');
$redis->delete('ko3');
$redis->zAdd('k1', 0, 'val0');
$redis->zAdd('k1', 1, 'val1');
$redis->zAdd('k2', 2, 'val2');
$redis->zAdd('k2', 3, 'val3');
$redis->zUnion('ko1', array('k1', 'k2')); /* 4, 'ko1' => array('val0', 'val1', 'val2', 'val3') */
/* Weighted zUnion */
$redis->zUnion('ko2', array('k1', 'k2'), array(1, 1)); /* 4, 'ko2' => array('val0', 'val1', 'val2', 'val3') */
$redis->zUnion('ko3', array('k1', 'k2'), array(5, 1)); /* 4, 'ko3' => array('val0', 'val2', 'val3', 'val1') */
~~~
### zScan
-----
_**Description**_: Scan a sorted set for members, with optional pattern and count
##### *Parameters*
*key*: String, the set to scan
*iterator*: Long (reference), initialized to NULL
*pattern*: String (optional), the pattern to match
*count*: How many keys to return per iteration (Redis might return a different number)
##### *Return value*
*Array, boolean* PHPRedis will return matching keys from Redis, or FALSE when iteration is complete
##### *Example*
~~~php
$it = NULL;
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
while($arr_matches = $redis->zScan('zset', $it, '*pattern*')) {
foreach($arr_matches as $str_mem => $f_score) {
echo "Key: $str_mem, Score: $f_score\n";
}
}
~~~
## Geocoding
### geoAdd
-----
##### *Prototype*
~~~php
$redis->geoAdd($key, $longitude, $latitude, $member [, $longitude, $latitude, $member, ...]);
~~~
_**Description**_: Add one or more geospatial items to the specified key. This function must be called with at least one _longitude, latitude, member_ triplet.
##### *Return value*
*Integer*: The number of elements added to the geospatial key.
##### *Example*
~~~php
$redis->del("myplaces");
/* Since the key will be new, $result will be 2 */
$result = $redis->geoAdd(
"myplaces",
37.773, -122.431, "San Francisco",
-157.858, 21.315, "Honolulu"
);
~~~
### geoHash
-----
##### *Prototype*
~~~php
$redis->geoHash($key, $member [, $member, $member, ...]);
~~~
_**Description**_: Retrieve Geohash strings for one or more elements of a geospatial index.
##### *Return value*
*Array*: One or more Redis Geohash encoded strings.
##### *Example*
~~~php
$redis->geoAdd("hawaii", -157.858, 21.306, "Honolulu", -156.331, 20.798, "Maui");
$hashes = $redis->geoHash("hawaii", "Honolulu", "Maui");
var_dump($hashes);
~~~
##### *Output*
~~~
array(2) {
[0]=>
string(11) "87z9pyek3y0"
[1]=>
string(11) "8e8y6d5jps0"
}
~~~
### geoPos
-----
##### *Prototype*
~~~php
$redis->geoPos($key, $member [, $member, $member, ...]);
~~~
_**Description**_: Return longitude, latitude positions for each requested member.
##### *Return value*
*Array*: One or more longitude/latitude positions
##### *Example*
~~~php
$redis->geoAdd("hawaii", -157.858, 21.306, "Honolulu", -156.331, 20.798, "Maui");
$positions = $redis->geoPos("hawaii", "Honolulu", "Maui");
var_dump($positions);
~~~
##### *Output*
~~~
array(2) {
[0]=>
array(2) {
[0]=>
string(22) "-157.85800248384475708"
[1]=>
string(19) "21.3060004581273077"
}
[1]=>
array(2) {
[0]=>
string(22) "-156.33099943399429321"
[1]=>
string(20) "20.79799924753607598"
}
}
~~~
### GeoDist
-----
##### *Prototype*
~~~php
$redis->geoDist($key, $member1, $member2 [, $unit]);
~~~
_**Description**_: Return the distance between two members in a geospatial set. If units are passed it must be one of the following values:
* 'm' => Meters
* 'km' => Kilometers
* 'mi' => Miles
* 'ft' => Feet
##### *Return value*
*Double*: The distance between the two passed members in the units requested (meters by default).
##### *Example*
~~~php
$redis->geoAdd("hawaii", -157.858, 21.306, "Honolulu", -156.331, 20.798, "Maui");
$meters = $redis->geoDist("hawaii", "Honolulu", "Maui");
$kilometers = $redis->geoDist("hawaii", "Honolulu", "Maui", 'km');
$miles = $redis->geoDist("hawaii", "Honolulu", "Maui", 'mi');
$feet = $redis->geoDist("hawaii", "Honolulu", "Maui", 'ft');
echo "Distance between Honolulu and Maui:\n";
echo " meters : $meters\n";
echo " kilometers: $kilometers\n";
echo " miles : $miles\n";
echo " feet : $feet\n";
/* Bad unit */
$inches = $redis->geoDist("hawaii", "Honolulu", "Maui", 'in');
echo "Invalid unit returned:\n";
var_dump($inches);
~~~
##### *Output*
~~~
Distance between Honolulu and Maui:
meters : 168275.204
kilometers: 168.2752
miles : 104.5616
feet : 552084.0028
Invalid unit returned:
bool(false)
~~~
### geoRadius
-----
##### *Prototype*
~~~php
$redis->geoRadius($key, $longitude, $latitude, $radius, $unit [, Array $options]);
~~~
_**Description**_: Return members of a set with geospatial information that are within the radius specified by the caller.
##### *Options Array*
The georadius command can be called with various options that control how Redis returns results. The following table describes the options phpredis supports. All options are case insensitive.
| Key | Value | Description
| :--- | :--- | :---- |
| COUNT | integer > 0 | Limit how many results are returned
| | WITHCOORD | Return longitude and latitude of matching members
| | WITHDIST | Return the distance from the center
| | WITHHASH | Return the raw geohash-encoded score
| | ASC | Sort results in ascending order
| | DESC | Sort results in descending order
| STORE | _key_ | Store results in _key_
| STOREDIST | _key_ | Store the results as distances in _key_
*Note*: It doesn't make sense to pass both `ASC` and `DESC` options but if both are passed the last one passed will be used.
*Note*: When using `STORE[DIST]` in Redis Cluster, the store key must has to the same slot as the query key or you will get a `CROSSLOT` error.
##### *Return value*
*Mixed*: When no `STORE` option is passed, this function returns an array of results. If it is passed this function returns the number of stored entries.
##### *Example*
~~~php
/* Add some cities */
$redis->geoAdd("hawaii", -157.858, 21.306, "Honolulu", -156.331, 20.798, "Maui");
echo "Within 300 miles of Honolulu:\n";
var_dump($redis->geoRadius("hawaii", -157.858, 21.306, 300, 'mi'));
echo "\nWithin 300 miles of Honolulu with distances:\n";
$options = ['WITHDIST'];
var_dump($redis->geoRadius("hawaii", -157.858, 21.306, 300, 'mi', $options));
echo "\nFirst result within 300 miles of Honolulu with distances:\n";
$options['count'] = 1;
var_dump($redis->geoRadius("hawaii", -157.858, 21.306, 300, 'mi', $options));
echo "\nFirst result within 300 miles of Honolulu with distances in descending sort order:\n";
$options[] = 'DESC';
var_dump($redis->geoRadius("hawaii", -157.858, 21.306, 300, 'mi', $options));
~~~
##### *Output*
~~~
Within 300 miles of Honolulu:
array(2) {
[0]=>
string(8) "Honolulu"
[1]=>
string(4) "Maui"
}
Within 300 miles of Honolulu with distances:
array(2) {
[0]=>
array(2) {
[0]=>
string(8) "Honolulu"
[1]=>
string(6) "0.0002"
}
[1]=>
array(2) {
[0]=>
string(4) "Maui"
[1]=>
string(8) "104.5615"
}
}
First result within 300 miles of Honolulu with distances:
array(1) {
[0]=>
array(2) {
[0]=>
string(8) "Honolulu"
[1]=>
string(6) "0.0002"
}
}
First result within 300 miles of Honolulu with distances in descending sort order:
array(1) {
[0]=>
array(2) {
[0]=>
string(4) "Maui"
[1]=>
string(8) "104.5615"
}
}
~~~
### geoRadiusByMember
##### *Prototype*
~~~php
$redis->geoRadiusByMember($key, $member, $radius, $units [, Array $options]);
~~~
_**Description**_: This method is identical to [geoRadius](#georadius) except that instead of passing a longitude and latitude as the "source" you pass an existing member in the geospatial set.
##### *Options Array*
See [geoRadius](#georadius) command for options array.
##### *Return value*
*Array*: The zero or more entries that are close enough to the member given the distance and radius specified.
##### *Example*
~~~php
$redis->geoAdd("hawaii", -157.858, 21.306, "Honolulu", -156.331, 20.798, "Maui");
echo "Within 300 miles of Honolulu:\n";
var_dump($redis->geoRadiusByMember("hawaii", "Honolulu", 300, 'mi'));
echo "\nFirst match within 300 miles of Honolulu:\n";
var_dump($redis->geoRadiusByMember("hawaii", "Honolulu", 300, 'mi', Array('count' => 1)));
~~~
##### *Output*
~~~
Within 300 miles of Honolulu:
array(2) {
[0]=>
string(8) "Honolulu"
[1]=>
string(4) "Maui"
}
First match within 300 miles of Honolulu:
array(1) {
[0]=>
string(8) "Honolulu"
}
~~~
## Streams
* [xAck](#xack) - Acknowledge one or more pending messages
* [xAdd](#xadd) - Add a message to a stream
* [xClaim](#xclaim) - Acquire ownership of a pending message
* [xDel](#xdel) - Remove a message from a stream
* [xGroup](#xgroup) - Manage consumer groups
* [xInfo](#xinfo) - Get information about a stream
* [xLen](#xlen) - Get the length of a stream
* [xPending](#xpending) - Inspect pending messages in a stream
* [xRange](#xrange) - Query a range of messages from a stream
* [xRead](#xread) - Read message(s) from a stream
* [xReadGroup](#xreadgroup) - Read stream messages with a group and consumer
* [xRevRange](#xrevrange) - Query one or more messages from end to start
* [xTrim](#xtrim) - Trim a stream's size
### xAck
-----
##### *Prototype*
~~~php
$obj_redis->xAck($stream, $group, $arr_messages);
~~~
_**Description**_: Acknowledge one or more messages on behalf of a consumer group.
##### *Return value*
*long*: The number of messages Redis reports as acknowledged.
##### *Example*
~~~php
$obj_redis->xAck('stream', 'group1', ['1530063064286-0', '1530063064286-1']);
~~~
### xAdd
-----
##### *Prototype*
~~~php
$obj_redis->xAdd($str_key, $str_id, $arr_message);
~~~
_**Description**_: Add a message to a stream
##### *Return value*
*String*: The added message ID
##### *Example*
~~~php
$obj_redis->xAdd('mystream', "*", ['field' => 'value']);
~~~
### xClaim
-----
##### *Prototype*
~~~php
$obj_redis->xClaim($str_key, $str_group, $str_consumer, $min_idle_time, $arr_ids, [$arr_options]);
~~~
_**Description**_: Claim ownership of one or more pending messages.
#### *Options Array*
~~~php
$options = [
/* Note: 'TIME', and 'IDLE' are mutually exclusive */
'IDLE' => $value, /* Set the idle time to $value ms */,
'TIME' => $value, /* Set the idle time to now - $value */
'RETRYCOUNT' => $value, /* Update message retrycount to $value */
'FORCE', /* Claim the message(s) even if they're not pending anywhere */
'JUSTID', /* Instruct Redis to only return IDs */
];
~~~
##### *Return value*
*Array*: Either an array of message IDs along with corresponding data, or just an array of IDs (if the 'JUSTID' option was passed).
##### *Example*
~~~php
$ids = ['1530113681011-0', '1530113681011-1', '1530113681011-2'];
/* Without any options */
$obj_redis->xClaim(
'mystream', 'group1', 'myconsumer1', 0, $ids
);
/* With options */
$obj_redis->xClaim(
'mystream', 'group1', 'myconsumer2', 0, $ids,
[
'IDLE' => time() * 1000,
'RETRYCOUNT' => 5,
'FORCE',
'JUSTID'
]
);
~~~
### xDel
-----
##### *Prototype*
~~~php
$obj_redis->xDel($str_key, $arr_ids);
~~~
_**Description**_: Delete one or more messages from a stream.
##### *Return value*
*long*: The number of messages removed
##### *Example*
~~~php
$obj_redis->xDel('mystream', ['1530115304877-0', '1530115305731-0']);
~~~
### xGroup
-----
##### *Prototype*
~~~php
$obj_redis->xGroup('HELP');
$obj_redis->xGroup('CREATE', $str_key, $str_group, $str_msg_id, [$boo_mkstream]);
$obj_redis->xGroup('SETID', $str_key, $str_group, $str_msg_id);
$obj_redis->xGroup('DESTROY', $str_key, $str_group);
$obj_redis->xGroup('DELCONSUMER', $str_key, $str_group, $str_consumer_name);
~~~
_**Description**_: This command is used in order to create, destroy, or manage consumer groups.
##### *Return value*
*Mixed*: This command returns different types depending on the specific XGROUP command executed.
##### *Example*
~~~php
$obj_redis->xGroup('CREATE', 'mystream', 'mygroup');
$obj_redis->xGroup('CREATE', 'mystream', 'mygroup2', true); /* Create stream if non-existent. */
$obj_redis->xGroup('DESTROY', 'mystream', 'mygroup');
~~~
### xInfo
-----
##### *Prototype*
~~~php
$obj_redis->xInfo('CONSUMERS', $str_stream, $str_group);
$obj_redis->xInfo('GROUPS', $str_stream);
$obj_redis->xInfo('STREAM', $str_stream);
$obj_redis->xInfo('HELP');
~~~
_**Description**_: Get information about a stream or consumer groups.
##### *Return value*
*Mixed*: This command returns different types depending on which subcommand is used.
##### *Example*
~~~php
$obj_redis->xInfo('STREAM', 'mystream');
~~~
### xLen
-----
##### *Prototype*
~~~php
$obj_redis->xLen($str_stream);
~~~
_**Description**_: Get the length of a given stream
##### *Return value*
*Long*: The number of messages in the stream.
##### *Example*
~~~php
$obj_redis->xLen('mystream');
~~~
### xPending
-----
##### *Prototype*
~~~php
$obj_redis->xPending($str_stream, $str_group [, $str_start, $str_end, $i_count, $str_consumer]);
~~~
_**Description**_: Get information about pending messages in a given stream.
##### *Return value*
*Array*: Information about the pending messages, in various forms depending on the specific invocation of XPENDING.
##### *Examples*
~~~php
$obj_redis->xPending('mystream', 'mygroup');
$obj_redis->xPending('mystream', 'mygroup', '-', '+', 1, 'consumer-1');
~~~
### xRange
-----
##### *Prototype*
~~~php
$obj_redis->xRange($str_stream, $str_start, $str_end [, $i_count]);
~~~
_**Description**_: Get a range of messages from a given stream.
##### *Return value*
*Array*: The messages in the stream within the requested range.
##### *Example*
~~~php
/* Get everything in this stream */
$obj_redis->xRange('mystream', '-', '+');
/* Only the first two messages */
$obj_redis->xRange('mystream', '-', '+', 2);
~~~
### xRead
-----
##### *Prototype*
~~~php
$obj_redis->xRead($arr_streams [, $i_count, $i_block);
~~~
_**Description**_: Read data from one or more streams and only return IDs greater than sent in the command.
##### *Return value*
*Array*: The messages in the stream newer than the IDs passed to Redis (if any).
##### *Example*
~~~php
$obj_redis->xRead(['stream1' => '1535222584555-0', 'stream2' => '1535222584555-0']);
/* --- Possible output ---
Array
(
[stream1] => Array
(
[1535222584555-1] => Array
(
[key:1] => val:1
)
)
[stream2] => Array
(
[1535222584555-1] => Array
(
[key:1] => val:1
)
)
)
*/
// Receive only new message ($ = last id) and wait for one new message unlimited time
$obj_redis->xRead(['stream1' => '$'], 1, 0);
~~~
### xReadGroup
-----
##### *Prototype*
~~~php
$obj_redis->xReadGroup($str_group, $str_consumer, $arr_streams [, $i_count, $i_block]);
~~~
_**Description**_: This method is similar to xRead except that it supports reading messages for a specific consumer group.
##### *Return value*
*Array*: The messages delivered to this consumer group (if any).
##### *Examples*
~~~php
/* Consume messages for 'mygroup', 'consumer1' */
$obj_redis->xReadGroup('mygroup', 'consumer1', ['s1' => 0, 's2' => 0]);
/* Consume messages for 'mygroup', 'consumer1' which were not consumed yet by the group */
$obj_redis->xReadGroup('mygroup', 'consumer1', ['s1' => '>', 's2' => '>']);
/* Read a single message as 'consumer2' wait for up to a second until a message arrives. */
$obj_redis->xReadGroup('mygroup', 'consumer2', ['s1' => 0, 's2' => 0], 1, 1000);
~~~
### xRevRange
-----
##### *Prototype*
~~~php
$obj_redis->xRevRange($str_stream, $str_end, $str_start [, $i_count]);
~~~
_**Description**_: This is identical to xRange except the results come back in reverse order. Also note that Redis reverses the order of "start" and "end".
##### *Return value*
*Array*: The messages in the range specified.
##### *Example*
~~~php
$obj_redis->xRevRange('mystream', '+', '-');
~~~
### xTrim
-----
##### *Prototype*
~~~php
$obj_redis->xTrim($str_stream, $i_max_len [, $boo_approximate]);
~~~
_**Description**_: Trim the stream length to a given maximum. If the "approximate" flag is pasesed, Redis will use your size as a hint but only trim trees in whole nodes (this is more efficient).
##### *Return value*
*long*: The number of messages trimed from the stream.
##### *Example*
~~~php
/* Trim to exactly 100 messages */
$obj_redis->xTrim('mystream', 100);
/* Let Redis approximate the trimming */
$obj_redis->xTrim('mystream', 100, true);
~~~
## Pub/sub
* [pSubscribe](#psubscribe) - Subscribe to channels by pattern
* [publish](#publish) - Post a message to a channel
* [subscribe](#subscribe) - Subscribe to channels
* [pubSub](#pubsub) - Introspection into the pub/sub subsystem
### pSubscribe
-----
_**Description**_: Subscribe to channels by pattern
##### *Parameters*
*patterns*: An array of patterns to match
*callback*: Either a string or an array with an object and method. The callback will get four arguments ($redis, $pattern, $channel, $message)
*return value*: Mixed. Any non-null return value in the callback will be returned to the caller.
##### *Example*
~~~php
function pSubscribe($redis, $pattern, $chan, $msg) {
echo "Pattern: $pattern\n";
echo "Channel: $chan\n";
echo "Payload: $msg\n";
}
~~~
### publish
-----
_**Description**_: Publish messages to channels. Warning: this function will probably change in the future.
##### *Parameters*
*channel*: a channel to publish to
*message*: string
##### *Example*
~~~php
$redis->publish('chan-1', 'hello, world!'); // send message.
~~~
### subscribe
-----
_**Description**_: Subscribe to channels. Warning: this function will probably change in the future.
##### *Parameters*
*channels*: an array of channels to subscribe to
*callback*: either a string or an array($instance, 'method_name'). The callback function receives 3 parameters: the redis instance, the channel name, and the message.
*return value*: Mixed. Any non-null return value in the callback will be returned to the caller.
##### *Example*
~~~php
function f($redis, $chan, $msg) {
switch($chan) {
case 'chan-1':
...
break;
case 'chan-2':
...
break;
case 'chan-2':
...
break;
}
}
$redis->subscribe(array('chan-1', 'chan-2', 'chan-3'), 'f'); // subscribe to 3 chans
~~~
### pubSub
-----
_**Description**_: A command allowing you to get information on the Redis pub/sub system.
##### *Parameters*
*keyword*: String, which can be: "channels", "numsub", or "numpat"
*argument*: Optional, variant. For the "channels" subcommand, you can pass a string pattern. For "numsub" an array of channel names.
##### *Return value*
*CHANNELS*: Returns an array where the members are the matching channels.
*NUMSUB*: Returns a key/value array where the keys are channel names and values are their counts.
*NUMPAT*: Integer return containing the number active pattern subscriptions
##### *Example*
~~~php
$redis->pubSub("channels"); /*All channels */
$redis->pubSub("channels", "*pattern*"); /* Just channels matching your pattern */
$redis->pubSub("numsub", Array("chan1", "chan2")); /*Get subscriber counts for 'chan1' and 'chan2'*/
$redis->pubSub("numpat"); /* Get the number of pattern subscribers */
~~~
## Generic
1. [rawCommand](#rawcommand) - Execute any generic command against the server.
### rawCommand
-----
_**Description**_: A method to execute any arbitrary command against the a Redis server
##### *Parameters*
This method is variadic and takes a dynamic number of arguments of various types (string, long, double), but must be passed at least one argument (the command keyword itself).
##### *Return value*
The return value can be various types depending on what the server itself returns. No post processing is done to the returned value and must be handled by the client code.
##### *Example*
```php
/* Returns: true */
$redis->rawCommand("set", "foo", "bar");
/* Returns: "bar" */
$redis->rawCommand("get", "foo");
/* Returns: 3 */
$redis->rawCommand("rpush", "mylist", "one", 2, 3.5));
/* Returns: ["one", "2", "3.5000000000000000"] */
$redis->rawCommand("lrange", "mylist", 0, -1);
```
## Transactions
1. [multi, exec, discard](#multi-exec-discard) - Enter and exit transactional mode
2. [watch, unwatch](#watch-unwatch) - Watches a key for modifications by another client.
### multi, exec, discard.
-----
_**Description**_: Enter and exit transactional mode.
##### *Parameters*
(optional) `Redis::MULTI` or `Redis::PIPELINE`. Defaults to `Redis::MULTI`. A `Redis::MULTI` block of commands runs as a single transaction; a `Redis::PIPELINE` block is simply transmitted faster to the server, but without any guarantee of atomicity. `discard` cancels a transaction.
##### *Return value*
`multi()` returns the Redis instance and enters multi-mode. Once in multi-mode, all subsequent method calls return the same object until `exec()` is called.
##### *Example*
~~~php
$ret = $redis->multi()
->set('key1', 'val1')
->get('key1')
->set('key2', 'val2')
->get('key2')
->exec();
/*
$ret == array(
0 => TRUE,
1 => 'val1',
2 => TRUE,
3 => 'val2');
*/
~~~
### watch, unwatch
-----
_**Description**_: Watches a key for modifications by another client.
If the key is modified between `WATCH` and `EXEC`, the MULTI/EXEC transaction will fail (return `FALSE`). `unwatch` cancels all the watching of all keys by this client.
##### *Parameters*
*keys*: string for one key or array for a list of keys
##### *Example*
~~~php
$redis->watch('x'); // or for a list of keys: $redis->watch(array('x','another key'));
/* long code here during the execution of which other clients could well modify `x` */
$ret = $redis->multi()
->incr('x')
->exec();
/*
$ret = FALSE if x has been modified between the call to WATCH and the call to EXEC.
*/
~~~
## Scripting
* [eval](#eval) - Evaluate a LUA script serverside
* [evalSha](#evalsha) - Evaluate a LUA script serverside, from the SHA1 hash of the script instead of the script itself
* [script](#script) - Execute the Redis SCRIPT command to perform various operations on the scripting subsystem
* [getLastError](#getlasterror) - The last error message (if any)
* [clearLastError](#clearlasterror) - Clear the last error message
* [_prefix](#_prefix) - A utility method to prefix the value with the prefix setting for phpredis
* [_unserialize](#_unserialize) - A utility method to unserialize data with whatever serializer is set up
* [_serialize](#_serialize) - A utility method to serialize data with whatever serializer is set up
### eval
-----
_**Description**_: Evaluate a LUA script serverside
##### *Parameters*
*script* string.
*args* array, optional.
*num_keys* int, optional.
##### *Return value*
Mixed. What is returned depends on what the LUA script itself returns, which could be a scalar value (int/string), or an array.
Arrays that are returned can also contain other arrays, if that's how it was set up in your LUA script. If there is an error
executing the LUA script, the getLastError() function can tell you the message that came back from Redis (e.g. compile error).
##### *Examples*
~~~php
$redis->eval("return 1"); // Returns an integer: 1
$redis->eval("return {1,2,3}"); // Returns Array(1,2,3)
$redis->del('mylist');
$redis->rpush('mylist','a');
$redis->rpush('mylist','b');
$redis->rpush('mylist','c');
// Nested response: Array(1,2,3,Array('a','b','c'));
$redis->eval("return {1,2,3,redis.call('lrange','mylist',0,-1)}");
~~~
### evalSha
-----
_**Description**_: Evaluate a LUA script serverside, from the SHA1 hash of the script instead of the script itself.
In order to run this command Redis will have to have already loaded the script,
either by running it or via the SCRIPT LOAD command.
##### *Parameters*
*script_sha* string. The sha1 encoded hash of the script you want to run.
*args* array, optional. Arguments to pass to the LUA script.
*num_keys* int, optional. The number of arguments that should go into the KEYS array, vs. the ARGV array when Redis spins the script
##### *Return value*
Mixed. See EVAL
##### *Examples*
~~~php
$script = 'return 1';
$sha = $redis->script('load', $script);
$redis->evalSha($sha); // Returns 1
~~~
### script
-----
_**Description**_: Execute the Redis SCRIPT command to perform various operations on the scripting subsystem.
##### *Usage*
~~~php
$redis->script('load', $script);
$redis->script('flush');
$redis->script('kill');
$redis->script('exists', $script1, [$script2, $script3, ...]);
~~~
##### *Return value*
* SCRIPT LOAD will return the SHA1 hash of the passed script on success, and FALSE on failure.
* SCRIPT FLUSH should always return TRUE
* SCRIPT KILL will return true if a script was able to be killed and false if not
* SCRIPT EXISTS will return an array with TRUE or FALSE for each passed script
### client
-----
_**Description**_: Issue the CLIENT command with various arguments.
The Redis CLIENT command can be used in four ways.
* CLIENT LIST
* CLIENT GETNAME
* CLIENT SETNAME [name]
* CLIENT KILL [ip:port]
##### *Usage*
~~~php
$redis->client('list'); // Get a list of clients
$redis->client('getname'); // Get the name of the current connection
$redis->client('setname', 'somename'); // Set the name of the current connection
$redis->client('kill', <ip:port>); // Kill the process at ip:port
~~~
##### *Return value*
This will vary depending on which client command was executed.
* CLIENT LIST will return an array of arrays with client information.
* CLIENT GETNAME will return the client name or false if none has been set
* CLIENT SETNAME will return true if it can be set and false if not
* CLIENT KILL will return true if the client can be killed, and false if not
Note: phpredis will attempt to reconnect so you can actually kill your own connection
but may not notice losing it!
### getLastError
-----
_**Description**_: The last error message (if any)
##### *Parameters*
*none*
##### *Return value*
A string with the last returned script based error message, or NULL if there is no error
##### *Examples*
~~~php
$redis->eval('this-is-not-lua');
$err = $redis->getLastError();
// "ERR Error compiling script (new function): user_script:1: '=' expected near '-'"
~~~
### clearLastError
-----
_**Description**_: Clear the last error message
##### *Parameters*
*none*
##### *Return value*
*BOOL* TRUE
##### *Examples*
~~~php
$redis->set('x', 'a');
$redis->incr('x');
$err = $redis->getLastError();
// "ERR value is not an integer or out of range"
$redis->clearLastError();
$err = $redis->getLastError();
// NULL
~~~
### _prefix
-----
_**Description**_: A utility method to prefix the value with the prefix setting for phpredis.
##### *Parameters*
*value* string. The value you wish to prefix
##### *Return value*
If a prefix is set up, the value now prefixed. If there is no prefix, the value will be returned unchanged.
##### *Examples*
~~~php
$redis->setOption(Redis::OPT_PREFIX, 'my-prefix:');
$redis->_prefix('my-value'); // Will return 'my-prefix:my-value'
~~~
### _serialize
-----
_**Description**_: A utility method to serialize values manually.
This method allows you to serialize a value with whatever serializer is configured, manually.
This can be useful for serialization/unserialization of data going in and out of EVAL commands
as phpredis can't automatically do this itself. Note that if no serializer is set, phpredis
will change Array values to 'Array', and Objects to 'Object'.
##### *Parameters*
*value*: Mixed. The value to be serialized
##### *Examples*
~~~php
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE);
$redis->_serialize("foo"); // returns "foo"
$redis->_serialize(Array()); // Returns "Array"
$redis->_serialize(new stdClass()); // Returns "Object"
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
$redis->_serialize("foo"); // Returns 's:3:"foo";'
~~~
### _unserialize
-----
_**Description**_: A utility method to unserialize data with whatever serializer is set up.
If there is no serializer set, the value will be returned unchanged. If there is a serializer set up,
and the data passed in is malformed, an exception will be thrown. This can be useful if phpredis is
serializing values, and you return something from redis in a LUA script that is serialized.
##### *Parameters*
*value* string. The value to be unserialized
##### *Examples*
~~~php
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
$redis->_unserialize('a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}'); // Will return Array(1,2,3)
~~~
## Introspection
### isConnected
-----
_**Description**_: A method to determine if a phpredis object thinks it's connected to a server
##### *Parameters*
None
##### *Return value*
*Boolean* Returns TRUE if phpredis thinks it's connected and FALSE if not
### getHost
-----
_**Description**_: Retrieve our host or unix socket that we're connected to
##### *Parameters*
None
##### *Return value*
*Mixed* The host or unix socket we're connected to or FALSE if we're not connected
### getPort
-----
_**Description**_: Get the port we're connected to
##### *Parameters*
None
##### *Return value*
*Mixed* Returns the port we're connected to or FALSE if we're not connected
### getDbNum
-----
_**Description**_: Get the database number phpredis is pointed to
##### *Parameters*
None
##### *Return value*
*Mixed* Returns the database number (LONG) phpredis thinks it's pointing to or FALSE if we're not connected
### getTimeout
-----
_**Description**_: Get the (write) timeout in use for phpredis
##### *Parameters*
None
##### *Return value*
*Mixed* The timeout (DOUBLE) specified in our connect call or FALSE if we're not connected
### getReadTimeout
_**Description**_: Get the read timeout specified to phpredis or FALSE if we're not connected
##### *Parameters*
None
##### *Return value*
*Mixed* Returns the read timeout (which can be set using setOption and Redis::OPT_READ_TIMEOUT) or FALSE if we're not connected
### getPersistentID
-----
_**Description**_: Gets the persistent ID that phpredis is using
##### *Parameters*
None
##### *Return value*
*Mixed* Returns the persistent id phpredis is using (which will only be set if connected with pconnect), NULL if we're not
using a persistent ID, and FALSE if we're not connected
### getAuth
-----
_**Description**_: Get the password used to authenticate the phpredis connection
### *Parameters*
None
### *Return value*
*Mixed* Returns the password used to authenticate a phpredis session or NULL if none was used, and FALSE if we're not connected
|