Changeset 42343 for trunk/tests/phpunit/includes/object-cache.php
- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/includes/object-cache.php
r41289 r42343 307 307 global $wp_object_cache; 308 308 309 if ( func_num_args() > 4 ) 309 if ( func_num_args() > 4 ) { 310 310 return $wp_object_cache->get( $key, $group, $force, $found, '', false, $cache_cb, $cas_token ); 311 else311 } else { 312 312 return $wp_object_cache->get( $key, $group, $force, $found ); 313 } 313 314 } 314 315 … … 332 333 * @return bool|mixed Cached object value. 333 334 */ 334 function wp_cache_get_by_key( $server_key, $key, $group = '', $force = false, &$found = null, $cache_cb = NULL, &$cas_token = NULL) {335 global $wp_object_cache; 336 337 if ( func_num_args() > 5 ) 335 function wp_cache_get_by_key( $server_key, $key, $group = '', $force = false, &$found = null, $cache_cb = null, &$cas_token = null ) { 336 global $wp_object_cache; 337 338 if ( func_num_args() > 5 ) { 338 339 return $wp_object_cache->getByKey( $server_key, $key, $group, $force, $found, $cache_cb, $cas_token ); 339 else340 } else { 340 341 return $wp_object_cache->getByKey( $server_key, $key, $group, $force, $found ); 342 } 341 343 } 342 344 … … 352 354 * @return bool Returns TRUE on success or FALSE on failure. 353 355 */ 354 function wp_cache_get_delayed( $keys, $groups = '', $with_cas = false, $value_cb = NULL) {356 function wp_cache_get_delayed( $keys, $groups = '', $with_cas = false, $value_cb = null ) { 355 357 global $wp_object_cache; 356 358 return $wp_object_cache->getDelayed( $keys, $groups, $with_cas, $value_cb ); … … 369 371 * @return bool Returns TRUE on success or FALSE on failure. 370 372 */ 371 function wp_cache_get_delayed_by_key( $server_key, $keys, $groups = '', $with_cas = false, $value_cb = NULL) {373 function wp_cache_get_delayed_by_key( $server_key, $keys, $groups = '', $with_cas = false, $value_cb = null ) { 372 374 global $wp_object_cache; 373 375 return $wp_object_cache->getDelayedByKey( $server_key, $keys, $groups, $with_cas, $value_cb ); … … 387 389 * @return bool|array Returns the array of found items or FALSE on failure. 388 390 */ 389 function wp_cache_get_multi( $keys, $groups = '', &$cas_tokens = NULL, $flags = NULL) {390 global $wp_object_cache; 391 392 if ( func_num_args() > 2 ) 391 function wp_cache_get_multi( $keys, $groups = '', &$cas_tokens = null, $flags = null ) { 392 global $wp_object_cache; 393 394 if ( func_num_args() > 2 ) { 393 395 return $wp_object_cache->getMulti( $keys, $groups, '', $cas_tokens, $flags ); 394 else396 } else { 395 397 return $wp_object_cache->getMulti( $keys, $groups ); 398 } 396 399 } 397 400 … … 410 413 * @return bool|array Returns the array of found items or FALSE on failure. 411 414 */ 412 function wp_cache_get_multi_by_key( $server_key, $keys, $groups = '', &$cas_tokens = NULL, $flags = NULL) {413 global $wp_object_cache; 414 415 if ( func_num_args() > 3 ) 415 function wp_cache_get_multi_by_key( $server_key, $keys, $groups = '', &$cas_tokens = null, $flags = null ) { 416 global $wp_object_cache; 417 418 if ( func_num_args() > 3 ) { 416 419 return $wp_object_cache->getMultiByKey( $server_key, $keys, $groups, $cas_tokens, $flags ); 417 else420 } else { 418 421 return $wp_object_cache->getMultiByKey( $server_key, $keys, $groups ); 422 } 419 423 } 420 424 … … 819 823 * @param null $persistent_id To create an instance that persists between requests, use persistent_id to specify a unique ID for the instance. 820 824 */ 821 public function __construct( $persistent_id = NULL) {825 public function __construct( $persistent_id = null ) { 822 826 global $memcached_servers, $blog_id, $table_prefix; 823 827 824 if ( is_null( $persistent_id ) || ! is_string( $persistent_id ) ) 828 if ( is_null( $persistent_id ) || ! is_string( $persistent_id ) ) { 825 829 $this->m = new Memcached(); 826 else830 } else { 827 831 $this->m = new Memcached( $persistent_id ); 828 829 if ( isset( $memcached_servers ) ) 832 } 833 834 if ( isset( $memcached_servers ) ) { 830 835 $this->servers = $memcached_servers; 831 else836 } else { 832 837 $this->servers = array( array( '127.0.0.1', 11211 ) ); 838 } 833 839 834 840 $this->addServers( $this->servers ); … … 838 844 * multi single WP installations on the same server. 839 845 */ 840 if ( ! defined( 'WP_CACHE_KEY_SALT' ) ) 846 if ( ! defined( 'WP_CACHE_KEY_SALT' ) ) { 841 847 define( 'WP_CACHE_KEY_SALT', '' ); 848 } 842 849 843 850 // Assign global and blog prefixes for use with keys 844 851 if ( function_exists( 'is_multisite' ) ) { 845 852 $this->global_prefix = ( is_multisite() || defined( 'CUSTOM_USER_TABLE' ) && defined( 'CUSTOM_USER_META_TABLE' ) ) ? '' : $table_prefix; 846 $this->blog_prefix = ( is_multisite() ? $blog_id : $table_prefix ) . ':';853 $this->blog_prefix = ( is_multisite() ? $blog_id : $table_prefix ) . ':'; 847 854 } 848 855 … … 886 893 887 894 // Add does not set the value if the key exists; mimic that here 888 if ( isset( $this->cache[ $derived_key] ) )895 if ( isset( $this->cache[ $derived_key ] ) ) { 889 896 return false; 897 } 890 898 891 899 $this->add_to_internal_cache( $derived_key, $value ); … … 895 903 896 904 // Save to Memcached 897 if ( $byKey ) 905 if ( $byKey ) { 898 906 $result = $this->m->addByKey( $server_key, $derived_key, $value, $expiration ); 899 else907 } else { 900 908 $result = $this->m->add( $derived_key, $value, $expiration ); 909 } 901 910 902 911 // Store in runtime cache if add was successful 903 if ( Memcached::RES_SUCCESS === $this->getResultCode() ) 912 if ( Memcached::RES_SUCCESS === $this->getResultCode() ) { 904 913 $this->add_to_internal_cache( $derived_key, $value ); 914 } 905 915 906 916 return $result; … … 939 949 */ 940 950 public function addServer( $host, $port, $weight = 0 ) { 941 $host = is_string( $host ) ? $host : '127.0.0.1';942 $port = is_numeric( $port ) && $port > 0 ? $port : 11211;951 $host = is_string( $host ) ? $host : '127.0.0.1'; 952 $port = is_numeric( $port ) && $port > 0 ? $port : 11211; 943 953 $weight = is_numeric( $weight ) && $weight > 0 ? $weight : 1; 944 954 … … 958 968 */ 959 969 public function addServers( $servers ) { 960 if ( ! is_object( $this->m ) ) 970 if ( ! is_object( $this->m ) ) { 961 971 return false; 972 } 962 973 963 974 return $this->m->addServers( $servers ); … … 984 995 */ 985 996 public function append( $key, $value, $group = 'default', $server_key = '', $byKey = false ) { 986 if ( ! is_string( $value ) && ! is_int( $value ) && ! is_float( $value ) ) 997 if ( ! is_string( $value ) && ! is_int( $value ) && ! is_float( $value ) ) { 987 998 return false; 999 } 988 1000 989 1001 $derived_key = $this->buildKey( $key, $group ); … … 991 1003 // If group is a non-Memcached group, append to runtime cache value, not Memcached 992 1004 if ( in_array( $group, $this->no_mc_groups ) ) { 993 if ( ! isset( $this->cache[ $derived_key] ) )1005 if ( ! isset( $this->cache[ $derived_key ] ) ) { 994 1006 return false; 995 996 $combined = $this->combine_values( $this->cache[$derived_key], $value, 'app' ); 1007 } 1008 1009 $combined = $this->combine_values( $this->cache[ $derived_key ], $value, 'app' ); 997 1010 $this->add_to_internal_cache( $derived_key, $combined ); 998 1011 return true; … … 1000 1013 1001 1014 // Append to Memcached value 1002 if ( $byKey ) 1015 if ( $byKey ) { 1003 1016 $result = $this->m->appendByKey( $server_key, $derived_key, $value ); 1004 else1017 } else { 1005 1018 $result = $this->m->append( $derived_key, $value ); 1019 } 1006 1020 1007 1021 // Store in runtime cache if add was successful 1008 1022 if ( Memcached::RES_SUCCESS === $this->getResultCode() ) { 1009 $combined = $this->combine_values( $this->cache[ $derived_key], $value, 'app' );1023 $combined = $this->combine_values( $this->cache[ $derived_key ], $value, 'app' ); 1010 1024 $this->add_to_internal_cache( $derived_key, $combined ); 1011 1025 } … … 1068 1082 1069 1083 // Save to Memcached 1070 if ( $byKey ) 1084 if ( $byKey ) { 1071 1085 $result = $this->m->casByKey( $cas_token, $server_key, $derived_key, $value, $expiration ); 1072 else1086 } else { 1073 1087 $result = $this->m->cas( $cas_token, $derived_key, $value, $expiration ); 1088 } 1074 1089 1075 1090 // Store in runtime cache if cas was successful 1076 if ( Memcached::RES_SUCCESS === $this->getResultCode() ) 1091 if ( Memcached::RES_SUCCESS === $this->getResultCode() ) { 1077 1092 $this->add_to_internal_cache( $derived_key, $value ); 1093 } 1078 1094 1079 1095 return $result; … … 1117 1133 1118 1134 // Only decrement if the key already exists and value is 0 or greater (mimics memcached behavior) 1119 if ( isset( $this->cache[ $derived_key] ) && $this->cache[$derived_key] >= 0 ) {1135 if ( isset( $this->cache[ $derived_key ] ) && $this->cache[ $derived_key ] >= 0 ) { 1120 1136 1121 1137 // If numeric, subtract; otherwise, consider it 0 and do nothing 1122 if ( is_numeric( $this->cache[$derived_key] ) ) 1123 $this->cache[$derived_key] -= (int) $offset; 1124 else 1125 $this->cache[$derived_key] = 0; 1138 if ( is_numeric( $this->cache[ $derived_key ] ) ) { 1139 $this->cache[ $derived_key ] -= (int) $offset; 1140 } else { 1141 $this->cache[ $derived_key ] = 0; 1142 } 1126 1143 1127 1144 // Returned value cannot be less than 0 1128 if ( $this->cache[$derived_key] < 0 ) 1129 $this->cache[$derived_key] = 0; 1130 1131 return $this->cache[$derived_key]; 1145 if ( $this->cache[ $derived_key ] < 0 ) { 1146 $this->cache[ $derived_key ] = 0; 1147 } 1148 1149 return $this->cache[ $derived_key ]; 1132 1150 } else { 1133 1151 return false; … … 1137 1155 $result = $this->m->decrement( $derived_key, $offset ); 1138 1156 1139 if ( Memcached::RES_SUCCESS === $this->getResultCode() ) 1157 if ( Memcached::RES_SUCCESS === $this->getResultCode() ) { 1140 1158 $this->add_to_internal_cache( $derived_key, $result ); 1159 } 1141 1160 1142 1161 return $result; … … 1180 1199 // Remove from no_mc_groups array 1181 1200 if ( in_array( $group, $this->no_mc_groups ) ) { 1182 if ( isset( $this->cache[$derived_key] ) ) 1183 unset( $this->cache[$derived_key] ); 1201 if ( isset( $this->cache[ $derived_key ] ) ) { 1202 unset( $this->cache[ $derived_key ] ); 1203 } 1184 1204 1185 1205 return true; 1186 1206 } 1187 1207 1188 if ( $byKey ) 1208 if ( $byKey ) { 1189 1209 $result = $this->m->deleteByKey( $server_key, $derived_key, $time ); 1190 else1210 } else { 1191 1211 $result = $this->m->delete( $derived_key, $time ); 1192 1193 if ( Memcached::RES_SUCCESS === $this->getResultCode() ) 1194 unset( $this->cache[$derived_key] ); 1212 } 1213 1214 if ( Memcached::RES_SUCCESS === $this->getResultCode() ) { 1215 unset( $this->cache[ $derived_key ] ); 1216 } 1195 1217 1196 1218 return $result; … … 1236 1258 */ 1237 1259 public function fetchAll() { 1238 1260 return $this->m->fetchAll(); 1239 1261 } 1240 1262 … … 1251 1273 1252 1274 // Only reset the runtime cache if memcached was properly flushed 1253 if ( Memcached::RES_SUCCESS === $this->getResultCode() ) 1275 if ( Memcached::RES_SUCCESS === $this->getResultCode() ) { 1254 1276 $this->cache = array(); 1277 } 1255 1278 1256 1279 return $result; … … 1281 1304 * @return bool|mixed Cached object value. 1282 1305 */ 1283 public function get( $key, $group = 'default', $force = false, &$found = null, $server_key = '', $byKey = false, $cache_cb = NULL, &$cas_token = NULL) {1306 public function get( $key, $group = 'default', $force = false, &$found = null, $server_key = '', $byKey = false, $cache_cb = null, &$cas_token = null ) { 1284 1307 $derived_key = $this->buildKey( $key, $group ); 1285 1308 … … 1289 1312 // If either $cache_db, or $cas_token is set, must hit Memcached and bypass runtime cache 1290 1313 if ( func_num_args() > 6 && ! in_array( $group, $this->no_mc_groups ) ) { 1291 if ( $byKey ) 1314 if ( $byKey ) { 1292 1315 $value = $this->m->getByKey( $server_key, $derived_key, $cache_cb, $cas_token ); 1293 else1316 } else { 1294 1317 $value = $this->m->get( $derived_key, $cache_cb, $cas_token ); 1318 } 1295 1319 } else { 1296 if ( isset( $this->cache[ $derived_key] ) ) {1320 if ( isset( $this->cache[ $derived_key ] ) ) { 1297 1321 $found = true; 1298 return is_object( $this->cache[ $derived_key] ) ? clone $this->cache[$derived_key] : $this->cache[$derived_key];1322 return is_object( $this->cache[ $derived_key ] ) ? clone $this->cache[ $derived_key ] : $this->cache[ $derived_key ]; 1299 1323 } elseif ( in_array( $group, $this->no_mc_groups ) ) { 1300 1324 return false; 1301 1325 } else { 1302 if ( $byKey ) 1326 if ( $byKey ) { 1303 1327 $value = $this->m->getByKey( $server_key, $derived_key ); 1304 else1328 } else { 1305 1329 $value = $this->m->get( $derived_key ); 1330 } 1306 1331 } 1307 1332 } … … 1338 1363 * @return bool|mixed Cached object value. 1339 1364 */ 1340 public function getByKey( $server_key, $key, $group = 'default', $force = false, &$found = null, $cache_cb = NULL, &$cas_token = NULL) {1365 public function getByKey( $server_key, $key, $group = 'default', $force = false, &$found = null, $cache_cb = null, &$cas_token = null ) { 1341 1366 /** 1342 1367 * Need to be careful how "get" is called. If you send $cache_cb, and $cas_token, it will hit memcached. 1343 1368 * Only send those args if they were sent to this function. 1344 1369 */ 1345 if ( func_num_args() > 5 ) 1370 if ( func_num_args() > 5 ) { 1346 1371 return $this->get( $key, $group, $force, $found, $server_key, true, $cache_cb, $cas_token ); 1347 else1372 } else { 1348 1373 return $this->get( $key, $group, $force, $found, $server_key, true ); 1374 } 1349 1375 } 1350 1376 … … 1360 1386 * @return bool Returns TRUE on success or FALSE on failure. 1361 1387 */ 1362 public function getDelayed( $keys, $groups = 'default', $with_cas = false, $value_cb = NULL) {1388 public function getDelayed( $keys, $groups = 'default', $with_cas = false, $value_cb = null ) { 1363 1389 $derived_keys = $this->buildKeys( $keys, $groups ); 1364 1390 return $this->m->getDelayed( $derived_keys, $with_cas, $value_cb ); … … 1377 1403 * @return bool Returns TRUE on success or FALSE on failure. 1378 1404 */ 1379 public function getDelayedByKey( $server_key, $keys, $groups = 'default', $with_cas = false, $value_cb = NULL) {1405 public function getDelayedByKey( $server_key, $keys, $groups = 'default', $with_cas = false, $value_cb = null ) { 1380 1406 $derived_keys = $this->buildKeys( $keys, $groups ); 1381 1407 return $this->m->getDelayedByKey( $server_key, $derived_keys, $with_cas, $value_cb ); … … 1396 1422 * @return bool|array Returns the array of found items or FALSE on failure. 1397 1423 */ 1398 public function getMulti( $keys, $groups = 'default', $server_key = '', &$cas_tokens = NULL, $flags = NULL) {1424 public function getMulti( $keys, $groups = 'default', $server_key = '', &$cas_tokens = null, $flags = null ) { 1399 1425 $derived_keys = $this->buildKeys( $keys, $groups ); 1400 1426 … … 1405 1431 */ 1406 1432 if ( func_num_args() > 3 && ! $this->contains_no_mc_group( $groups ) ) { 1407 if ( ! empty( $server_key ) ) 1433 if ( ! empty( $server_key ) ) { 1408 1434 $values = $this->m->getMultiByKey( $server_key, $derived_keys, $cas_tokens, $flags ); 1409 else1435 } else { 1410 1436 $values = $this->m->getMulti( $derived_keys, $cas_tokens, $flags ); 1437 } 1411 1438 } else { 1412 $values = array();1439 $values = array(); 1413 1440 $need_to_get = array(); 1414 1441 1415 1442 // Pull out values from runtime cache, or mark for retrieval 1416 1443 foreach ( $derived_keys as $key ) { 1417 if ( isset( $this->cache[$key] ) ) 1418 $values[$key] = $this->cache[$key]; 1419 else 1420 $need_to_get[$key] = $key; 1444 if ( isset( $this->cache[ $key ] ) ) { 1445 $values[ $key ] = $this->cache[ $key ]; 1446 } else { 1447 $need_to_get[ $key ] = $key; 1448 } 1421 1449 } 1422 1450 1423 1451 // Get those keys not found in the runtime cache 1424 1452 if ( ! empty( $need_to_get ) ) { 1425 if ( ! empty( $server_key ) ) 1453 if ( ! empty( $server_key ) ) { 1426 1454 $result = $this->m->getMultiByKey( $server_key, array_keys( $need_to_get ) ); 1427 else1455 } else { 1428 1456 $result = $this->m->getMulti( array_keys( $need_to_get ) ); 1457 } 1429 1458 } 1430 1459 1431 1460 // Merge with values found in runtime cache 1432 if ( isset( $result ) && Memcached::RES_SUCCESS === $this->getResultCode() ) 1461 if ( isset( $result ) && Memcached::RES_SUCCESS === $this->getResultCode() ) { 1433 1462 $values = array_merge( $values, $result ); 1463 } 1434 1464 1435 1465 // If order should be preserved, reorder now … … 1438 1468 1439 1469 foreach ( $derived_keys as $key ) { 1440 if ( isset( $values[$key] ) ) 1441 $ordered_values[$key] = $values[$key]; 1470 if ( isset( $values[ $key ] ) ) { 1471 $ordered_values[ $key ] = $values[ $key ]; 1472 } 1442 1473 } 1443 1474 … … 1467 1498 * @return bool|array Returns the array of found items or FALSE on failure. 1468 1499 */ 1469 public function getMultiByKey( $server_key, $keys, $groups = 'default', &$cas_tokens = NULL, $flags = NULL) {1500 public function getMultiByKey( $server_key, $keys, $groups = 'default', &$cas_tokens = null, $flags = null ) { 1470 1501 /** 1471 1502 * Need to be careful how "getMulti" is called. If you send $cache_cb, and $cas_token, it will hit memcached. 1472 1503 * Only send those args if they were sent to this function. 1473 1504 */ 1474 if ( func_num_args() > 3 ) 1505 if ( func_num_args() > 3 ) { 1475 1506 return $this->getMulti( $keys, $groups, $server_key, $cas_tokens, $flags ); 1476 else1507 } else { 1477 1508 return $this->getMulti( $keys, $groups, $server_key ); 1509 } 1478 1510 } 1479 1511 … … 1498 1530 */ 1499 1531 public function getResultCode() { 1500 1532 return $this->m->getResultCode(); 1501 1533 } 1502 1534 … … 1509 1541 */ 1510 1542 public function getResultMessage() { 1511 1543 return $this->m->getResultMessage(); 1512 1544 } 1513 1545 … … 1536 1568 1537 1569 /** 1538 1570 * Get server pool statistics. 1539 1571 * 1540 1572 * @link http://www.php.net/manual/en/memcached.getstats.php … … 1574 1606 1575 1607 // Only increment if the key already exists and the number is currently 0 or greater (mimics memcached behavior) 1576 if ( isset( $this->cache[ $derived_key] ) && $this->cache[$derived_key] >= 0 ) {1608 if ( isset( $this->cache[ $derived_key ] ) && $this->cache[ $derived_key ] >= 0 ) { 1577 1609 1578 1610 // If numeric, add; otherwise, consider it 0 and do nothing 1579 if ( is_numeric( $this->cache[$derived_key] ) ) 1580 $this->cache[$derived_key] += (int) $offset; 1581 else 1582 $this->cache[$derived_key] = 0; 1611 if ( is_numeric( $this->cache[ $derived_key ] ) ) { 1612 $this->cache[ $derived_key ] += (int) $offset; 1613 } else { 1614 $this->cache[ $derived_key ] = 0; 1615 } 1583 1616 1584 1617 // Returned value cannot be less than 0 1585 if ( $this->cache[$derived_key] < 0 ) 1586 $this->cache[$derived_key] = 0; 1587 1588 return $this->cache[$derived_key]; 1618 if ( $this->cache[ $derived_key ] < 0 ) { 1619 $this->cache[ $derived_key ] = 0; 1620 } 1621 1622 return $this->cache[ $derived_key ]; 1589 1623 } else { 1590 1624 return false; … … 1594 1628 $result = $this->m->increment( $derived_key, $offset ); 1595 1629 1596 if ( Memcached::RES_SUCCESS === $this->getResultCode() ) 1630 if ( Memcached::RES_SUCCESS === $this->getResultCode() ) { 1597 1631 $this->add_to_internal_cache( $derived_key, $result ); 1632 } 1598 1633 1599 1634 return $result; … … 1637 1672 */ 1638 1673 public function prepend( $key, $value, $group = 'default', $server_key = '', $byKey = false ) { 1639 if ( ! is_string( $value ) && ! is_int( $value ) && ! is_float( $value ) ) 1674 if ( ! is_string( $value ) && ! is_int( $value ) && ! is_float( $value ) ) { 1640 1675 return false; 1676 } 1641 1677 1642 1678 $derived_key = $this->buildKey( $key, $group ); … … 1644 1680 // If group is a non-Memcached group, prepend to runtime cache value, not Memcached 1645 1681 if ( in_array( $group, $this->no_mc_groups ) ) { 1646 if ( ! isset( $this->cache[ $derived_key] ) )1682 if ( ! isset( $this->cache[ $derived_key ] ) ) { 1647 1683 return false; 1648 1649 $combined = $this->combine_values( $this->cache[$derived_key], $value, 'pre' ); 1684 } 1685 1686 $combined = $this->combine_values( $this->cache[ $derived_key ], $value, 'pre' ); 1650 1687 $this->add_to_internal_cache( $derived_key, $combined ); 1651 1688 return true; … … 1653 1690 1654 1691 // Append to Memcached value 1655 if ( $byKey ) 1692 if ( $byKey ) { 1656 1693 $result = $this->m->prependByKey( $server_key, $derived_key, $value ); 1657 else1694 } else { 1658 1695 $result = $this->m->prepend( $derived_key, $value ); 1696 } 1659 1697 1660 1698 // Store in runtime cache if add was successful 1661 1699 if ( Memcached::RES_SUCCESS === $this->getResultCode() ) { 1662 $combined = $this->combine_values( $this->cache[ $derived_key], $value, 'pre' );1700 $combined = $this->combine_values( $this->cache[ $derived_key ], $value, 'pre' ); 1663 1701 $this->add_to_internal_cache( $derived_key, $combined ); 1664 1702 } … … 1714 1752 1715 1753 // Replace won't save unless the key already exists; mimic this behavior here 1716 if ( ! isset( $this->cache[ $derived_key] ) )1754 if ( ! isset( $this->cache[ $derived_key ] ) ) { 1717 1755 return false; 1718 1719 $this->cache[$derived_key] = $value; 1756 } 1757 1758 $this->cache[ $derived_key ] = $value; 1720 1759 return true; 1721 1760 } 1722 1761 1723 1762 // Save to Memcached 1724 if ( $byKey ) 1763 if ( $byKey ) { 1725 1764 $result = $this->m->replaceByKey( $server_key, $derived_key, $value, $expiration ); 1726 else1765 } else { 1727 1766 $result = $this->m->replace( $derived_key, $value, $expiration ); 1767 } 1728 1768 1729 1769 // Store in runtime cache if add was successful 1730 if ( Memcached::RES_SUCCESS === $this->getResultCode() ) 1770 if ( Memcached::RES_SUCCESS === $this->getResultCode() ) { 1731 1771 $this->add_to_internal_cache( $derived_key, $value ); 1772 } 1732 1773 1733 1774 return $result; … … 1786 1827 1787 1828 // Store in runtime cache if add was successful 1788 if ( Memcached::RES_SUCCESS === $this->getResultCode() ) 1829 if ( Memcached::RES_SUCCESS === $this->getResultCode() ) { 1789 1830 $this->add_to_internal_cache( $derived_key, $value ); 1831 } 1790 1832 1791 1833 return $result; … … 1843 1885 if ( in_array( $key_pieces[1], $this->no_mc_groups ) ) { 1844 1886 $this->add_to_internal_cache( $derived_key, $value ); 1845 unset( $derived_items[ $derived_key] );1887 unset( $derived_items[ $derived_key ] ); 1846 1888 } 1847 1889 } 1848 1890 1849 1891 // Save to memcached 1850 if ( $byKey ) 1892 if ( $byKey ) { 1851 1893 $result = $this->m->setMultiByKey( $server_key, $derived_items, $expiration ); 1852 else1894 } else { 1853 1895 $result = $this->m->setMulti( $derived_items, $expiration ); 1896 } 1854 1897 1855 1898 // Store in runtime cache if add was successful 1856 if ( Memcached::RES_SUCCESS === $this->getResultCode() ) 1899 if ( Memcached::RES_SUCCESS === $this->getResultCode() ) { 1857 1900 $this->cache = array_merge( $this->cache, $derived_items ); 1901 } 1858 1902 1859 1903 return $result; … … 1905 1949 */ 1906 1950 public function buildKey( $key, $group = 'default' ) { 1907 if ( empty( $group ) ) 1951 if ( empty( $group ) ) { 1908 1952 $group = 'default'; 1909 1910 if ( false !== array_search( $group, $this->global_groups ) ) 1953 } 1954 1955 if ( false !== array_search( $group, $this->global_groups ) ) { 1911 1956 $prefix = $this->global_prefix; 1912 else1957 } else { 1913 1958 $prefix = $this->blog_prefix; 1959 } 1914 1960 1915 1961 return preg_replace( '/\s+/', '', WP_CACHE_KEY_SALT . "$prefix$group:$key" ); … … 1936 1982 1937 1983 // If strings sent, convert to arrays for proper handling 1938 if ( ! is_array( $groups ) ) 1984 if ( ! is_array( $groups ) ) { 1939 1985 $groups = (array) $groups; 1940 1941 if ( ! is_array( $keys ) ) 1986 } 1987 1988 if ( ! is_array( $keys ) ) { 1942 1989 $keys = (array) $keys; 1990 } 1943 1991 1944 1992 // If we have equal numbers of keys and groups, merge $keys[n] and $group[n] 1945 1993 if ( count( $keys ) == count( $groups ) ) { 1946 1994 for ( $i = 0; $i < count( $keys ); $i++ ) { 1947 $derived_keys[] = $this->buildKey( $keys[ $i], $groups[$i] );1995 $derived_keys[] = $this->buildKey( $keys[ $i ], $groups[ $i ] ); 1948 1996 } 1949 1997 1950 // If more keys are received than groups, merge $keys[n] and $group[n] until no more group are left; remaining groups are 'default'1998 // If more keys are received than groups, merge $keys[n] and $group[n] until no more group are left; remaining groups are 'default' 1951 1999 } elseif ( count( $keys ) > count( $groups ) ) { 1952 2000 for ( $i = 0; $i < count( $keys ); $i++ ) { 1953 if ( isset( $groups[$i] ) ) 1954 $derived_keys[] = $this->buildKey( $keys[$i], $groups[$i] ); 1955 elseif ( count( $groups ) == 1 ) 1956 $derived_keys[] = $this->buildKey( $keys[$i], $groups[0] ); 1957 else 1958 $derived_keys[] = $this->buildKey( $keys[$i], 'default' ); 2001 if ( isset( $groups[ $i ] ) ) { 2002 $derived_keys[] = $this->buildKey( $keys[ $i ], $groups[ $i ] ); 2003 } elseif ( count( $groups ) == 1 ) { 2004 $derived_keys[] = $this->buildKey( $keys[ $i ], $groups[0] ); 2005 } else { 2006 $derived_keys[] = $this->buildKey( $keys[ $i ], 'default' ); 2007 } 1959 2008 } 1960 2009 } … … 1997 2046 1998 2047 // Combine the values based on direction of the "pend" 1999 if ( 'pre' == $direction ) 2048 if ( 'pre' == $direction ) { 2000 2049 $combined = $pended . $original; 2001 else2050 } else { 2002 2051 $combined = $original . $pended; 2052 } 2003 2053 2004 2054 // Cast type of combined value … … 2019 2069 } 2020 2070 2021 $this->cache[ $derived_key] = $value;2071 $this->cache[ $derived_key ] = $value; 2022 2072 } 2023 2073 … … 2029 2079 */ 2030 2080 public function contains_no_mc_group( $groups ) { 2031 if ( is_scalar( $groups ) ) 2081 if ( is_scalar( $groups ) ) { 2032 2082 return in_array( $groups, $this->no_mc_groups ); 2033 2034 if ( ! is_array( $groups ) ) 2083 } 2084 2085 if ( ! is_array( $groups ) ) { 2035 2086 return false; 2087 } 2036 2088 2037 2089 foreach ( $groups as $group ) { 2038 if ( in_array( $group, $this->no_mc_groups ) ) 2090 if ( in_array( $group, $this->no_mc_groups ) ) { 2039 2091 return true; 2092 } 2040 2093 } 2041 2094 … … 2053 2106 */ 2054 2107 public function add_global_groups( $groups ) { 2055 if ( ! is_array( $groups ) ) 2108 if ( ! is_array( $groups ) ) { 2056 2109 $groups = (array) $groups; 2057 2058 $this->global_groups = array_merge( $this->global_groups, $groups); 2110 } 2111 2112 $this->global_groups = array_merge( $this->global_groups, $groups ); 2059 2113 $this->global_groups = array_unique( $this->global_groups ); 2060 2114 } … … 2070 2124 */ 2071 2125 public function add_non_persistent_groups( $groups ) { 2072 if ( ! is_array( $groups ) ) 2126 if ( ! is_array( $groups ) ) { 2073 2127 $groups = (array) $groups; 2128 } 2074 2129 2075 2130 $this->no_mc_groups = array_merge( $this->no_mc_groups, $groups ); … … 2087 2142 $derived_key = $this->buildKey( $key, $group ); 2088 2143 2089 if ( isset( $this->cache[$derived_key] ) ) 2090 return $this->cache[$derived_key]; 2144 if ( isset( $this->cache[ $derived_key ] ) ) { 2145 return $this->cache[ $derived_key ]; 2146 } 2091 2147 2092 2148 return false;
Note: See TracChangeset
for help on using the changeset viewer.