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
|
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2007 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.0 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_0.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Antony Dovgal <tony2001@phpclub.net> |
| Mikael Johansson <mikael AT synd DOT info> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/standard/php_string.h"
#include "php_memcache.h"
/* True global resources - no need for thread safety here */
static int le_memcache_pool, le_memcache_server;
static zend_class_entry *memcache_pool_ce;
static zend_class_entry *memcache_ce;
ZEND_EXTERN_MODULE_GLOBALS(memcache)
/* {{{ memcache_functions[]
*/
ZEND_BEGIN_ARG_INFO(arginfo_memcache_get, 1)
ZEND_ARG_PASS_INFO(0)
ZEND_ARG_PASS_INFO(0)
ZEND_ARG_PASS_INFO(1)
ZEND_ARG_PASS_INFO(1)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_memcache_object_get, 1)
ZEND_ARG_PASS_INFO(0)
ZEND_ARG_PASS_INFO(1)
ZEND_ARG_PASS_INFO(1)
ZEND_END_ARG_INFO()
zend_function_entry memcache_functions[] = {
PHP_FE(memcache_connect, NULL)
PHP_FE(memcache_pconnect, NULL)
PHP_FE(memcache_add_server, NULL)
PHP_FE(memcache_set_server_params, NULL)
PHP_FE(memcache_set_failure_callback, NULL)
PHP_FE(memcache_get_server_status, NULL)
PHP_FE(memcache_get_version, NULL)
PHP_FE(memcache_add, NULL)
PHP_FE(memcache_set, NULL)
PHP_FE(memcache_replace, NULL)
PHP_FE(memcache_cas, NULL)
PHP_FE(memcache_append, NULL)
PHP_FE(memcache_prepend, NULL)
PHP_FE(memcache_get, arginfo_memcache_get)
PHP_FE(memcache_delete, NULL)
PHP_FE(memcache_debug, NULL)
PHP_FE(memcache_get_stats, NULL)
PHP_FE(memcache_get_extended_stats, NULL)
PHP_FE(memcache_set_compress_threshold, NULL)
PHP_FE(memcache_increment, NULL)
PHP_FE(memcache_decrement, NULL)
PHP_FE(memcache_close, NULL)
PHP_FE(memcache_flush, NULL)
PHP_FE(memcache_set_sasl_auth_data, NULL)
{NULL, NULL, NULL}
};
static zend_function_entry php_memcache_pool_class_functions[] = {
PHP_NAMED_FE(connect, zif_memcache_pool_connect, NULL)
PHP_NAMED_FE(addserver, zif_memcache_pool_addserver, NULL)
PHP_FALIAS(setserverparams, memcache_set_server_params, NULL)
PHP_FALIAS(setfailurecallback, memcache_set_failure_callback, NULL)
PHP_FALIAS(getserverstatus, memcache_get_server_status, NULL)
PHP_NAMED_FE(findserver, zif_memcache_pool_findserver, NULL)
PHP_FALIAS(getversion, memcache_get_version, NULL)
PHP_FALIAS(add, memcache_add, NULL)
PHP_FALIAS(set, memcache_set, NULL)
PHP_FALIAS(replace, memcache_replace, NULL)
PHP_FALIAS(cas, memcache_cas, NULL)
PHP_FALIAS(append, memcache_append, NULL)
PHP_FALIAS(prepend, memcache_prepend, NULL)
PHP_FALIAS(get, memcache_get, arginfo_memcache_object_get)
PHP_FALIAS(delete, memcache_delete, NULL)
PHP_FALIAS(getstats, memcache_get_stats, NULL)
PHP_FALIAS(getextendedstats, memcache_get_extended_stats, NULL)
PHP_FALIAS(setcompressthreshold, memcache_set_compress_threshold, NULL)
PHP_FALIAS(increment, memcache_increment, NULL)
PHP_FALIAS(decrement, memcache_decrement, NULL)
PHP_FALIAS(close, memcache_close, NULL)
PHP_FALIAS(flush, memcache_flush, NULL)
PHP_FALIAS(setSaslAuthData, memcache_set_sasl_auth_data, NULL)
{NULL, NULL, NULL}
};
static zend_function_entry php_memcache_class_functions[] = {
PHP_FALIAS(connect, memcache_connect, NULL)
PHP_FALIAS(pconnect, memcache_pconnect, NULL)
PHP_FALIAS(addserver, memcache_add_server, NULL)
{NULL, NULL, NULL}
};
/* }}} */
/* {{{ memcache_module_entry
*/
zend_module_entry memcache_module_entry = {
STANDARD_MODULE_HEADER,
"memcache",
memcache_functions,
PHP_MINIT(memcache),
PHP_MSHUTDOWN(memcache),
NULL,
NULL,
PHP_MINFO(memcache),
PHP_MEMCACHE_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_MEMCACHE
ZEND_GET_MODULE(memcache)
#endif
static PHP_INI_MH(OnUpdateChunkSize) /* {{{ */
{
zend_long val;
char *endptr = NULL;
val = ZEND_STRTOL(ZSTR_VAL(new_value), &endptr, 10);
if (!endptr || (*endptr != '\0') || val <= 0) {
php_error_docref(NULL, E_WARNING, "memcache.chunk_size must be a positive integer ('%s' given)", ZSTR_VAL(new_value));
return FAILURE;
}
return OnUpdateLong(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
}
/* }}} */
static PHP_INI_MH(OnUpdateFailoverAttempts) /* {{{ */
{
zend_long val;
char *endptr = NULL;
val = ZEND_STRTOL(ZSTR_VAL(new_value), &endptr, 10);
if (!endptr || (*endptr != '\0') || val <= 0) {
php_error_docref(NULL, E_WARNING, "memcache.max_failover_attempts must be a positive integer ('%s' given)", ZSTR_VAL(new_value));
return FAILURE;
}
return OnUpdateLong(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
}
/* }}} */
static PHP_INI_MH(OnUpdateProtocol) /* {{{ */
{
if (!strcasecmp(ZSTR_VAL(new_value), "ascii")) {
MEMCACHE_G(protocol) = MMC_ASCII_PROTOCOL;
}
else if (!strcasecmp(ZSTR_VAL(new_value), "binary")) {
MEMCACHE_G(protocol) = MMC_BINARY_PROTOCOL;
}
else {
php_error_docref(NULL, E_WARNING, "memcache.protocol must be in set {ascii, binary} ('%s' given)", ZSTR_VAL(new_value));
return FAILURE;
}
return SUCCESS;
}
/* }}} */
static PHP_INI_MH(OnUpdateHashStrategy) /* {{{ */
{
if (!strcasecmp(ZSTR_VAL(new_value), "standard")) {
MEMCACHE_G(hash_strategy) = MMC_STANDARD_HASH;
}
else if (!strcasecmp(ZSTR_VAL(new_value), "consistent")) {
MEMCACHE_G(hash_strategy) = MMC_CONSISTENT_HASH;
}
else {
php_error_docref(NULL, E_WARNING, "memcache.hash_strategy must be in set {standard, consistent} ('%s' given)", ZSTR_VAL(new_value));
return FAILURE;
}
return SUCCESS;
}
/* }}} */
static PHP_INI_MH(OnUpdateHashFunction) /* {{{ */
{
if (!strcasecmp(ZSTR_VAL(new_value), "crc32")) {
MEMCACHE_G(hash_function) = MMC_HASH_CRC32;
}
else if (!strcasecmp(ZSTR_VAL(new_value), "fnv")) {
MEMCACHE_G(hash_function) = MMC_HASH_FNV1A;
}
else {
php_error_docref(NULL, E_WARNING, "memcache.hash_function must be in set {crc32, fnv} ('%s' given)", ZSTR_VAL(new_value));
return FAILURE;
}
return SUCCESS;
}
/* }}} */
static PHP_INI_MH(OnUpdateRedundancy) /* {{{ */
{
zend_long val;
char *endptr = NULL;
val = ZEND_STRTOL(ZSTR_VAL(new_value), &endptr, 10);
if (!endptr || (*endptr != '\0') || val <= 0) {
php_error_docref(NULL, E_WARNING, "memcache.redundancy must be a positive integer ('%s' given)", ZSTR_VAL(new_value));
return FAILURE;
}
return OnUpdateLong(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
}
/* }}} */
static PHP_INI_MH(OnUpdateCompressThreshold) /* {{{ */
{
zend_long val;
char *endptr = NULL;
val = ZEND_STRTOL(ZSTR_VAL(new_value), &endptr, 10);
if (!endptr || (*endptr != '\0') || val < 0) {
php_error_docref(NULL, E_WARNING, "memcache.compress_threshold must be a positive integer ('%s' given)", ZSTR_VAL(new_value));
return FAILURE;
}
return OnUpdateLong(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
}
/* }}} */
static PHP_INI_MH(OnUpdateLockTimeout) /* {{{ */
{
zend_long val;
char *endptr = NULL;
val = ZEND_STRTOL(ZSTR_VAL(new_value), &endptr, 10);
if (!endptr || (*endptr != '\0') || val <= 0) {
php_error_docref(NULL, E_WARNING, "memcache.lock_timeout must be a positive integer ('%s' given)", ZSTR_VAL(new_value));
return FAILURE;
}
return OnUpdateLong(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
}
/* }}} */
/* {{{ PHP_INI */
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("memcache.allow_failover", "1", PHP_INI_ALL, OnUpdateLong, allow_failover, zend_memcache_globals, memcache_globals)
STD_PHP_INI_ENTRY("memcache.max_failover_attempts", "20", PHP_INI_ALL, OnUpdateFailoverAttempts, max_failover_attempts, zend_memcache_globals, memcache_globals)
STD_PHP_INI_ENTRY("memcache.default_port", "11211", PHP_INI_ALL, OnUpdateLong, default_port, zend_memcache_globals, memcache_globals)
STD_PHP_INI_ENTRY("memcache.chunk_size", "32768", PHP_INI_ALL, OnUpdateChunkSize, chunk_size, zend_memcache_globals, memcache_globals)
STD_PHP_INI_ENTRY("memcache.protocol", "ascii", PHP_INI_ALL, OnUpdateProtocol, protocol, zend_memcache_globals, memcache_globals)
STD_PHP_INI_ENTRY("memcache.hash_strategy", "consistent", PHP_INI_ALL, OnUpdateHashStrategy, hash_strategy, zend_memcache_globals, memcache_globals)
STD_PHP_INI_ENTRY("memcache.hash_function", "crc32", PHP_INI_ALL, OnUpdateHashFunction, hash_function, zend_memcache_globals, memcache_globals)
STD_PHP_INI_ENTRY("memcache.redundancy", "1", PHP_INI_ALL, OnUpdateRedundancy, redundancy, zend_memcache_globals, memcache_globals)
STD_PHP_INI_ENTRY("memcache.session_redundancy", "2", PHP_INI_ALL, OnUpdateRedundancy, session_redundancy, zend_memcache_globals, memcache_globals)
STD_PHP_INI_ENTRY("memcache.compress_threshold", "20000", PHP_INI_ALL, OnUpdateCompressThreshold, compress_threshold, zend_memcache_globals, memcache_globals)
STD_PHP_INI_ENTRY("memcache.lock_timeout", "15", PHP_INI_ALL, OnUpdateLockTimeout, lock_timeout, zend_memcache_globals, memcache_globals)
PHP_INI_END()
/* }}} */
/* {{{ internal function protos */
static void _mmc_pool_list_dtor(zend_resource*);
static void _mmc_server_list_dtor(zend_resource*);
static void php_mmc_set_failure_callback(mmc_pool_t *, zval *, zval *);
static void php_mmc_failure_callback(mmc_pool_t *, mmc_t *, zval *);
/* }}} */
/* {{{ php_memcache_init_globals()
*/
static void php_memcache_init_globals(zend_memcache_globals *memcache_globals_p)
{
MEMCACHE_G(hash_strategy) = MMC_STANDARD_HASH;
MEMCACHE_G(hash_function) = MMC_HASH_CRC32;
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(memcache)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "MemcachePool", php_memcache_pool_class_functions);
memcache_pool_ce = zend_register_internal_class(&ce);
INIT_CLASS_ENTRY(ce, "Memcache", php_memcache_class_functions);
memcache_ce = zend_register_internal_class_ex(&ce, memcache_pool_ce);
le_memcache_pool = zend_register_list_destructors_ex(_mmc_pool_list_dtor, NULL, "memcache connection", module_number);
le_memcache_server = zend_register_list_destructors_ex(NULL, _mmc_server_list_dtor, "persistent memcache connection", module_number);
#ifdef ZTS
ts_allocate_id(&memcache_globals_id, sizeof(zend_memcache_globals), (ts_allocate_ctor) php_memcache_init_globals, NULL);
#else
php_memcache_init_globals(&memcache_globals);
#endif
REGISTER_LONG_CONSTANT("MEMCACHE_COMPRESSED", MMC_COMPRESSED, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MEMCACHE_USER1", MMC_RESERVED_APPLICATIONDEFINEDFLAG_12, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MEMCACHE_USER2", MMC_RESERVED_APPLICATIONDEFINEDFLAG_13, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MEMCACHE_USER3", MMC_RESERVED_APPLICATIONDEFINEDFLAG_14, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MEMCACHE_USER4", MMC_RESERVED_APPLICATIONDEFINEDFLAG_15, CONST_CS | CONST_PERSISTENT);
REGISTER_INI_ENTRIES();
#if HAVE_MEMCACHE_SESSION
REGISTER_LONG_CONSTANT("MEMCACHE_HAVE_SESSION", 1, CONST_CS | CONST_PERSISTENT);
php_session_register_module(ps_memcache_ptr);
#else
REGISTER_LONG_CONSTANT("MEMCACHE_HAVE_SESSION", 0, CONST_CS | CONST_PERSISTENT);
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(memcache)
{
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(memcache)
{
php_info_print_table_start();
php_info_print_table_header(2, "memcache support", "enabled");
php_info_print_table_row(2, "Version", PHP_MEMCACHE_VERSION);
php_info_print_table_row(2, "Revision", "$Revision$");
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* ------------------
internal functions
------------------ */
static void _mmc_pool_list_dtor(zend_resource *rsrc) /* {{{ */
{
mmc_pool_t *pool = (mmc_pool_t *)rsrc->ptr;
if (!Z_ISUNDEF(pool->failure_callback_param)) {
Z_DELREF(pool->failure_callback_param);
ZVAL_UNDEF(&pool->failure_callback_param);
}
mmc_pool_free(pool);
}
/* }}} */
static void _mmc_server_list_dtor(zend_resource *rsrc) /* {{{ */
{
mmc_server_free((mmc_t *)rsrc->ptr);
}
/* }}} */
static int mmc_get_pool(zval *id, mmc_pool_t **pool) /* {{{ */
{
zval *zv;
if (Z_TYPE_P(id) != IS_OBJECT || (zv = zend_hash_str_find(Z_OBJPROP_P(id), "connection", sizeof("connection")-1)) == NULL) {
php_error_docref(NULL, E_WARNING, "No servers added to memcache connection");
return 0;
}
if (Z_TYPE_P(zv) != IS_RESOURCE || (*pool = zend_fetch_resource_ex(zv, "connection", le_memcache_pool)) == NULL) {
php_error_docref(NULL, E_WARNING, "Invalid MemcachePool->connection member variable");
return 0;
}
return 1;
}
/* }}} */
int mmc_stored_handler(mmc_t *mmc, mmc_request_t *request, int response, const char *message, unsigned int message_len, void *param) /*
handles SET/ADD/REPLACE response, param is a zval pointer to store result into {{{ */
{
zval *result = (zval *)param;
if (response == MMC_OK) {
if (Z_TYPE_P(result) == IS_NULL) {
ZVAL_TRUE(result);
}
return MMC_REQUEST_DONE;
}
/* return FALSE or catch memory errors without failover */
if (response == MMC_RESPONSE_EXISTS || response == MMC_RESPONSE_OUT_OF_MEMORY || response == MMC_RESPONSE_TOO_LARGE
|| response == MMC_RESPONSE_CLIENT_ERROR) {
ZVAL_FALSE(result);
if (response != MMC_RESPONSE_EXISTS) {
/* trigger notice but no need for failover */
php_error_docref(NULL, E_NOTICE, "Server %s (tcp %d, udp %d) failed with: %s (%d)",
mmc->host, mmc->tcp.port, mmc->udp.port, message, response);
}
return MMC_REQUEST_DONE;
}
return mmc_request_failure(mmc, request->io, message, message_len, 0);
}
/* }}} */
static void php_mmc_store(INTERNAL_FUNCTION_PARAMETERS, int op) /* {{{ */
{
mmc_pool_t *pool;
mmc_request_t *request;
zval *keys, *value = 0, *mmc_object = getThis();
zend_long flags = 0, exptime = 0, cas = 0;
if (mmc_object == NULL) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz|zlll", &mmc_object, memcache_pool_ce, &keys, &value, &flags, &exptime, &cas) == FAILURE) {
return;
}
}
else {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|zlll", &keys, &value, &flags, &exptime, &cas) == FAILURE) {
return;
}
}
if (!mmc_get_pool(mmc_object, &pool) || !pool->num_servers) {
RETURN_FALSE;
}
RETVAL_NULL();
if (Z_TYPE_P(keys) == IS_ARRAY) {
zend_string *key;
zval *val;
zend_ulong index;
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(keys), index, key, val ) {
zend_string *str_key = NULL;
if (key == NULL) {
str_key = strpprintf(0, ZEND_ULONG_FMT, index);
} else {
str_key = key;
}
/* allocate request */
request = mmc_pool_request(pool, MMC_PROTO_TCP,
mmc_stored_handler, return_value, mmc_pool_failover_handler, NULL);
if (mmc_prepare_key_ex(ZSTR_VAL(str_key), ZSTR_LEN(str_key), request->key, &(request->key_len)) != MMC_OK) {
php_error_docref(NULL, E_WARNING, "Invalid key");
mmc_pool_release(pool, request);
if (key == NULL) {
zend_string_release(str_key);
}
continue;
}
if (key == NULL) {
zend_string_release(str_key);
}
/* assemble command */
if (pool->protocol->store(pool, request, op, request->key, request->key_len, flags, exptime, cas, val) != MMC_OK) {
mmc_pool_release(pool, request);
continue;
}
/* schedule request */
if (mmc_pool_schedule_key(pool, request->key, request->key_len, request, MEMCACHE_G(redundancy)) != MMC_OK) {
continue;
}
/* begin sending requests immediatly */
mmc_pool_select(pool);
} ZEND_HASH_FOREACH_END();
}
else if (value) {
/* allocate request */
request = mmc_pool_request(pool, MMC_PROTO_TCP, mmc_stored_handler, return_value, mmc_pool_failover_handler, NULL);
if (mmc_prepare_key(keys, request->key, &(request->key_len)) != MMC_OK) {
php_error_docref(NULL, E_WARNING, "Invalid key");
mmc_pool_release(pool, request);
RETURN_FALSE;
}
/* assemble command */
if (pool->protocol->store(pool, request, op, request->key, request->key_len, flags, exptime, cas, value) != MMC_OK) {
mmc_pool_release(pool, request);
RETURN_FALSE;
}
/* schedule request */
if (mmc_pool_schedule_key(pool, request->key, request->key_len, request, MEMCACHE_G(redundancy)) != MMC_OK) {
RETURN_FALSE;
}
}
else {
WRONG_PARAM_COUNT;
}
/* execute all requests */
mmc_pool_run(pool);
if (Z_TYPE_P(return_value) == IS_NULL) {
RETVAL_FALSE;
}
}
/* }}} */
int mmc_numeric_response_handler(mmc_t *mmc, mmc_request_t *request, int response, const char *message, unsigned int message_len, void *param) /*
handles a mutate response line, param is a zval pointer to store result into {{{ */
{
zval *result = (zval *)param;
if (response == MMC_OK) {
if (Z_TYPE_P(result) == IS_ARRAY) {
add_assoc_bool_ex(result, request->key, request->key_len + 1, 1);
}
else if (Z_TYPE_P(result) == IS_NULL) {
/* switch only from null to true, not from false to true */
ZVAL_TRUE(result);
}
return MMC_REQUEST_DONE;
}
if (response == MMC_RESPONSE_NOT_FOUND || response == MMC_RESPONSE_CLIENT_ERROR) {
if (Z_TYPE_P(result) == IS_ARRAY) {
add_assoc_bool_ex(result, request->key, request->key_len + 1, 0);
}
else {
ZVAL_FALSE(result);
}
if (response != MMC_RESPONSE_NOT_FOUND) {
php_error_docref(NULL,
E_NOTICE, "Server %s (tcp %d, udp %d) failed with: %s (%d)",
mmc->host, mmc->tcp.port,
mmc->udp.port, message, response);
}
return MMC_REQUEST_DONE;
}
return mmc_request_failure(mmc, request->io, message, message_len, 0);
}
/* }}} */
static void php_mmc_numeric(INTERNAL_FUNCTION_PARAMETERS, int deleted, int invert) /*
sends one or several commands which have a single optional numeric parameter (incr, decr, delete) {{{ */
{
mmc_pool_t *pool;
zval *mmc_object = getThis();
zval *keys;
zend_long value = 1, defval = 0, exptime = 0;
mmc_request_t *request;
void *value_handler_param[3];
int defval_used = 0;
if (mmc_object == NULL) {
if (deleted) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz|l", &mmc_object, memcache_pool_ce, &keys, &value) == FAILURE) {
return;
}
}
else {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz|lll", &mmc_object, memcache_pool_ce, &keys, &value, &defval, &exptime) == FAILURE) {
return;
}
defval_used = ZEND_NUM_ARGS() >= 4;
}
}
else {
if (deleted) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|l", &keys, &value) == FAILURE) {
return;
}
}
else {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|lll", &keys, &value, &defval, &exptime) == FAILURE) {
return;
}
defval_used = ZEND_NUM_ARGS() >= 3;
}
}
if (!mmc_get_pool(mmc_object, &pool) || !pool->num_servers) {
RETURN_FALSE;
}
value_handler_param[0] = return_value;
value_handler_param[1] = NULL;
value_handler_param[2] = NULL;
if (Z_TYPE_P(keys) == IS_ARRAY) {
zval *key;
if (deleted) {
/* changed to true/false by mmc_numeric_response_handler */
RETVAL_NULL();
}
else {
/* populated with responses by mmc_numeric_response_handler and mmc_value_handler_multi */
array_init(return_value);
}
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(keys), key) {
/* allocate request */
request = mmc_pool_request(
pool, MMC_PROTO_TCP, mmc_numeric_response_handler, return_value,
mmc_pool_failover_handler, NULL);
request->value_handler = mmc_value_handler_multi;
request->value_handler_param = value_handler_param;
if (mmc_prepare_key(key, request->key, &(request->key_len)) != MMC_OK) {
mmc_pool_release(pool, request);
php_error_docref(NULL, E_WARNING, "Invalid key");
continue;
}
if (deleted) {
pool->protocol->delete(request, request->key, request->key_len, exptime);
}
else {
pool->protocol->mutate(request, key, request->key, request->key_len, invert ? -value : value, defval, defval_used, exptime);
}
/* schedule request */
if (mmc_pool_schedule_key(pool, request->key, request->key_len, request, MEMCACHE_G(redundancy)) != MMC_OK) {
continue;
}
/* begin sending requests immediatly */
mmc_pool_select(pool);
} ZEND_HASH_FOREACH_END();
}
else {
/* changed to true/false by mmc_numeric_response_handler or set to a value
* by mmc_value_handler_single if incr/decr returns one */
RETVAL_NULL();
/* allocate request */
request = mmc_pool_request(pool, MMC_PROTO_TCP,
mmc_numeric_response_handler, return_value, mmc_pool_failover_handler, NULL);
request->value_handler = mmc_value_handler_single;
request->value_handler_param = value_handler_param;
if (mmc_prepare_key(keys, request->key, &(request->key_len)) != MMC_OK) {
mmc_pool_release(pool, request);
php_error_docref(NULL, E_WARNING, "Invalid key");
RETURN_FALSE;
}
if (deleted) {
pool->protocol->delete(request, request->key, request->key_len, exptime);
}
else {
pool->protocol->mutate(request, keys, request->key, request->key_len, invert ? -value : value, defval, defval_used, exptime);
}
/* schedule request */
if (mmc_pool_schedule_key(pool, request->key, request->key_len, request, MEMCACHE_G(redundancy)) != MMC_OK) {
RETURN_FALSE;
}
}
/* execute all requests */
mmc_pool_run(pool);
}
/* }}} */
mmc_t *mmc_find_persistent(const char *host, int host_len, unsigned short port, unsigned short udp_port, double timeout, int retry_interval) /* {{{ */
{
mmc_t *mmc;
zend_resource *le;
char *key;
int key_len;
key_len = spprintf(&key, 0, "memcache:server:%s:%u:%u", host, port, udp_port);
if ((le = zend_hash_str_find_ptr(&EG(persistent_list), key, key_len)) == NULL) {
mmc = mmc_server_new(host, host_len, port, udp_port, 1, timeout, retry_interval);
le = zend_register_resource(mmc, le_memcache_server);
/* register new persistent connection */
if (zend_hash_str_update_mem(&EG(persistent_list), key, key_len, le, sizeof(*le)) == NULL) {
mmc_server_free(mmc);
mmc = NULL;
} else {
MEMCACHE_LIST_INSERT(mmc, le_memcache_server);
}
}
else if (le->type != le_memcache_server || le->ptr == NULL) {
zend_hash_str_del(&EG(persistent_list), key, key_len);
mmc = mmc_server_new(host, host_len, port, udp_port, 1, timeout, retry_interval);
le->type = le_memcache_server;
le->ptr = mmc;
#if PHP_VERSION_ID < 70300
GC_REFCOUNT(le) = 1;
#else
GC_SET_REFCOUNT(le, 1);
#endif
/* register new persistent connection */
if (zend_hash_str_update_mem(&EG(persistent_list), key, key_len, le, sizeof(*le)) == NULL) {
mmc_server_free(mmc);
mmc = NULL;
}
else {
MEMCACHE_LIST_INSERT(mmc, le_memcache_server);
}
}
else {
mmc = (mmc_t *)le->ptr;
mmc->timeout = double_to_timeval(timeout);
mmc->tcp.retry_interval = retry_interval;
/* attempt to reconnect this node before failover in case connection has gone away */
if (mmc->tcp.status == MMC_STATUS_CONNECTED) {
mmc->tcp.status = MMC_STATUS_UNKNOWN;
}
if (mmc->udp.status == MMC_STATUS_CONNECTED) {
mmc->udp.status = MMC_STATUS_UNKNOWN;
}
}
efree(key);
return mmc;
}
/* }}} */
static mmc_t *php_mmc_pool_addserver(
zval *mmc_object, const char *host, int host_len, long tcp_port, long udp_port, long weight,
zend_bool persistent, double timeout, long retry_interval, zend_bool status, mmc_pool_t **pool_result) /* {{{ */
{
zval *connection;
mmc_pool_t *pool;
mmc_t *mmc;
zend_resource *list_res;
if (weight < 1) {
php_error_docref(NULL, E_WARNING, "weight must be a positive integer");
return NULL;
}
if (tcp_port > 65635 || tcp_port < 0) {
php_error_docref(NULL, E_WARNING, "invalid tcp port number");
return NULL;
}
if (udp_port > 65635 || udp_port < 0) {
php_error_docref(NULL, E_WARNING, "invalid udp port number");
return NULL;
}
/* initialize pool if need be */
if ((connection = zend_hash_str_find(Z_OBJPROP_P(mmc_object), "connection", sizeof("connection")-1)) == NULL) {
pool = mmc_pool_new();
pool->failure_callback = &php_mmc_failure_callback;
list_res = zend_register_resource(pool, le_memcache_pool);
add_property_resource(mmc_object, "connection", list_res);
#if PHP_VERSION_ID < 70300
GC_REFCOUNT(list_res)++;
#else
GC_ADDREF(list_res);
#endif
}
else {
pool = zend_fetch_resource_ex(connection, "connection", le_memcache_pool);
if (!pool) {
php_error_docref(NULL, E_WARNING, "Unknown connection identifier");
return NULL;
}
}
/* binary protocol isn't support over UDP yet */
if (udp_port && pool->protocol == &mmc_binary_protocol) {
php_error_docref(NULL, E_NOTICE, "binary protocol isn't support over UDP, defaulting to TCP");
udp_port = 0;
}
/* lazy initialization of server struct */
if (persistent && status) {
mmc = mmc_find_persistent(host, host_len, (unsigned short) tcp_port, (unsigned short) udp_port, timeout, retry_interval);
}
else {
mmc = mmc_server_new(host, host_len, (unsigned short) tcp_port, (unsigned short) udp_port, 0, timeout, retry_interval);
}
/* add server in failed mode */
if (!status) {
mmc->tcp.status = MMC_STATUS_FAILED;
mmc->udp.status = MMC_STATUS_FAILED;
}
mmc_pool_add(pool, mmc, weight);
if (pool_result != NULL) {
*pool_result = pool;
}
if (pool->protocol == &mmc_binary_protocol) {
zval rv1, rv2;
zval *username = zend_read_property(memcache_ce, mmc_object, "username", strlen("username"), 1, &rv1);
zval *password = zend_read_property(memcache_ce, mmc_object, "password", strlen("password"), 1, &rv2);
if (Z_TYPE_P(username) == IS_STRING && Z_TYPE_P(password) == IS_STRING) {
if (Z_STRLEN_P(username) > 1 && Z_STRLEN_P(password) > 1) {
mmc_request_t *request;
zval sasl_value;
/* allocate request */
request = mmc_pool_request(pool, MMC_PROTO_TCP, mmc_stored_handler, &sasl_value, mmc_pool_failover_handler, NULL);
pool->protocol->set_sasl_auth_data(pool, request, Z_STRVAL_P(username), Z_STRVAL_P(password));
/* schedule request */
if (mmc_pool_schedule_key(pool, request->key, request->key_len, request, MEMCACHE_G(redundancy)) != MMC_OK) {
return NULL;
}
}
}
}
return mmc;
}
/* }}} */
static void php_mmc_connect(INTERNAL_FUNCTION_PARAMETERS, zend_bool persistent) /* {{{ */
{
zval *mmc_object = getThis();
mmc_pool_t *pool;
mmc_t *mmc;
char *host;
size_t host_len;
zend_long tcp_port = MEMCACHE_G(default_port);
double timeout = MMC_DEFAULT_TIMEOUT;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|ld", &host, &host_len, &tcp_port, &timeout) == FAILURE) {
return;
}
/* initialize pool and object if need be */
if (!mmc_object) {
zend_resource *list_res;
mmc_pool_t *pool = mmc_pool_new();
pool->failure_callback = &php_mmc_failure_callback;
list_res = zend_register_resource(pool, le_memcache_pool);
mmc_object = return_value;
object_init_ex(mmc_object, memcache_ce);
add_property_resource(mmc_object, "connection", list_res);
#if PHP_VERSION_ID < 70300
GC_REFCOUNT(list_res)++;
#else
GC_ADDREF(list_res);
#endif
} else {
RETVAL_TRUE;
}
mmc = php_mmc_pool_addserver(mmc_object, host, host_len, tcp_port, 0, 1, persistent, timeout, MMC_DEFAULT_RETRY, 1, NULL);
if (mmc == NULL) {
RETURN_FALSE;
}
/* force a reconnect attempt if stream EOF */
if (mmc->tcp.stream != NULL && php_stream_eof(mmc->tcp.stream)) {
mmc_server_disconnect(mmc, &(mmc->tcp));
}
if (!mmc_get_pool(mmc_object, &pool)) {
RETURN_FALSE;
}
/* force a tcp connect (if not persistently connected) */
if (mmc_pool_open(pool, mmc, &(mmc->tcp), 0) != MMC_OK) {
php_error_docref(NULL, E_WARNING, "Can't connect to %s:%d, %s (%d)", host, mmc->tcp.port, mmc->error ? mmc->error : "Unknown error", mmc->errnum);
RETURN_FALSE;
}
}
/* }}} */
/*
* STAT 6:chunk_size 64
*/
static int mmc_stats_parse_stat(char *start, char *end, zval *result) /* {{{ */
{
char *key;
const char *space, *colon;
zend_long index = 0;
if (Z_TYPE_P(result) != IS_ARRAY) {
array_init(result);
}
/* find space delimiting key and value */
if ((space = php_memnstr(start, " ", 1, end)) == NULL) {
return 0;
}
/* find colon delimiting subkeys */
if ((colon = php_memnstr(start, ":", 1, space - 1)) != NULL) {
zval *element, *elem;
zval new_element;
key = estrndup(start, colon - start);
/* find existing or create subkey array in result */
if ((is_numeric_string(key, colon - start, &index, NULL, 0) &&
(elem = zend_hash_index_find(Z_ARRVAL_P(result), index)) != NULL) ||
(elem = zend_hash_str_find(Z_ARRVAL_P(result), key, colon - start)) != NULL) {
element = elem;
}
else {
array_init(&new_element);
add_assoc_zval_ex(result, key, colon - start, &new_element);
element = &new_element;
}
efree(key);
return mmc_stats_parse_stat(((char *) colon) + 1, end, element);
}
/* no more subkeys, add value under last subkey */
key = estrndup(start, space - start);
add_assoc_stringl_ex(result, key, ((char *) space) - start, ((char *) space) + 1, end - ((char *) space));
efree(key);
return 1;
}
/* }}} */
/*
* ITEM test_key [3 b; 1157099416 s]
*/
static int mmc_stats_parse_item(char *start, char *end, zval *result) /* {{{ */
{
char *key;
const char *space, *value, *value_end;
zval element;
if (Z_TYPE_P(result) != IS_ARRAY) {
array_init(result);
}
/* find space delimiting key and value */
if ((space = php_memnstr(start, " ", 1, end)) == NULL) {
return 0;
}
array_init(&element);
/* parse each contained value */
for (value = php_memnstr(space, "[", 1, end); value != NULL && value <= end; value = php_memnstr(value + 1, ";", 1, end)) {
do {
value++;
} while (*value == ' ' && value <= end);
if (value <= end && (value_end = php_memnstr(value, " ", 1, end)) != NULL && value_end <= end) {
add_next_index_stringl(&element, value, value_end - value);
}
}
/* add parsed values under key */
key = estrndup(start, space - start);
add_assoc_zval_ex(result, key, space - start, &element);
efree(key);
return 1;
}
/* }}} */
static int mmc_stats_parse_generic(char *start, char *end, zval *result) /* {{{ */
{
const char *space;
char *key;
if (Z_TYPE_P(result) != IS_ARRAY) {
array_init(result);
}
if (start < end) {
if ((space = php_memnstr(start, " ", 1, end)) != NULL) {
key = estrndup(start, space - start);
add_assoc_stringl_ex(result, key, ((char *) space) - start + 1, ((char *) space) + 1, end - ((char *) space));
efree(key);
}
else {
add_next_index_stringl(result, start, end - start);
}
}
else {
return 0;
}
return 1;
}
/* }}} */
static void php_mmc_failure_callback(mmc_pool_t *pool, mmc_t *mmc, zval *param) /* {{{ */
{
zval *callback;
/* check for userspace callback */
if (!Z_ISUNDEF_P(param) && (callback = zend_hash_str_find(Z_OBJPROP_P((zval *)param), "_failureCallback", sizeof("_failureCallback")-1)) != NULL && Z_TYPE_P(callback) != IS_NULL) {
if (MEMCACHE_IS_CALLABLE(callback, 0, NULL)) {
zval retval;
zval *host, *tcp_port, *udp_port, *error, *errnum;
zval params[5];
ZVAL_UNDEF(&retval);
host = ¶ms[0];
tcp_port = ¶ms[1];
udp_port = ¶ms[2];
error = ¶ms[3];
errnum = ¶ms[4];
ZVAL_STRING(host, mmc->host);
ZVAL_LONG(tcp_port, mmc->tcp.port); ZVAL_LONG(udp_port, mmc->udp.port);
if (mmc->error != NULL) {
ZVAL_STRING(error, mmc->error);
}
else {
ZVAL_NULL(error);
}
ZVAL_LONG(errnum, mmc->errnum);
call_user_function_ex(EG(function_table), NULL, callback, &retval, 5, params, 0, NULL);
zval_ptr_dtor(host);
zval_ptr_dtor(tcp_port); zval_ptr_dtor(udp_port);
zval_ptr_dtor(error); zval_ptr_dtor(errnum);
if (Z_TYPE(retval) != IS_UNDEF) {
zval_ptr_dtor(&retval);
}
}
else {
php_mmc_set_failure_callback(pool, (zval *)param, NULL);
php_error_docref(NULL, E_WARNING, "Invalid failure callback");
}
}
else {
php_error_docref(NULL, E_NOTICE, "Server %s (tcp %d, udp %d) failed with: %s (%d)",
mmc->host, mmc->tcp.port, mmc->udp.port, mmc->error, mmc->errnum);
}
}
/* }}} */
static void php_mmc_set_failure_callback(mmc_pool_t *pool, zval *mmc_object, zval *callback) /* {{{ */
{
// Decrease refcount of old mmc_object
if (!Z_ISUNDEF(pool->failure_callback_param)) {
Z_DELREF(pool->failure_callback_param);
}
if (callback != NULL) {
zval callback_tmp;
callback_tmp = *callback;
zval_copy_ctor(&callback_tmp);
add_property_zval(mmc_object, "_failureCallback", &callback_tmp);
zval_ptr_dtor(&callback_tmp);
pool->failure_callback_param = *mmc_object;
Z_ADDREF_P(mmc_object);
}
else {
add_property_null(mmc_object, "_failureCallback");
ZVAL_UNDEF(&pool->failure_callback_param);
}
}
/* }}} */
/* ----------------
module functions
---------------- */
/* {{{ proto bool MemcachePool::connect(string host [, int tcp_port [, int udp_port [, bool persistent [, int weight [, double timeout [, int retry_interval] ] ] ] ] ])
Connects to server and returns a Memcache object */
PHP_NAMED_FUNCTION(zif_memcache_pool_connect)
{
zval *mmc_object = getThis();
mmc_pool_t *pool;
mmc_t *mmc;
char *host;
size_t host_len;
zend_long tcp_port = MEMCACHE_G(default_port), udp_port = 0, weight = 1, retry_interval = MMC_DEFAULT_RETRY;
double timeout = MMC_DEFAULT_TIMEOUT;
zend_bool persistent = 1;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|llbldl",
&host, &host_len, &tcp_port, &udp_port, &persistent, &weight, &timeout, &retry_interval) == FAILURE) {
return;
}
mmc = php_mmc_pool_addserver(mmc_object, host, host_len, tcp_port, udp_port, weight, persistent, timeout, retry_interval, 1, NULL);
if (mmc == NULL) {
RETURN_FALSE;
}
/* force a reconnect attempt if stream EOF */
if (mmc->tcp.stream != NULL && php_stream_eof(mmc->tcp.stream)) {
mmc_server_disconnect(mmc, &(mmc->tcp));
}
if (!mmc_get_pool(mmc_object, &pool)) {
RETURN_FALSE;
}
/* force a tcp connect (if not persistently connected) */
if (mmc_pool_open(pool, mmc, &(mmc->tcp), 0) != MMC_OK) {
php_error_docref(NULL, E_WARNING, "Can't connect to %s:%d, %s (%d)", host, mmc->tcp.port, mmc->error ? mmc->error : "Unknown error", mmc->errnum);
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto object memcache_connect(string host [, int port [, double timeout ] ])
Connects to server and returns a Memcache object */
PHP_FUNCTION(memcache_connect)
{
php_mmc_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
/* {{{ proto object memcache_pconnect(string host [, int port [, double timeout ] ])
Connects to server and returns a Memcache object */
PHP_FUNCTION(memcache_pconnect)
{
php_mmc_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */
/* {{{ proto bool MemcachePool::addServer(string host [, int tcp_port [, int udp_port [, bool persistent [, int weight [, double timeout [, int retry_interval [, bool status] ] ] ] ])
Adds a server to the pool */
PHP_NAMED_FUNCTION(zif_memcache_pool_addserver)
{
zval *mmc_object = getThis();
mmc_t *mmc;
char *host;
size_t host_len;
zend_long tcp_port = MEMCACHE_G(default_port), udp_port = 0, weight = 1, retry_interval = MMC_DEFAULT_RETRY;
double timeout = MMC_DEFAULT_TIMEOUT;
zend_bool persistent = 1, status = 1;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|llbldlb",
&host, &host_len, &tcp_port, &udp_port, &persistent, &weight, &timeout, &retry_interval, &status) == FAILURE) {
return;
}
mmc = php_mmc_pool_addserver(mmc_object, host, host_len, tcp_port, udp_port, weight, persistent, timeout, retry_interval, status, NULL);
if (mmc == NULL) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto string MemcachePool::findServer(string key)
Returns the server corresponding to a key
*/
PHP_NAMED_FUNCTION(zif_memcache_pool_findserver)
{
zval *mmc_object = getThis();
mmc_pool_t *pool;
mmc_t *mmc;
zval *zkey;
char key[MMC_MAX_KEY_LEN + 1];
unsigned int key_len;
zend_string *hostname;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zkey) == FAILURE) {
return;
}
if (!mmc_get_pool(mmc_object, &pool) || !pool->num_servers) {
RETURN_FALSE;
}
if (mmc_prepare_key(zkey, key, &key_len) != MMC_OK) {
php_error_docref(NULL, E_WARNING, "Invalid key");
RETURN_FALSE;
}
mmc = mmc_pool_find(pool, key, key_len);
hostname = strpprintf(0, "%s:%d", mmc->host, mmc->tcp.port);
RETURN_STR(hostname);
}
/* }}} */
/* {{{ proto bool memcache_add_server(string host [, int port [, bool persistent [, int weight [, double timeout [, int retry_interval [, bool status [, callback failure_callback ] ] ] ] ] ] ])
Adds a connection to the pool. The order in which this function is called is significant */
PHP_FUNCTION(memcache_add_server)
{
zval *mmc_object = getThis(), *failure_callback = NULL;
mmc_pool_t *pool;
mmc_t *mmc;
char *host;
size_t host_len;
zend_long tcp_port = MEMCACHE_G(default_port), weight = 1, retry_interval = MMC_DEFAULT_RETRY;
double timeout = MMC_DEFAULT_TIMEOUT;
zend_bool persistent = 1, status = 1;
if (mmc_object) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|lbldlbz",
&host, &host_len, &tcp_port, &persistent, &weight, &timeout, &retry_interval, &status, &failure_callback) == FAILURE) {
return;
}
}
else {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os|lbldlbz", &mmc_object, memcache_ce,
&host, &host_len, &tcp_port, &persistent, &weight, &timeout, &retry_interval, &status, &failure_callback) == FAILURE) {
return;
}
}
if (failure_callback != NULL && Z_TYPE_P(failure_callback) != IS_NULL) {
if (!MEMCACHE_IS_CALLABLE(failure_callback, 0, NULL)) {
php_error_docref(NULL, E_WARNING, "Invalid failure callback");
RETURN_FALSE;
}
}
mmc = php_mmc_pool_addserver(mmc_object, host, host_len, tcp_port, 0, weight, persistent, timeout, retry_interval, status, &pool);
if (mmc == NULL) {
RETURN_FALSE;
}
if (failure_callback != NULL && Z_TYPE_P(failure_callback) != IS_NULL) {
php_mmc_set_failure_callback(pool, mmc_object, failure_callback);
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool memcache_set_server_params( string host [, int port [, double timeout [, int retry_interval [, bool status [, callback failure_callback ] ] ] ] ])
Changes server parameters at runtime */
PHP_FUNCTION(memcache_set_server_params)
{
zval *mmc_object = getThis(), *failure_callback = NULL;
mmc_pool_t *pool;
mmc_t *mmc = NULL;
zend_long tcp_port = MEMCACHE_G(default_port), retry_interval = MMC_DEFAULT_RETRY;
double timeout = MMC_DEFAULT_TIMEOUT;
zend_bool status = 1;
size_t host_len;
int i;
char *host;
if (mmc_object) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|ldlbz",
&host, &host_len, &tcp_port, &timeout, &retry_interval, &status, &failure_callback) == FAILURE) {
return;
}
}
else {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os|ldlbz", &mmc_object, memcache_pool_ce,
&host, &host_len, &tcp_port, &timeout, &retry_interval, &status, &failure_callback) == FAILURE) {
return;
}
}
if (!mmc_get_pool(mmc_object, &pool)) {
RETURN_FALSE;
}
for (i=0; i<pool->num_servers; i++) {
if (!strcmp(pool->servers[i]->host, host) && pool->servers[i]->tcp.port == tcp_port) {
mmc = pool->servers[i];
break;
}
}
if (!mmc) {
php_error_docref(NULL, E_WARNING, "Server not found in pool");
RETURN_FALSE;
}
if (failure_callback != NULL && Z_TYPE_P(failure_callback) != IS_NULL) {
if (!MEMCACHE_IS_CALLABLE(failure_callback, 0, NULL)) {
php_error_docref(NULL, E_WARNING, "Invalid failure callback");
RETURN_FALSE;
}
}
mmc->timeout = double_to_timeval(timeout);
mmc->tcp.retry_interval = retry_interval;
/* store the smallest timeout for any server */
if (timeval_to_double(mmc->timeout) < timeval_to_double(pool->timeout)) {
pool->timeout = mmc->timeout;
}
if (!status) {
mmc->tcp.status = MMC_STATUS_FAILED;
mmc->udp.status = MMC_STATUS_FAILED;
}
else {
if (mmc->tcp.status == MMC_STATUS_FAILED) {
mmc->tcp.status = MMC_STATUS_DISCONNECTED;
}
if (mmc->udp.status == MMC_STATUS_FAILED) {
mmc->udp.status = MMC_STATUS_DISCONNECTED;
}
}
if (failure_callback != NULL) {
if (Z_TYPE_P(failure_callback) != IS_NULL) {
php_mmc_set_failure_callback(pool, mmc_object, failure_callback);
}
else {
php_mmc_set_failure_callback(pool, mmc_object, NULL);
}
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool memcache_set_failure_callback( callback failure_callback )
Changes the failover callback */
PHP_FUNCTION(memcache_set_failure_callback)
{
zval *mmc_object = getThis(), *failure_callback;
mmc_pool_t *pool;
if (mmc_object) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z",
&failure_callback) == FAILURE) {
return;
}
}
else {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz", &mmc_object, memcache_pool_ce,
&failure_callback) == FAILURE) {
return;
}
}
if (!mmc_get_pool(mmc_object, &pool)) {
RETURN_FALSE;
}
if (Z_TYPE_P(failure_callback) != IS_NULL) {
if (!MEMCACHE_IS_CALLABLE(failure_callback, 0, NULL)) {
php_error_docref(NULL, E_WARNING, "Invalid failure callback");
RETURN_FALSE;
}
}
if (Z_TYPE_P(failure_callback) != IS_NULL) {
php_mmc_set_failure_callback(pool, mmc_object, failure_callback);
}
else {
php_mmc_set_failure_callback(pool, mmc_object, NULL);
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int memcache_get_server_status( string host [, int port ])
Returns server status (0 if server is failed, otherwise non-zero) */
PHP_FUNCTION(memcache_get_server_status)
{
zval *mmc_object = getThis();
mmc_pool_t *pool;
mmc_t *mmc = NULL;
zend_long tcp_port = MEMCACHE_G(default_port);
size_t host_len;
int i;
char *host;
if (mmc_object) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &host, &host_len, &tcp_port) == FAILURE) {
return;
}
}
else {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os|l", &mmc_object, memcache_pool_ce, &host, &host_len, &tcp_port) == FAILURE) {
return;
}
}
if (!mmc_get_pool(mmc_object, &pool)) {
RETURN_FALSE;
}
for (i=0; i<pool->num_servers; i++) {
if (!strcmp(pool->servers[i]->host, host) && pool->servers[i]->tcp.port == tcp_port) {
mmc = pool->servers[i];
break;
}
}
if (!mmc) {
php_error_docref(NULL, E_WARNING, "Server not found in pool");
RETURN_FALSE;
}
RETURN_LONG(mmc->tcp.status > MMC_STATUS_FAILED ? 1 : 0);
}
/* }}} */
static int mmc_version_handler(mmc_t *mmc, mmc_request_t *request, int response, const char *message, unsigned int message_len, void *param) /*
parses the VERSION response line, param is a zval pointer to store version into {{{ */
{
if (response != MMC_RESPONSE_ERROR) {
char *version = emalloc(message_len + 1);
if (sscanf(message, "VERSION %s", version) == 1) {
ZVAL_STRING((zval *)param, version);
efree(version);
}
else {
efree(version);
ZVAL_STRINGL((zval *)param, (char *)message, message_len);
}
return MMC_REQUEST_DONE;
}
return mmc_request_failure(mmc, request->io, message, message_len, 0);
}
/* }}} */
/* {{{ proto string memcache_get_version( object memcache )
Returns server's version */
PHP_FUNCTION(memcache_get_version)
{
mmc_pool_t *pool;
zval *mmc_object = getThis();
int i;
mmc_request_t *request;
if (mmc_object == NULL) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &mmc_object, memcache_pool_ce) == FAILURE) {
return;
}
}
if (!mmc_get_pool(mmc_object, &pool) || !pool->num_servers) {
RETURN_FALSE;
}
RETVAL_FALSE;
for (i=0; i<pool->num_servers; i++) {
/* run command and check for valid return value */
request = mmc_pool_request(pool, MMC_PROTO_TCP, mmc_version_handler, return_value, NULL, NULL);
pool->protocol->version(request);
if (mmc_pool_schedule(pool, pool->servers[i], request) == MMC_OK) {
mmc_pool_run(pool);
if (Z_TYPE_P(return_value) == IS_STRING) {
break;
}
}
}
}
/* }}} */
/* {{{ proto bool memcache_add(object memcache, mixed key [, mixed var [, int flag [, int exptime ] ] ])
Adds new item. Item with such key should not exist. */
PHP_FUNCTION(memcache_add)
{
php_mmc_store(INTERNAL_FUNCTION_PARAM_PASSTHRU, MMC_OP_ADD);
}
/* }}} */
/* {{{ proto bool memcache_set(object memcache, mixed key [, mixed var [, int flag [, int exptime ] ] ])
Sets the value of an item. Item may exist or not */
PHP_FUNCTION(memcache_set)
{
php_mmc_store(INTERNAL_FUNCTION_PARAM_PASSTHRU, MMC_OP_SET);
}
/* }}} */
/* {{{ proto bool memcache_replace(object memcache, mixed key [, mixed var [, int flag [, int exptime ] ] )
Replaces existing item. Returns false if item doesn't exist */
PHP_FUNCTION(memcache_replace)
{
php_mmc_store(INTERNAL_FUNCTION_PARAM_PASSTHRU, MMC_OP_REPLACE);
}
/* }}} */
/* {{{ proto bool memcache_cas(object memcache, mixed key [, mixed var [, int flag [, int exptime [, long cas ] ] ] ])
Sets the value of an item if the CAS value is the same (Compare-And-Swap) */
PHP_FUNCTION(memcache_cas)
{
php_mmc_store(INTERNAL_FUNCTION_PARAM_PASSTHRU, MMC_OP_CAS);
}
/* }}} */
/* {{{ proto bool memcache_prepend(object memcache, mixed key [, mixed var [, int flag [, int exptime ] ] ])
Appends a value to the stored value, value must exist */
PHP_FUNCTION(memcache_append)
{
php_mmc_store(INTERNAL_FUNCTION_PARAM_PASSTHRU, MMC_OP_APPEND);
}
/* }}} */
/* {{{ proto bool memcache_prepend(object memcache, mixed key [, mixed var [, int flag [, int exptime ] ] ])
Prepends a value to the stored value, value must exist */
PHP_FUNCTION(memcache_prepend)
{
php_mmc_store(INTERNAL_FUNCTION_PARAM_PASSTHRU, MMC_OP_PREPEND);
}
/* }}} */
int mmc_value_handler_multi(
const char *key, unsigned int key_len, zval *value,
unsigned int flags, unsigned long cas, void *param) /*
receives a multiple values, param is a zval** array to store value and flags in {{{ */
{
zval **result = (zval **)param;
/* add value to result */
if (Z_TYPE_P(result[0]) != IS_ARRAY) {
array_init(result[0]);
}
add_assoc_zval_ex(result[0], (char *)key, key_len, value);
/* add flags to result */
if (result[1] != NULL) {
if (Z_TYPE_P(result[1]) != IS_ARRAY) {
array_init(result[1]);
}
add_assoc_long_ex(result[1], (char *)key, key_len, flags);
}
/* add CAS value to result */
if (result[2] != NULL) {
if (Z_TYPE_P(result[2]) != IS_ARRAY) {
array_init(result[2]);
}
add_assoc_long_ex(result[2], (char *)key, key_len, cas);
}
return MMC_REQUEST_DONE;
}
/* }}} */
int mmc_value_handler_single(
const char *key, unsigned int key_len, zval *value,
unsigned int flags, unsigned long cas, void *param) /*
receives a single value, param is a zval pointer to store value to {{{ */
{
zval **result = (zval **)param;
ZVAL_ZVAL(result[0], value, 1, 1);
if (result[1] != NULL) {
ZVAL_LONG(result[1], flags);
}
if (result[2] != NULL) {
ZVAL_LONG(result[2], cas);
}
return MMC_REQUEST_DONE;
}
/* }}} */
static int mmc_value_failover_handler(mmc_pool_t *pool, mmc_t *mmc, mmc_request_t *request, void *param) /*
uses keys and return value to reschedule requests to other servers, param is a zval ** pointer {{{ */
{
zval *keys = ((zval **)param)[0], **value_handler_param = (zval **)((void **)param)[1];
zval *key;
if (!MEMCACHE_G(allow_failover) || request->failed_servers.len >= MEMCACHE_G(max_failover_attempts)) {
mmc_pool_release(pool, request);
return MMC_REQUEST_FAILURE;
}
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(keys), key) {
if (Z_TYPE_P(value_handler_param[0]) != IS_ARRAY ||
!zend_hash_str_exists(Z_ARRVAL_P(value_handler_param[0]), Z_STRVAL_P(key), Z_STRLEN_P(key)))
{
mmc_pool_schedule_get(pool, MMC_PROTO_UDP,
value_handler_param[2] != NULL ? MMC_OP_GETS : MMC_OP_GET, key,
request->value_handler, request->value_handler_param,
request->failover_handler, request->failover_handler_param, request);
}
} ZEND_HASH_FOREACH_END();
mmc_pool_release(pool, request);
return MMC_OK;
}
/* }}}*/
/* {{{ proto mixed memcache_get( object memcache, mixed key [, mixed &flags [, mixed &cas ] ] )
Returns value of existing item or false */
PHP_FUNCTION(memcache_get)
{
mmc_pool_t *pool;
zval *keys, *flags = NULL, *cas = NULL, *mmc_object = getThis();
void *value_handler_param[3], *failover_handler_param[2];
if (mmc_object == NULL) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz|z/z/", &mmc_object, memcache_pool_ce, &keys, &flags, &cas) == FAILURE) {
return;
}
}
else {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|z/z/", &keys, &flags, &cas) == FAILURE) {
return;
}
}
if (!mmc_get_pool(mmc_object, &pool) || !pool->num_servers) {
RETURN_FALSE;
}
value_handler_param[0] = return_value;
value_handler_param[1] = flags;
value_handler_param[2] = cas;
if (Z_TYPE_P(keys) == IS_ARRAY) {
zval *zv;
/* return empty array if no keys found */
array_init(return_value);
failover_handler_param[0] = keys;
failover_handler_param[1] = value_handler_param;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(keys), zv) {
/* schedule request */
mmc_pool_schedule_get(pool, MMC_PROTO_UDP,
cas != NULL ? MMC_OP_GETS : MMC_OP_GET, zv,
mmc_value_handler_multi, value_handler_param,
mmc_value_failover_handler, failover_handler_param, NULL);
} ZEND_HASH_FOREACH_END();
}
else {
mmc_request_t *request;
/* return false if key isn't found */
ZVAL_FALSE(return_value);
/* allocate request */
request = mmc_pool_request_get(
pool, MMC_PROTO_TCP,
mmc_value_handler_single, value_handler_param,
mmc_pool_failover_handler, NULL);
if (mmc_prepare_key(keys, request->key, &(request->key_len)) != MMC_OK) {
mmc_pool_release(pool, request);
php_error_docref(NULL, E_WARNING, "Invalid key");
return;
}
pool->protocol->get(request, cas != NULL ? MMC_OP_GETS : MMC_OP_GET, keys, request->key, request->key_len);
/* schedule request */
if (mmc_pool_schedule_key(pool, request->key, request->key_len, request, 1) != MMC_OK) {
return;
}
}
/* execute all requests */
mmc_pool_run(pool);
}
/* }}} */
static int mmc_stats_handler(mmc_t *mmc, mmc_request_t *request, int response, const char *message, unsigned int message_len, void *param) /*
parses the stats response line, param is a zval pointer to store stats into {{{ */
{
if (response != MMC_RESPONSE_ERROR)
{
char *line = (char *)message;
if(!message_len) {
return MMC_REQUEST_DONE;
}
if (mmc_str_left(line, "RESET", message_len, sizeof("RESET")-1)) {
ZVAL_TRUE((zval *)param);
return MMC_REQUEST_DONE;
}
else if (mmc_str_left(line, "STAT ", message_len, sizeof("STAT ")-1)) {
if (mmc_stats_parse_stat(line + sizeof("STAT ")-1, line + message_len - 1, (zval *)param)) {
return MMC_REQUEST_AGAIN;
}
}
else if (mmc_str_left(line, "ITEM ", message_len, sizeof("ITEM ")-1)) {
if (mmc_stats_parse_item(line + sizeof("ITEM ")-1, line + message_len - 1, (zval *)param)) {
return MMC_REQUEST_AGAIN;
}
}
else if (mmc_str_left(line, "END", message_len, sizeof("END")-1)) {
return MMC_REQUEST_DONE;
}
else if (mmc_stats_parse_generic(line, line + message_len, (zval *)param)) {
return MMC_REQUEST_AGAIN;
}
zval_dtor((zval *)param);
ZVAL_FALSE((zval *)param);
return MMC_REQUEST_FAILURE;
}
return mmc_request_failure(mmc, request->io, message, message_len, 0);
}
/* }}} */
static int mmc_stats_checktype(const char *type) { /* {{{ */
return type == NULL ||
!strcmp(type, "reset") ||
!strcmp(type, "malloc") ||
!strcmp(type, "slabs") ||
!strcmp(type, "cachedump") ||
!strcmp(type, "items") ||
!strcmp(type, "sizes");
}
/* }}} */
/* {{{ proto array memcache_get_stats( object memcache [, string type [, int slabid [, int limit ] ] ])
Returns server's statistics */
PHP_FUNCTION(memcache_get_stats)
{
mmc_pool_t *pool;
zval *mmc_object = getThis();
char *type = NULL;
int i;
size_t type_len = 0;
zend_long slabid = 0, limit = MMC_DEFAULT_CACHEDUMP_LIMIT;
mmc_request_t *request;
if (mmc_object == NULL) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|sll", &mmc_object, memcache_pool_ce, &type, &type_len, &slabid, &limit) == FAILURE) {
return;
}
}
else {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sll", &type, &type_len, &slabid, &limit) == FAILURE) {
return;
}
}
if (!mmc_get_pool(mmc_object, &pool) || !pool->num_servers) {
RETURN_FALSE;
}
if (!mmc_stats_checktype(type)) {
php_error_docref(NULL, E_WARNING, "Invalid stats type");
RETURN_FALSE;
}
ZVAL_FALSE(return_value);
for (i=0; i<pool->num_servers; i++) {
request = mmc_pool_request(pool, MMC_PROTO_TCP, mmc_stats_handler, return_value, NULL, NULL);
pool->protocol->stats(request, type, slabid, limit);
/* run command and check for valid return value */
if (mmc_pool_schedule(pool, pool->servers[i], request) == MMC_OK) {
mmc_pool_run(pool);
if (Z_TYPE_P(return_value) != IS_FALSE) {
break;
}
}
}
/* execute all requests */
mmc_pool_run(pool);
}
/* }}} */
/* {{{ proto array memcache_get_extended_stats( object memcache [, string type [, int slabid [, int limit ] ] ])
Returns statistics for each server in the pool */
PHP_FUNCTION(memcache_get_extended_stats)
{
mmc_pool_t *pool;
zval *mmc_object = getThis();
char *host, *type = NULL;
int i;
size_t host_len, type_len = 0;
zend_long slabid = 0, limit = MMC_DEFAULT_CACHEDUMP_LIMIT;
mmc_request_t *request;
if (mmc_object == NULL) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|sll", &mmc_object, memcache_pool_ce, &type, &type_len, &slabid, &limit) == FAILURE) {
return;
}
}
else {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sll", &type, &type_len, &slabid, &limit) == FAILURE) {
return;
}
}
if (!mmc_get_pool(mmc_object, &pool) || !pool->num_servers) {
RETURN_FALSE;
}
if (!mmc_stats_checktype(type)) {
php_error_docref(NULL, E_WARNING, "Invalid stats type");
RETURN_FALSE;
}
array_init(return_value);
for (i=0; i<pool->num_servers; i++) {
zval new_stats;
zval *stats;
ZVAL_FALSE(&new_stats);
host_len = spprintf(&host, 0, "%s:%u", pool->servers[i]->host, pool->servers[i]->tcp.port);
stats = zend_symtable_str_update(Z_ARRVAL_P(return_value), host, host_len, &new_stats);
efree(host);
request = mmc_pool_request(pool, MMC_PROTO_TCP, mmc_stats_handler, stats, NULL, NULL);
pool->protocol->stats(request, type, slabid, limit);
if (mmc_pool_schedule(pool, pool->servers[i], request) == MMC_OK) {
mmc_pool_run(pool);
}
}
/* execute all requests */
mmc_pool_run(pool);
}
/* }}} */
/* {{{ proto array memcache_set_compress_threshold( object memcache, int threshold [, float min_savings ] )
Set automatic compress threshold */
PHP_FUNCTION(memcache_set_compress_threshold)
{
mmc_pool_t *pool;
zval *mmc_object = getThis();
zend_long threshold;
double min_savings = MMC_DEFAULT_SAVINGS;
if (mmc_object == NULL) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol|d", &mmc_object, memcache_pool_ce, &threshold, &min_savings) == FAILURE) {
return;
}
}
else {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|d", &threshold, &min_savings) == FAILURE) {
return;
}
}
if (!mmc_get_pool(mmc_object, &pool)) {
RETURN_FALSE;
}
if (threshold < 0) {
php_error_docref(NULL, E_WARNING, "threshold must be a positive integer");
RETURN_FALSE;
}
pool->compress_threshold = threshold;
if (min_savings != MMC_DEFAULT_SAVINGS) {
if (min_savings < 0 || min_savings > 1) {
php_error_docref(NULL, E_WARNING, "min_savings must be a float in the 0..1 range");
RETURN_FALSE;
}
pool->min_compress_savings = min_savings;
}
else {
pool->min_compress_savings = MMC_DEFAULT_SAVINGS;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool memcache_delete(object memcache, mixed key [, int exptime ])
Deletes existing item */
PHP_FUNCTION(memcache_delete)
{
php_mmc_numeric(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, 0);
}
/* }}} */
/* {{{ proto mixed memcache_increment(object memcache, mixed key [, int value [, int defval [, int exptime ] ] ])
Increments existing variable */
PHP_FUNCTION(memcache_increment)
{
php_mmc_numeric(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
}
/* }}} */
/* {{{ proto mixed memcache_decrement(object memcache, mixed key [, int value [, int defval [, int exptime ] ] ])
Decrements existing variable */
PHP_FUNCTION(memcache_decrement)
{
php_mmc_numeric(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1);
}
/* }}} */
/* {{{ proto bool memcache_close( object memcache )
Closes connection to memcached */
PHP_FUNCTION(memcache_close)
{
mmc_pool_t *pool;
zval *mmc_object = getThis();
if (mmc_object == NULL) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &mmc_object, memcache_pool_ce) == FAILURE) {
return;
}
}
if (!mmc_get_pool(mmc_object, &pool)) {
RETURN_FALSE;
}
mmc_pool_close(pool);
RETURN_TRUE;
}
/* }}} */
static int mmc_flush_handler(mmc_t *mmc, mmc_request_t *request, int response, const char *message, unsigned int message_len, void *param) /*
parses the OK response line, param is an int pointer to increment on success {{{ */
{
if (response == MMC_OK) {
(*((int *)param))++;
return MMC_REQUEST_DONE;
}
if (response == MMC_RESPONSE_CLIENT_ERROR) {
ZVAL_FALSE((zval *)param);
php_error_docref(NULL, E_NOTICE,
"Server %s (tcp %d, udp %d) failed with: %s (%d)",
mmc->host, mmc->tcp.port,
mmc->udp.port, message, response);
return MMC_REQUEST_DONE;
}
return mmc_request_failure(mmc, request->io, message, message_len, 0);
}
/* }}} */
/* {{{ proto bool memcache_flush( object memcache [, int delay ] )
Flushes cache, optionally at after the specified delay */
PHP_FUNCTION(memcache_flush)
{
mmc_pool_t *pool;
zval *mmc_object = getThis();
mmc_request_t *request;
unsigned int i, responses = 0;
zend_long delay = 0;
if (mmc_object == NULL) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l", &mmc_object, memcache_pool_ce, &delay) == FAILURE) {
return;
}
}
else {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &delay) == FAILURE) {
return;
}
}
if (!mmc_get_pool(mmc_object, &pool)) {
RETURN_FALSE;
}
for (i=0; i<pool->num_servers; i++) {
request = mmc_pool_request(pool, MMC_PROTO_TCP, mmc_flush_handler, &responses, NULL, NULL);
pool->protocol->flush(request, delay);
if (mmc_pool_schedule(pool, pool->servers[i], request) == MMC_OK) {
/* begin sending requests immediatly */
mmc_pool_select(pool);
}
}
/* execute all requests */
mmc_pool_run(pool);
if (responses < pool->num_servers) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto string memcache_set_sasl_data(object memcache, string username, string password)
Set credentials for sals authentification */
PHP_FUNCTION(memcache_set_sasl_auth_data)
{
zval *mmc_object = getThis();
char *user;
size_t user_length;
char *password;
size_t password_length;
if (mmc_object == NULL) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oss", &mmc_object, memcache_pool_ce, &user, &user_length, &password, &password_length) == FAILURE) {
return;
}
} else {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &user, &user_length, &password, &password_length) == FAILURE) {
return;
}
}
if (user_length < 1 || password_length < 1) {
RETURN_FALSE;
}
zend_update_property_stringl(memcache_pool_ce, mmc_object, "username", strlen("username"), user, user_length);
zend_update_property_stringl(memcache_pool_ce, mmc_object, "password", strlen("password"), password, password_length);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool memcache_debug( bool onoff ) */
PHP_FUNCTION(memcache_debug)
{
php_error_docref(NULL, E_WARNING, "memcache_debug() is deprecated, please use a debugger (like Eclipse + CDT)");
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/
|