1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
|
<?php
/**
* Adds a value to cache.
*
* If the specified key already exists, the value is not stored and the function
* returns false.
*
* @link https://www.php.net/manual/en/memcached.add.php
*
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
* @return bool True on success, false on failure.
*/
function wp_cache_add( $key, $value, $group = '', $expiration = 0 ) {
global $wp_object_cache;
return $wp_object_cache->add( $key, $value, $group, $expiration );
}
/**
* Adds a value to cache on a specific server.
*
* Using a server_key value, the object can be stored on a specified server as opposed
* to a random server in the stack. Note that this method will add the key/value to the
* _cache object as part of the runtime cache. It will add it to an array for the
* specified server_key.
*
* @link https://www.php.net/manual/en/memcached.addbykey.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
* @return bool True on success, false on failure.
*/
function wp_cache_add_by_key( $server_key, $key, $value, $group = '', $expiration = 0 ) {
global $wp_object_cache;
return $wp_object_cache->addByKey( $server_key, $key, $value, $group, $expiration );
}
/**
* Adds multiple values to the cache in one call, if the cache keys don't already exist.
*
* @param array $items Array of keys and values to be added.
* @param string $group Optional. Where the cache contents are grouped. Default empty.
* @param int $expiration Optional. When to expire the cache contents, in seconds.
* Default 0 (no expiration).
* @return bool[] Array of return values, grouped by key. Each value is either
* true on success, or false if cache key and group already exist.
*/
function wp_cache_add_multiple( array $items, $group = '', $expiration = 0 ) {
global $wp_object_cache;
return $wp_object_cache->addMultiple( $items, $group, $expiration );
}
/**
* Adds a single server to the list of Memcached servers.
*
* @link https://www.php.net/manual/en/memcached.addserver.php
*
* @param string $host The hostname of the memcache server.
* @param int $port The port on which memcache is running.
* @param int $weight The weight of the server relative to the total weight
* of all the servers in the pool.
* @return bool True on success, false on failure.
*/
function wp_cache_add_server( $host, $port, $weight = 0 ) {
global $wp_object_cache;
return $wp_object_cache->addServer( $host, $port, $weight );
}
/**
* Adds an array of servers to the pool.
*
* Each individual server in the array must include a domain and port, with an optional
* weight value: $servers = array( array( '127.0.0.1', 11211, 0 ) );
*
* @link https://www.php.net/manual/en/memcached.addservers.php
*
* @param array $servers Array of server to register.
* @return bool True on success, false on failure.
*/
function wp_cache_add_servers( $servers ) {
global $wp_object_cache;
return $wp_object_cache->addServers( $servers );
}
/**
* Appends data to an existing item.
*
* This method should throw an error if it is used with compressed data.
* This is an expected behavior. Memcached casts the value to be appended to the initial value
* to the type of the initial value. Be careful as this leads to unexpected behavior at times.
* Due to how memcached treats types, the behavior has been mimicked in the internal cache to produce
* similar results and improve consistency. It is recommended that appends only occur with data of
* the same type.
*
* @link https://www.php.net/manual/en/memcached.append.php
*
* @param string $key The key under which to store the value.
* @param mixed $value Must be string as appending mixed values is not well-defined.
* @param string $group The group value appended to the $key.
* @return bool True on success, false on failure.
*/
function wp_cache_append( $key, $value, $group = '' ) {
global $wp_object_cache;
return $wp_object_cache->append( $key, $value, $group );
}
/**
* Appends data to an existing item by server key.
*
* This method should throw an error if it is used with compressed data.
* This is an expected behavior. Memcached casts the value to be appended to the initial value
* to the type of the initial value. Be careful as this leads to unexpected behavior at times.
* Due to how memcached treats types, the behavior has been mimicked in the internal cache to produce
* similar results and improve consistency. It is recommended that appends only occur with data of
* the same type.
*
* @link https://www.php.net/manual/en/memcached.appendbykey.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param string $key The key under which to store the value.
* @param mixed $value Must be string as appending mixed values is not well-defined.
* @param string $group The group value appended to the $key.
* @return bool True on success, false on failure.
*/
function wp_cache_append_by_key( $server_key, $key, $value, $group = '' ) {
global $wp_object_cache;
return $wp_object_cache->appendByKey( $server_key, $key, $value, $group );
}
/**
* Performs a "check and set" to store data.
*
* The set will be successful only if the no other request has updated the value
* since it was fetched by this request.
*
* @link https://www.php.net/manual/en/memcached.cas.php
*
* @param float $cas_token Unique value associated with the existing item. Generated by memcached.
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
* @return bool True on success, false on failure.
*/
function wp_cache_cas( $cas_token, $key, $value, $group = '', $expiration = 0 ) {
global $wp_object_cache;
return $wp_object_cache->cas( $cas_token, $key, $value, $group, $expiration );
}
/**
* Performs a "check and set" to store data with a server key.
*
* The set will be successful only if the no other request has updated the value
* since it was fetched by this request.
*
* @link https://www.php.net/manual/en/memcached.casbykey.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param float $cas_token Unique value associated with the existing item. Generated by memcached.
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
* @return bool True on success, false on failure.
*/
function wp_cache_cas_by_key( $cas_token, $server_key, $key, $value, $group = '', $expiration = 0 ) {
global $wp_object_cache;
return $wp_object_cache->casByKey( $cas_token, $server_key, $key, $value, $group, $expiration );
}
/**
* Closes the cache.
*
* This function has ceased to do anything since WordPress 2.5.
* The functionality was removed along with the rest of the persistent cache.
* This does not mean that plugins can't implement this function when they need
* to make sure that the cache is cleaned up after WordPress no longer needs it.
*
* @since 2.0.0
*
* @return bool Always returns true.
*/
function wp_cache_close() {
return true;
}
/**
* Decrements a numeric item's value.
*
* @link https://www.php.net/manual/en/memcached.decrement.php
*
* @param string $key The key under which to store the value.
* @param int $offset The amount by which to decrement the item's value.
* @param string $group The group value appended to the $key.
* @return int|bool Item's new value on success, false on failure.
*/
function wp_cache_decrement( $key, $offset = 1, $group = '' ) {
global $wp_object_cache;
return $wp_object_cache->decrement( $key, $offset, $group );
}
/**
* Decrements a numeric item's value.
*
* This is the same as wp_cache_decrement(), but kept for backward compatibility.
* The original WordPress caching backends use wp_cache_decr().
*
* @link https://www.php.net/manual/en/memcached.decrement.php
*
* @param string $key The key under which to store the value.
* @param int $offset The amount by which to decrement the item's value.
* @param string $group The group value appended to the $key.
* @return int|bool Item's new value on success, false on failure.
*/
function wp_cache_decr( $key, $offset = 1, $group = '' ) {
return wp_cache_decrement( $key, $offset, $group );
}
/**
* Removes the item from the cache.
*
* Removes an item from memcached with identified by $key after $time seconds.
* The $time parameter allows an object to be queued for deletion without
* immediately deleting. Between the time that it is queued and the time it's deleted,
* add, replace, and get will fail, but set will succeed.
*
* @link https://www.php.net/manual/en/memcached.delete.php
*
* @param string $key The key under which to store the value.
* @param string $group The group value appended to the $key.
* @param int $time The amount of time the server will wait to delete the item in seconds.
* @return bool True on success, false on failure.
*/
function wp_cache_delete( $key, $group = '', $time = 0 ) {
global $wp_object_cache;
return $wp_object_cache->delete( $key, $group, $time );
}
/**
* Removes the item from the cache by server key.
*
* Removes an item from memcached with identified by $key after $time seconds.
* The $time parameter allows an object to be queued for deletion without
* immediately deleting. Between the time that it is queued and the time it's deleted,
* add, replace, and get will fail, but set will succeed.
*
* @link https://www.php.net/manual/en/memcached.deletebykey.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param string $key The key under which to store the value.
* @param string $group The group value appended to the $key.
* @param int $time The amount of time the server will wait to delete the item in seconds.
* @return bool True on success, false on failure.
*/
function wp_cache_delete_by_key( $server_key, $key, $group = '', $time = 0 ) {
global $wp_object_cache;
return $wp_object_cache->deleteByKey( $server_key, $key, $group, $time );
}
/**
* Deletes multiple values from the cache in one call.
*
* @param array $keys Array of keys under which the cache to deleted.
* @param string $group Optional. Where the cache contents are grouped. Default empty.
* @return bool[] Array of return values, grouped by key. Each value is either
* true on success, or false if the contents were not deleted.
*/
function wp_cache_delete_multiple( array $keys, $group = '' ) {
global $wp_object_cache;
return $wp_object_cache->deleteMultiple( $keys, $group );
}
/**
* Fetches the next result.
*
* @link https://www.php.net/manual/en/memcached.fetch.php
*
* @return array|false The next result on success, false on failure.
*/
function wp_cache_fetch() {
global $wp_object_cache;
return $wp_object_cache->fetch();
}
/**
* Fetches all remaining results from the last request.
*
* @link https://www.php.net/manual/en/memcached.fetchall.php
*
* @return array|false The results on success, false on failure.
*/
function wp_cache_fetch_all() {
global $wp_object_cache;
return $wp_object_cache->fetchAll();
}
/**
* Invalidates all items in the cache.
*
* @link https://www.php.net/manual/en/memcached.flush.php
*
* @param int $delay Number of seconds to wait before invalidating the items.
* @return bool True on success, false on failure.
*/
function wp_cache_flush( $delay = 0 ) {
global $wp_object_cache;
return $wp_object_cache->flush( $delay );
}
/**
* Removes all cache items from the in-memory runtime cache.
*
* @return bool True on success, false on failure.
*/
function wp_cache_flush_runtime() {
global $wp_object_cache;
return $wp_object_cache->flush_runtime();
}
/**
* Determines whether the object cache implementation supports a particular feature.
*
* @since 6.1.0
*
* @param string $feature Name of the feature to check for. Possible values include:
* 'add_multiple', 'set_multiple', 'get_multiple', 'delete_multiple',
* 'flush_runtime', 'flush_group'.
* @return bool True if the feature is supported, false otherwise.
*/
function wp_cache_supports( $feature ) {
switch ( $feature ) {
case 'get_multiple':
case 'flush_runtime':
return true;
default:
return false;
}
}
/**
* Retrieves object from cache.
*
* Gets an object from cache based on $key and $group. In order to fully support
* the $cache_cb and $cas_token parameters, the runtime cache is ignored by this function
* if either of those values are set. In that case, the request is made directly
* to the memcached server for proper handling of the callback and/or token.
*
* Note that the $deprecated and $found args are only here for compatibility
* with the native wp_cache_get() function.
*
* @link https://www.php.net/manual/en/memcached.get.php
*
* @param string $key The key under which to store the value.
* @param string $group The group value appended to the $key.
* @param bool $force Whether or not to force a cache invalidation.
* @param null|bool $found Variable passed by reference to determine if the value was found or not.
* @param null|string $cache_cb Read-through caching callback.
* @param null|float $cas_token The variable to store the CAS token in.
* @return bool|mixed Cached object value.
*/
function wp_cache_get( $key, $group = '', $force = false, &$found = null, $cache_cb = null, &$cas_token = null ) {
global $wp_object_cache;
if ( func_num_args() > 4 ) {
return $wp_object_cache->get( $key, $group, $force, $found, '', false, $cache_cb, $cas_token );
} else {
return $wp_object_cache->get( $key, $group, $force, $found );
}
}
/**
* Retrieves object from cache from specified server.
*
* Gets an object from cache based on $key, $group, and $server_key. In order to fully support
* the $cache_cb and $cas_token parameters, the runtime cache is ignored by this function
* if either of those values are set. In that case, the request is made directly
* to the memcached server for proper handling of the callback and/or token.
*
* @link https://www.php.net/manual/en/memcached.getbykey.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param string $key The key under which to store the value.
* @param string $group The group value appended to the $key.
* @param bool $force Whether or not to force a cache invalidation.
* @param null|bool $found Variable passed by reference to determine if the value was found or not.
* @param null|string $cache_cb Read-through caching callback.
* @param null|float $cas_token The variable to store the CAS token in.
* @return bool|mixed Cached object value.
*/
function wp_cache_get_by_key( $server_key, $key, $group = '', $force = false, &$found = null, $cache_cb = null, &$cas_token = null ) {
global $wp_object_cache;
if ( func_num_args() > 5 ) {
return $wp_object_cache->getByKey( $server_key, $key, $group, $force, $found, $cache_cb, $cas_token );
} else {
return $wp_object_cache->getByKey( $server_key, $key, $group, $force, $found );
}
}
/**
* Requests multiple keys without blocking.
*
* @link https://www.php.net/manual/en/memcached.getdelayed.php
*
* @param string|array $keys Array or string of key(s) to request.
* @param string|array $groups Array or string of group(s) for the key(s).
* See buildKeys for more on how these are handled.
* @param bool $with_cas Whether to request CAS token values also.
* @param null $value_cb The result callback or null.
* @return bool True on success, false on failure.
*/
function wp_cache_get_delayed( $keys, $groups = '', $with_cas = false, $value_cb = null ) {
global $wp_object_cache;
return $wp_object_cache->getDelayed( $keys, $groups, $with_cas, $value_cb );
}
/**
* Requests multiple keys without blocking from a specified server.
*
* @link https://www.php.net/manual/en/memcached.getdelayed.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param string|array $keys Array or string of key(s) to request.
* @param string|array $groups Array or string of group(s) for the key(s).
* See buildKeys for more on how these are handled.
* @param bool $with_cas Whether to request CAS token values also.
* @param null $value_cb The result callback or null.
* @return bool True on success, false on failure.
*/
function wp_cache_get_delayed_by_key( $server_key, $keys, $groups = '', $with_cas = false, $value_cb = null ) {
global $wp_object_cache;
return $wp_object_cache->getDelayedByKey( $server_key, $keys, $groups, $with_cas, $value_cb );
}
/**
* Gets multiple values from memcached in one request.
*
* See the buildKeys method definition to understand the $keys/$groups parameters.
*
* @link https://www.php.net/manual/en/memcached.getmulti.php
*
* @param array $keys Array of keys to retrieve.
* @param string|array $groups If string, used for all keys.
* If arrays, corresponds with the $keys array.
* @param null|array $cas_tokens The variable to store the CAS tokens for the found items.
* @param int $flags The flags for the get operation.
* @return bool|array The array of found items on success, false on failure.
*/
function wp_cache_get_multi( $keys, $groups = '', &$cas_tokens = null, $flags = null ) {
global $wp_object_cache;
if ( func_num_args() > 2 ) {
return $wp_object_cache->getMulti( $keys, $groups, '', $cas_tokens, $flags );
} else {
return $wp_object_cache->getMulti( $keys, $groups );
}
}
/**
* Gets multiple values from memcached in one request by specified server key.
*
* See the buildKeys method definition to understand the $keys/$groups parameters.
*
* @link https://www.php.net/manual/en/memcached.getmultibykey.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param array $keys Array of keys to retrieve.
* @param string|array $groups If string, used for all keys.
* If arrays, corresponds with the $keys array.
* @param null|array $cas_tokens The variable to store the CAS tokens for the found items.
* @param int $flags The flags for the get operation.
* @return bool|array The array of found items on success, false on failure.
*/
function wp_cache_get_multi_by_key( $server_key, $keys, $groups = '', &$cas_tokens = null, $flags = null ) {
global $wp_object_cache;
if ( func_num_args() > 3 ) {
return $wp_object_cache->getMultiByKey( $server_key, $keys, $groups, $cas_tokens, $flags );
} else {
return $wp_object_cache->getMultiByKey( $server_key, $keys, $groups );
}
}
/**
* Retrieves multiple values from the cache in one call.
*
* @param array $keys Array of keys under which the cache contents are stored.
* @param string $group Optional. Where the cache contents are grouped. Default empty.
* @param bool $force Optional. Whether to force an update of the local cache
* from the persistent cache. Default false.
* @return array Array of return values, grouped by key. Each value is either
* the cache contents on success, or false on failure.
*/
function wp_cache_get_multiple( $keys, $group = '', $force = false ) {
global $wp_object_cache;
// Prime multiple keys in a single Memcached call.
$wp_object_cache->getMulti( $keys, $group );
return $wp_object_cache->getMultiple( $keys, $group, $force );
}
/**
* Retrieves a Memcached option value.
*
* @link https://www.php.net/manual/en/memcached.getoption.php
*
* @param int $option One of the Memcached::OPT_* constants.
* @return mixed The value of the requested option on success, false on failure.
*/
function wp_cache_get_option( $option ) {
global $wp_object_cache;
return $wp_object_cache->getOption( $option );
}
/**
* Returns the result code of the last option.
*
* @link https://www.php.net/manual/en/memcached.getresultcode.php
*
* @return int Result code of the last Memcached operation.
*/
function wp_cache_get_result_code() {
global $wp_object_cache;
return $wp_object_cache->getResultCode();
}
/**
* Return the message describing the result of the last operation.
*
* @link https://www.php.net/manual/en/memcached.getresultmessage.php
*
* @return string Message describing the result of the last Memcached operation.
*/
function wp_cache_get_result_message() {
global $wp_object_cache;
return $wp_object_cache->getResultMessage();
}
/**
* Gets server information by key.
*
* @link https://www.php.net/manual/en/memcached.getserverbykey.php
*
* @param string $server_key The key identifying the server to store the value on.
* @return array Array with host, post, and weight on success, fales on failure.
*/
function wp_cache_get_server_by_key( $server_key ) {
global $wp_object_cache;
return $wp_object_cache->getServerByKey( $server_key );
}
/**
* Gets the list of servers in the pool.
*
* @link https://www.php.net/manual/en/memcached.getserverlist.php
*
* @return array The list of all servers in the server pool.
*/
function wp_cache_get_server_list() {
global $wp_object_cache;
return $wp_object_cache->getServerList();
}
/**
* Gets server pool statistics.
*
* @link https://www.php.net/manual/en/memcached.getstats.php
*
* @return array Array of server statistics, one entry per server.
*/
function wp_cache_get_stats() {
global $wp_object_cache;
return $wp_object_cache->getStats();
}
/**
* Gets server pool memcached version information.
*
* @link https://www.php.net/manual/en/memcached.getversion.php
*
* @return array Array of server versions, one entry per server.
*/
function wp_cache_get_version() {
global $wp_object_cache;
return $wp_object_cache->getVersion();
}
/**
* Increments a numeric item's value.
*
* @link https://www.php.net/manual/en/memcached.increment.php
*
* @param string $key The key under which to store the value.
* @param int $offset The amount by which to increment the item's value.
* @param string $group The group value appended to the $key.
* @return int|bool Item's new value on success, false on failure.
*/
function wp_cache_increment( $key, $offset = 1, $group = '' ) {
global $wp_object_cache;
return $wp_object_cache->increment( $key, $offset, $group );
}
/**
* Increments a numeric item's value.
*
* This is the same as wp_cache_increment(), but kept for backward compatibility.
* The original WordPress caching backends use wp_cache_incr().
*
* @link https://www.php.net/manual/en/memcached.increment.php
*
* @param string $key The key under which to store the value.
* @param int $offset The amount by which to increment the item's value.
* @param string $group The group value appended to the $key.
* @return int|bool Item's new value on success, false on failure.
*/
function wp_cache_incr( $key, $offset = 1, $group = '' ) {
return wp_cache_increment( $key, $offset, $group );
}
/**
* Prepends data to an existing item.
*
* This method should throw an error if it is used with compressed data. This is an expected behavior.
* Memcached casts the value to be prepended to the initial value to the type of the initial value.
* Be careful as this leads to unexpected behavior at times. For instance, prepending (float) 45.23
* to (int) 23 will result in 45, because the value is first combined (45.2323) then cast to "integer"
* (the original value), which will be (int) 45. Due to how memcached treats types, the behavior has been
* mimicked in the internal cache to produce similar results and improve consistency. It is recommended
* that prepends only occur with data of the same type.
*
* @link https://www.php.net/manual/en/memcached.prepend.php
*
* @param string $key The key under which to store the value.
* @param string $value Must be string as prepending mixed values is not well-defined.
* @param string $group The group value prepended to the $key.
* @return bool True on success, false on failure.
*/
function wp_cache_prepend( $key, $value, $group = '' ) {
global $wp_object_cache;
return $wp_object_cache->prepend( $key, $value, $group );
}
/**
* Appends data to an existing item by server key.
*
* This method should throw an error if it is used with compressed data. This is an expected behavior.
* Memcached casts the value to be prepended to the initial value to the type of the initial value.
* Be careful as this leads to unexpected behavior at times. For instance, prepending (float) 45.23
* to (int) 23 will result in 45, because the value is first combined (45.2323) then cast to "integer"
* (the original value), which will be (int) 45. Due to how memcached treats types, the behavior has been
* mimicked in the internal cache to produce similar results and improve consistency. It is recommended
* that prepends only occur with data of the same type.
*
* @link https://www.php.net/manual/en/memcached.prependbykey.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param string $key The key under which to store the value.
* @param string $value Must be string as prepending mixed values is not well-defined.
* @param string $group The group value prepended to the $key.
* @return bool True on success, false on failure.
*/
function wp_cache_prepend_by_key( $server_key, $key, $value, $group = '' ) {
global $wp_object_cache;
return $wp_object_cache->prependByKey( $server_key, $key, $value, $group );
}
/**
* Replaces a value in cache.
*
* This method is similar to "add"; however, is does not successfully set a value
* if the object's key is not already set in cache.
*
* @link https://www.php.net/manual/en/memcached.replace.php
*
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
* @return bool True on success, false on failure.
*/
function wp_cache_replace( $key, $value, $group = '', $expiration = 0 ) {
global $wp_object_cache;
return $wp_object_cache->replace( $key, $value, $group, $expiration );
}
/**
* Replaces a value in cache on a specific server.
*
* This method is similar to "addByKey"; however, is does not successfully set a value
* if the object's key is not already set in cache.
*
* @link https://www.php.net/manual/en/memcached.addbykey.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
* @return bool True on success, false on failure.
*/
function wp_cache_replace_by_key( $server_key, $key, $value, $group = '', $expiration = 0 ) {
global $wp_object_cache;
return $wp_object_cache->replaceByKey( $server_key, $key, $value, $group, $expiration );
}
/**
* Sets a value in cache.
*
* The value is set whether or not this key already exists in memcached.
*
* @link https://www.php.net/manual/en/memcached.set.php
*
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
* @return bool True on success, false on failure.
*/
function wp_cache_set( $key, $value, $group = '', $expiration = 0 ) {
global $wp_object_cache;
return $wp_object_cache->set( $key, $value, $group, $expiration );
}
/**
* Sets a value in cache.
*
* The value is set whether or not this key already exists in memcached.
*
* @link https://www.php.net/manual/en/memcached.set.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
* @return bool True on success, false on failure.
*/
function wp_cache_set_by_key( $server_key, $key, $value, $group = '', $expiration = 0 ) {
global $wp_object_cache;
return $wp_object_cache->setByKey( $server_key, $key, $value, $group, $expiration );
}
/**
* Sets multiple values to cache at once.
*
* By sending an array of $items to this function, all values are saved at once to
* memcached, reducing the need for multiple requests to memcached. The $items array
* keys and values are what are stored to memcached. The keys in the $items array
* are merged with the $groups array/string value via buildKeys to determine the
* final key for the object.
*
* @param array $items An array of key/value pairs to store on the server.
* @param string|array $groups Group(s) to merge with key(s) in $items.
* @param int $expiration The expiration time, defaults to 0.
* @return bool True on success, false on failure.
*/
function wp_cache_set_multi( $items, $groups = '', $expiration = 0 ) {
global $wp_object_cache;
return $wp_object_cache->setMulti( $items, $groups, $expiration );
}
/**
* Sets multiple values to cache at once on specified server.
*
* By sending an array of $items to this function, all values are saved at once to
* memcached, reducing the need for multiple requests to memcached. The $items array
* keys and values are what are stored to memcached. The keys in the $items array
* are merged with the $groups array/string value via buildKeys to determine the
* final key for the object.
*
* @param string $server_key The key identifying the server to store the value on.
* @param array $items An array of key/value pairs to store on the server.
* @param string|array $groups Group(s) to merge with key(s) in $items.
* @param int $expiration The expiration time, defaults to 0.
* @return bool True on success, false on failure.
*/
function wp_cache_set_multi_by_key( $server_key, $items, $groups = 'default', $expiration = 0 ) {
global $wp_object_cache;
return $wp_object_cache->setMultiByKey( $server_key, $items, $groups, $expiration );
}
/**
* Sets multiple values to the cache in one call.
*
* Differs from wp_cache_add_multiple() in that it will always write data.
*
* @param array $items Array of keys and values to be set.
* @param string $group Optional. Where the cache contents are grouped. Default empty.
* @param int $expiration Optional. When to expire the cache contents, in seconds.
* Default 0 (no expiration).
* @return bool[] Array of return values, grouped by key. Each value is either
* true on success, or false on failure.
*/
function wp_cache_set_multiple( array $items, $group = '', $expiration = 0 ) {
global $wp_object_cache;
return $wp_object_cache->setMultiple( $items, $group, $expiration );
}
/**
* Sets a Memcached option.
*
* @link https://www.php.net/manual/en/memcached.setoption.php
*
* @param int $option Option name.
* @param mixed $value Option value.
* @return bool True on success, false on failure.
*/
function wp_cache_set_option( $option, $value ) {
global $wp_object_cache;
return $wp_object_cache->setOption( $option, $value );
}
/**
* Switches blog prefix, which changes the cache that is accessed.
*
* @param int $blog_id Blog to switch to.
* @return void
*/
function wp_cache_switch_to_blog( $blog_id ) {
global $wp_object_cache;
return $wp_object_cache->switch_to_blog( $blog_id );
}
/**
* Sets up Object Cache Global and assigns it.
*
* @global WP_Object_Cache $wp_object_cache WordPress Object Cache
* @return void
*/
function wp_cache_init() {
global $wp_object_cache;
$wp_object_cache = new WP_Object_Cache();
}
/**
* Adds a group or set of groups to the list of non-persistent groups.
*
* @param string|array $groups A group or an array of groups to add.
* @return void
*/
function wp_cache_add_global_groups( $groups ) {
global $wp_object_cache;
$wp_object_cache->add_global_groups( $groups );
}
/**
* Adds a group or set of groups to the list of non-Memcached groups.
*
* @param string|array $groups A group or an array of groups to add.
* @return void
*/
function wp_cache_add_non_persistent_groups( $groups ) {
global $wp_object_cache;
$wp_object_cache->add_non_persistent_groups( $groups );
}
// phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
class WP_Object_Cache {
/**
* Holds the Memcached object.
*
* @var Memcached
*/
public $m;
/**
* Hold the Memcached server details.
*
* @var array
*/
public $servers;
/**
* Holds the non-Memcached objects.
*
* @var array
*/
public $cache = array();
/**
* List of global groups.
*
* @var array
*/
public $global_groups = array();
/**
* List of groups not saved to Memcached.
*
* @var array
*/
public $no_mc_groups = array();
/**
* Prefix used for global groups.
*
* @var string
*/
public $global_prefix = '';
/**
* Prefix used for non-global groups.
*
* @var string
*/
public $blog_prefix = '';
/**
* Thirty days in seconds.
*
* @var int
*/
public $thirty_days;
/**
* Current unix time stamp.
*
* @var int
*/
public $now;
/**
* Instantiates the Memcached class.
*
* Instantiates the Memcached class and returns adds the servers specified
* in the $memcached_servers global array.
*
* @link https://www.php.net/manual/en/memcached.construct.php
*
* @param null $persistent_id To create an instance that persists between requests,
* use persistent_id to specify a unique ID for the instance.
*/
public function __construct( $persistent_id = null ) {
global $memcached_servers, $blog_id, $table_prefix;
if ( is_null( $persistent_id ) || ! is_string( $persistent_id ) ) {
$this->m = new Memcached();
} else {
$this->m = new Memcached( $persistent_id );
}
if ( isset( $memcached_servers ) ) {
$this->servers = $memcached_servers;
} else {
$this->servers = array( array( 'memcached', 11211 ) );
}
$this->addServers( $this->servers );
/**
* This approach is borrowed from Sivel and Boren. Use the salt for easy cache invalidation
* and for multi single WP installations on the same server.
*/
if ( ! defined( 'WP_CACHE_KEY_SALT' ) ) {
define( 'WP_CACHE_KEY_SALT', '' );
}
// Assign global and blog prefixes for use with keys.
if ( function_exists( 'is_multisite' ) ) {
$this->global_prefix = ( is_multisite() || defined( 'CUSTOM_USER_TABLE' ) && defined( 'CUSTOM_USER_META_TABLE' ) ) ? '' : $table_prefix;
$this->blog_prefix = ( is_multisite() ? $blog_id : $table_prefix ) . ':';
}
// Setup cacheable values for handling expiration times.
$this->thirty_days = 60 * 60 * 24 * 30;
$this->now = time();
}
/**
* Adds a value to cache.
*
* If the specified key already exists, the value is not stored and the function
* returns false.
*
* @link https://www.php.net/manual/en/memcached.add.php
*
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
* @param string $server_key The key identifying the server to store the value on.
* @param bool $by_key True to store in internal cache by key; false to not store by key.
* @return bool True on success, false on failure.
*/
public function add( $key, $value, $group = 'default', $expiration = 0, $server_key = '', $by_key = false ) {
/*
* Ensuring that wp_suspend_cache_addition is defined before calling, because sometimes an advanced-cache.php
* file will load object-cache.php before wp-includes/functions.php is loaded. In those cases, if wp_cache_add
* is called in advanced-cache.php before any more of WordPress is loaded, we get a fatal error because
* wp_suspend_cache_addition will not be defined until wp-includes/functions.php is loaded.
*/
if ( function_exists( 'wp_suspend_cache_addition' ) && wp_suspend_cache_addition() ) {
return false;
}
$derived_key = $this->buildKey( $key, $group );
// Add does not set the value if the key exists; mimic that here.
if ( isset( $this->cache[ $derived_key ] ) ) {
return false;
}
// If group is a non-Memcached group, save to runtime cache, not Memcached.
if ( in_array( $group, $this->no_mc_groups, true ) ) {
$this->add_to_internal_cache( $derived_key, $value );
return true;
}
$expiration = $this->sanitize_expiration( $expiration );
// Save to Memcached.
if ( $by_key ) {
$result = $this->m->addByKey( $server_key, $derived_key, $value, $expiration );
} else {
$result = $this->m->add( $derived_key, $value, $expiration );
}
// Store in runtime cache if add was successful.
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
$this->add_to_internal_cache( $derived_key, $value );
}
return $result;
}
/**
* Adds a value to cache on a specific server.
*
* Using a server_key value, the object can be stored on a specified server as opposed
* to a random server in the stack. Note that this method will add the key/value to the
* _cache object as part of the runtime cache. It will add it to an array for the
* specified server_key.
*
* @link https://www.php.net/manual/en/memcached.addbykey.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
* @return bool True on success, false on failure.
*/
public function addByKey( $server_key, $key, $value, $group = 'default', $expiration = 0 ) {
return $this->add( $key, $value, $group, $expiration, $server_key, true );
}
/**
* Adds multiple values to cache.
*
* @param array $items Array of keys and values to be added.
* @param string $group Optional. Where the cache contents are grouped. Default empty.
* @param int $expiration Optional. When to expire the cache contents, in seconds.
* Default 0 (no expiration).
* @return bool[] Array of return values, grouped by key. Each value is either
* true on success, or false if cache key and group already exist.
*/
public function addMultiple( array $items, $group = '', $expiration = 0 ) {
$values = array();
foreach ( $items as $key => $value ) {
$values[ $key ] = $this->add( $key, $value, $group, $expiration );
}
return $values;
}
/**
* Adds a single server to the list of Memcached servers.
*
* @link https://www.php.net/manual/en/memcached.addserver.php
*
* @param string $host The hostname of the memcache server.
* @param int $port The port on which memcache is running.
* @param int $weight The weight of the server relative to the total weight
* of all the servers in the pool.
* @return bool True on success, false on failure.
*/
public function addServer( $host, $port, $weight = 0 ) {
$host = is_string( $host ) ? $host : '127.0.0.1';
$port = is_numeric( $port ) && $port > 0 ? $port : 11211;
$weight = is_numeric( $weight ) && $weight > 0 ? $weight : 1;
return $this->m->addServer( $host, $port, $weight );
}
/**
* Adds an array of servers to the pool.
*
* Each individual server in the array must include a domain and port, with an optional
* weight value: $servers = array( array( '127.0.0.1', 11211, 0 ) );
*
* @link https://www.php.net/manual/en/memcached.addservers.php
*
* @param array $servers Array of server to register.
* @return bool True on success, false on failure.
*/
public function addServers( $servers ) {
if ( ! is_object( $this->m ) ) {
return false;
}
return $this->m->addServers( $servers );
}
/**
* Appends data to an existing item.
*
* This method should throw an error if it is used with compressed data.
* This is an expected behavior. Memcached casts the value to be appended to the initial value
* to the type of the initial value. Be careful as this leads to unexpected behavior at times.
* Due to how memcached treats types, the behavior has been mimicked in the internal cache to produce
* similar results and improve consistency. It is recommended that appends only occur with data of
* the same type.
*
* @link https://www.php.net/manual/en/memcached.append.php
*
* @param string $key The key under which to store the value.
* @param mixed $value Must be string as appending mixed values is not well-defined.
* @param string $group The group value appended to the $key.
* @param string $server_key The key identifying the server to store the value on.
* @param bool $by_key True to store in internal cache by key; false to not store by key.
* @return bool True on success, false on failure.
*/
public function append( $key, $value, $group = 'default', $server_key = '', $by_key = false ) {
if ( ! is_string( $value ) && ! is_int( $value ) && ! is_float( $value ) ) {
return false;
}
$derived_key = $this->buildKey( $key, $group );
// If group is a non-Memcached group, append to runtime cache value, not Memcached.
if ( in_array( $group, $this->no_mc_groups, true ) ) {
if ( ! isset( $this->cache[ $derived_key ] ) ) {
return false;
}
$combined = $this->combine_values( $this->cache[ $derived_key ], $value, 'app' );
$this->add_to_internal_cache( $derived_key, $combined );
return true;
}
// Append to Memcached value.
if ( $by_key ) {
$result = $this->m->appendByKey( $server_key, $derived_key, $value );
} else {
$result = $this->m->append( $derived_key, $value );
}
// Store in runtime cache if add was successful.
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
$combined = $this->combine_values( $this->cache[ $derived_key ], $value, 'app' );
$this->add_to_internal_cache( $derived_key, $combined );
}
return $result;
}
/**
* Appends data to an existing item by server key.
*
* This method should throw an error if it is used with compressed data.
* This is an expected behavior. Memcached casts the value to be appended to the initial value
* to the type of the initial value. Be careful as this leads to unexpected behavior at times.
* Due to how memcached treats types, the behavior has been mimicked in the internal cache to produce
* similar results and improve consistency. It is recommended that appends only occur with data of
* the same type.
*
* @link https://www.php.net/manual/en/memcached.appendbykey.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param string $key The key under which to store the value.
* @param mixed $value Must be string as appending mixed values is not well-defined.
* @param string $group The group value appended to the $key.
* @return bool True on success, false on failure.
*/
public function appendByKey( $server_key, $key, $value, $group = 'default' ) {
return $this->append( $key, $value, $group, $server_key, true );
}
/**
* Performs a "check and set" to store data.
*
* The set will be successful only if the no other request has updated the value
* since it was fetched by this request.
*
* @link https://www.php.net/manual/en/memcached.cas.php
*
* @param float $cas_token Unique value associated with the existing item. Generated by memcached.
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
* @param string $server_key The key identifying the server to store the value on.
* @param bool $by_key True to store in internal cache by key; false to not store by key.
* @return bool True on success, false on failure.
*/
public function cas( $cas_token, $key, $value, $group = 'default', $expiration = 0, $server_key = '', $by_key = false ) {
$derived_key = $this->buildKey( $key, $group );
/**
* If group is a non-Memcached group, save to runtime cache, not Memcached. Note
* that since check and set cannot be emulated in the run time cache, this value
* operation is treated as a normal "add" for no_mc_groups.
*/
if ( in_array( $group, $this->no_mc_groups, true ) ) {
$this->add_to_internal_cache( $derived_key, $value );
return true;
}
$expiration = $this->sanitize_expiration( $expiration );
// Save to Memcached.
if ( $by_key ) {
$result = $this->m->casByKey( $cas_token, $server_key, $derived_key, $value, $expiration );
} else {
$result = $this->m->cas( $cas_token, $derived_key, $value, $expiration );
}
// Store in runtime cache if cas was successful.
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
$this->add_to_internal_cache( $derived_key, $value );
}
return $result;
}
/**
* Performs a "check and set" to store data with a server key.
*
* The set will be successful only if the no other request has updated the value
* since it was fetched by this request.
*
* @link https://www.php.net/manual/en/memcached.casbykey.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param float $cas_token Unique value associated with the existing item. Generated by memcached.
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
* @return bool True on success, false on failure.
*/
public function casByKey( $cas_token, $server_key, $key, $value, $group = 'default', $expiration = 0 ) {
return $this->cas( $cas_token, $key, $value, $group, $expiration, $server_key, true );
}
/**
* Decrements a numeric item's value.
*
* @link https://www.php.net/manual/en/memcached.decrement.php
*
* @param string $key The key under which to store the value.
* @param int $offset The amount by which to decrement the item's value.
* @param string $group The group value appended to the $key.
* @return int|bool Item's new value on success, false on failure.
*/
public function decrement( $key, $offset = 1, $group = 'default' ) {
$derived_key = $this->buildKey( $key, $group );
// Decrement values in no_mc_groups.
if ( in_array( $group, $this->no_mc_groups, true ) ) {
// Only decrement if the key already exists and value is 0 or greater (mimics memcached behavior).
if ( isset( $this->cache[ $derived_key ] ) && $this->cache[ $derived_key ] >= 0 ) {
// If numeric, subtract; otherwise, consider it 0 and do nothing.
if ( is_numeric( $this->cache[ $derived_key ] ) ) {
$this->cache[ $derived_key ] -= (int) $offset;
} else {
$this->cache[ $derived_key ] = 0;
}
// Returned value cannot be less than 0.
if ( $this->cache[ $derived_key ] < 0 ) {
$this->cache[ $derived_key ] = 0;
}
return $this->cache[ $derived_key ];
} else {
return false;
}
}
$result = $this->m->decrement( $derived_key, $offset );
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
$this->add_to_internal_cache( $derived_key, $result );
}
return $result;
}
/**
* Decrements a numeric item's value.
*
* Alias for $this->decrement(). Other caching backends use this abbreviated form
* of the function. It *may* cause breakage somewhere, so it is nice to have.
* This function will also allow the core unit tests to pass.
*
* @param string $key The key under which to store the value.
* @param int $offset The amount by which to decrement the item's value.
* @param string $group The group value appended to the $key.
* @return int|bool Item's new value on success, false on failure.
*/
public function decr( $key, $offset = 1, $group = 'default' ) {
return $this->decrement( $key, $offset, $group );
}
/**
* Removes the item from the cache.
*
* Removes an item from memcached with identified by $key after $time seconds.
* The $time parameter allows an object to be queued for deletion without
* immediately deleting. Between the time that it is queued and the time it's deleted,
* add, replace, and get will fail, but set will succeed.
*
* @link https://www.php.net/manual/en/memcached.delete.php
*
* @param string $key The key under which to store the value.
* @param string $group The group value appended to the $key.
* @param int $time The amount of time the server will wait to delete the item in seconds.
* @param string $server_key The key identifying the server to store the value on.
* @param bool $by_key True to store in internal cache by key; false to not store by key.
* @return bool True on success, false on failure.
*/
public function delete( $key, $group = 'default', $time = 0, $server_key = '', $by_key = false ) {
$derived_key = $this->buildKey( $key, $group );
// Remove from no_mc_groups array.
if ( in_array( $group, $this->no_mc_groups, true ) ) {
if ( isset( $this->cache[ $derived_key ] ) ) {
unset( $this->cache[ $derived_key ] );
}
return true;
}
if ( $by_key ) {
$result = $this->m->deleteByKey( $server_key, $derived_key, $time );
} else {
$result = $this->m->delete( $derived_key, $time );
}
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
unset( $this->cache[ $derived_key ] );
}
return $result;
}
/**
* Removes the item from the cache by server key.
*
* Removes an item from memcached with identified by $key after $time seconds.
* The $time parameter allows an object to be queued for deletion without
* immediately deleting. Between the time that it is queued and the time it's deleted,
* add, replace, and get will fail, but set will succeed.
*
* @link https://www.php.net/manual/en/memcached.deletebykey.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param string $key The key under which to store the value.
* @param string $group The group value appended to the $key.
* @param int $time The amount of time the server will wait to delete the item in seconds.
* @return bool True on success, false on failure.
*/
public function deleteByKey( $server_key, $key, $group = 'default', $time = 0 ) {
return $this->delete( $key, $group, $time, $server_key, true );
}
/**
* Removes multiple items from the cache.
*
* @param array $keys Array of keys under which the cache to deleted.
* @param string $group Optional. Where the cache contents are grouped. Default empty.
* @return bool[] Array of return values, grouped by key. Each value is either
* true on success, or false if the contents were not deleted.
*/
public function deleteMultiple( $keys, $group ) {
$values = array();
foreach ( $keys as $key ) {
$values[ $key ] = $this->delete( $key, $group );
}
return $values;
}
/**
* Fetches the next result.
*
* @link https://www.php.net/manual/en/memcached.fetch.php
*
* @return array|false The next result on success, false on failure.
*/
public function fetch() {
return $this->m->fetch();
}
/**
* Fetches all remaining results from the last request.
*
* @link https://www.php.net/manual/en/memcached.fetchall.php
*
* @return array|false The results on success, false on failure.
*/
public function fetchAll() {
return $this->m->fetchAll();
}
/**
* Invalidates all items in the cache.
*
* @link https://www.php.net/manual/en/memcached.flush.php
*
* @param int $delay Number of seconds to wait before invalidating the items.
* @return bool True on success, false on failure.
*/
public function flush( $delay = 0 ) {
$result = $this->m->flush( $delay );
// Only reset the runtime cache if memcached was properly flushed.
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
$this->cache = array();
}
return $result;
}
/**
* Clears the in-memory cache of all data leaving the external cache untouched.
*
* @return bool Always returns true.
*/
public function flush_runtime() {
$this->cache = array();
return true;
}
/**
* Retrieves object from cache.
*
* Gets an object from cache based on $key and $group. In order to fully support
* the $cache_cb and $cas_token parameters, the runtime cache is ignored by this function
* if either of those values are set. In that case, the request is made directly
* to the memcached server for proper handling of the callback and/or token.
* Note that the $cas_token variable cannot be directly passed to the function.
* The variable needs to be first defined with a non-null value.
*
* If using the $cache_cb argument, the new value will always have an expiration
* of time of 0 (forever). This is a limitation of the Memcached PECL extension.
*
* @link https://www.php.net/manual/en/memcached.get.php
*
* @param string $key The key under which to store the value.
* @param string $group The group value appended to the $key.
* @param bool $force Whether or not to force a cache invalidation.
* @param null|bool $found Variable passed by reference to determine if the value was found or not.
* @param string $server_key The key identifying the server to store the value on.
* @param bool $by_key True to store in internal cache by key; false to not store by key.
* @param null|callable $cache_cb Read-through caching callback.
* @param null|float $cas_token The variable to store the CAS token in.
* @return bool|mixed Cached object value.
*/
public function get( $key, $group = 'default', $force = false, &$found = null, $server_key = '', $by_key = false, $cache_cb = null, &$cas_token = null ) {
$derived_key = $this->buildKey( $key, $group );
// Assume object is not found.
$found = false;
// If either $cache_db, or $cas_token is set, must hit Memcached and bypass runtime cache.
if ( func_num_args() > 6 && ! in_array( $group, $this->no_mc_groups, true ) ) {
if ( $by_key ) {
$value = $this->m->getByKey( $server_key, $derived_key, $cache_cb, $cas_token );
} else {
$value = $this->m->get( $derived_key, $cache_cb, $cas_token );
}
} else {
if ( isset( $this->cache[ $derived_key ] ) ) {
$found = true;
return is_object( $this->cache[ $derived_key ] ) ? clone $this->cache[ $derived_key ] : $this->cache[ $derived_key ];
} elseif ( in_array( $group, $this->no_mc_groups, true ) ) {
return false;
} else {
if ( $by_key ) {
$value = $this->m->getByKey( $server_key, $derived_key );
} else {
$value = $this->m->get( $derived_key );
}
}
}
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
$this->add_to_internal_cache( $derived_key, $value );
$found = true;
}
return is_object( $value ) ? clone $value : $value;
}
/**
* Retrieves object from cache from specified server.
*
* Gets an object from cache based on $key and $group, and $server_key. In order to fully support
* the $cache_cb and $cas_token parameters, the runtime cache is ignored by this function
* if either of those values are set. In that case, the request is made directly
* to the memcached server for proper handling of the callback and/or token.
* Note that the $cas_token variable cannot be directly passed to the function.
* The variable needs to be first defined with a non-null value.
*
* If using the $cache_cb argument, the new value will always have an expiration
* of time of 0 (forever). This is a limitation of the Memcached PECL extension.
*
* @link https://www.php.net/manual/en/memcached.getbykey.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param string $key The key under which to store the value.
* @param string $group The group value appended to the $key.
* @param bool $force Whether or not to force a cache invalidation.
* @param null|bool $found Variable passed by reference to determine if the value was found or not.
* @param null|string $cache_cb Read-through caching callback.
* @param null|float $cas_token The variable to store the CAS token in.
* @return bool|mixed Cached object value.
*/
public function getByKey( $server_key, $key, $group = 'default', $force = false, &$found = null, $cache_cb = null, &$cas_token = null ) {
/**
* Need to be careful how "get" is called. If you send $cache_cb, and $cas_token, it will hit memcached.
* Only send those args if they were sent to this function.
*/
if ( func_num_args() > 5 ) {
return $this->get( $key, $group, $force, $found, $server_key, true, $cache_cb, $cas_token );
} else {
return $this->get( $key, $group, $force, $found, $server_key, true );
}
}
/**
* Requests multiple keys without blocking.
*
* @link https://www.php.net/manual/en/memcached.getdelayed.php
*
* @param string|array $keys Array or string of key(s) to request.
* @param string|array $groups Array or string of group(s) for the key(s).
* See buildKeys for more on how these are handled.
* @param bool $with_cas Whether to request CAS token values also.
* @param null $value_cb The result callback or null.
* @return bool True on success, false on failure.
*/
public function getDelayed( $keys, $groups = 'default', $with_cas = false, $value_cb = null ) {
$derived_keys = $this->buildKeys( $keys, $groups );
return $this->m->getDelayed( $derived_keys, $with_cas, $value_cb );
}
/**
* Requests multiple keys without blocking from a specified server.
*
* @link https://www.php.net/manual/en/memcached.getdelayed.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param string|array $keys Array or string of key(s) to request.
* @param string|array $groups Array or string of group(s) for the key(s).
* See buildKeys for more on how these are handled.
* @param bool $with_cas Whether to request CAS token values also.
* @param null $value_cb The result callback or null.
* @return bool True on success, false on failure.
*/
public function getDelayedByKey( $server_key, $keys, $groups = 'default', $with_cas = false, $value_cb = null ) {
$derived_keys = $this->buildKeys( $keys, $groups );
return $this->m->getDelayedByKey( $server_key, $derived_keys, $with_cas, $value_cb );
}
/**
* Gets multiple values from memcached in one request.
*
* See the buildKeys method definition to understand the $keys/$groups parameters.
*
* @link https://www.php.net/manual/en/memcached.getmulti.php
*
* @param array $keys Array of keys to retrieve.
* @param string|array $groups If string, used for all keys.
* If arrays, corresponds with the $keys array.
* @param string $server_key The key identifying the server to store the value on.
* @param null|array $cas_tokens The variable to store the CAS tokens for the found items.
* @param int $flags The flags for the get operation.
* @return bool|array The array of found items on success, false on failure.
*/
public function getMulti( $keys, $groups = 'default', $server_key = '', &$cas_tokens = null, $flags = null ) {
$derived_keys = $this->buildKeys( $keys, $groups );
/**
* If either $cas_tokens, or $flags is set, must hit Memcached and bypass runtime cache.
* Note that this will purposely ignore no_mc_groups values as they cannot handle CAS tokens
* or the special flags; however, if the groups of groups contains a no_mc_group, this is bypassed.
*/
if ( func_num_args() > 3 && ! $this->contains_no_mc_group( $groups ) ) {
if ( ! empty( $server_key ) ) {
$values = $this->m->getMultiByKey( $server_key, $derived_keys, $cas_tokens, $flags );
} else {
$values = $this->m->getMulti( $derived_keys, $cas_tokens, $flags );
}
} else {
$values = array();
$need_to_get = array();
// Pull out values from runtime cache, or mark for retrieval.
foreach ( $derived_keys as $key ) {
if ( isset( $this->cache[ $key ] ) ) {
$values[ $key ] = $this->cache[ $key ];
} else {
$need_to_get[ $key ] = $key;
}
}
// Get those keys not found in the runtime cache.
if ( ! empty( $need_to_get ) ) {
if ( ! empty( $server_key ) ) {
$result = $this->m->getMultiByKey( $server_key, array_keys( $need_to_get ) );
} else {
$result = $this->m->getMulti( array_keys( $need_to_get ) );
}
}
// Merge with values found in runtime cache.
if ( isset( $result ) && Memcached::RES_SUCCESS === $this->getResultCode() ) {
$values = array_merge( $values, $result );
}
// If order should be preserved, reorder now.
if ( ! empty( $need_to_get ) && Memcached::GET_PRESERVE_ORDER === $flags ) {
$ordered_values = array();
foreach ( $derived_keys as $key ) {
if ( isset( $values[ $key ] ) ) {
$ordered_values[ $key ] = $values[ $key ];
}
}
$values = $ordered_values;
unset( $ordered_values );
}
}
// Add the values to the runtime cache.
$this->cache = array_merge( $this->cache, $values );
return $values;
}
/**
* Gets multiple values from memcached in one request by specified server key.
*
* See the buildKeys method definition to understand the $keys/$groups parameters.
*
* @link https://www.php.net/manual/en/memcached.getmultibykey.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param array $keys Array of keys to retrieve.
* @param string|array $groups If string, used for all keys.
* If arrays, corresponds with the $keys array.
* @param null|array $cas_tokens The variable to store the CAS tokens for the found items.
* @param int $flags The flags for the get operation.
* @return bool|array The array of found items on success, false on failure.
*/
public function getMultiByKey( $server_key, $keys, $groups = 'default', &$cas_tokens = null, $flags = null ) {
/**
* Need to be careful how "getMulti" is called. If you send $cache_cb, and $cas_token, it will hit memcached.
* Only send those args if they were sent to this function.
*/
if ( func_num_args() > 3 ) {
return $this->getMulti( $keys, $groups, $server_key, $cas_tokens, $flags );
} else {
return $this->getMulti( $keys, $groups, $server_key );
}
}
/**
* Get multiple items from the cache.
*
* @param array $keys Array of keys under which the cache contents are stored.
* @param string $group Optional. Where the cache contents are grouped. Default empty.
* @param bool $force Optional. Whether to force an update of the local cache
* from the persistent cache. Default false.
* @return array Array of return values, grouped by key. Each value is either
* the cache contents on success, or false on failure.
*/
public function getMultiple( $keys, $group = '', $force = false ) {
$values = array();
foreach ( $keys as $key ) {
$found = null;
$value = $this->get( $key, $group, $force, $found );
$values[ $key ] = $found ? $value : false;
}
return $values;
}
/**
* Retrieves a Memcached option value.
*
* @link https://www.php.net/manual/en/memcached.getoption.php
*
* @param int $option One of the Memcached::OPT_* constants.
* @return mixed The value of the requested option on success, false on failure.
*/
public function getOption( $option ) {
return $this->m->getOption( $option );
}
/**
* Returns the result code of the last option.
*
* @link https://www.php.net/manual/en/memcached.getresultcode.php
*
* @return int Result code of the last Memcached operation.
*/
public function getResultCode() {
return $this->m->getResultCode();
}
/**
* Return the message describing the result of the last operation.
*
* @link https://www.php.net/manual/en/memcached.getresultmessage.php
*
* @return string Message describing the result of the last Memcached operation.
*/
public function getResultMessage() {
return $this->m->getResultMessage();
}
/**
* Gets server information by key.
*
* @link https://www.php.net/manual/en/memcached.getserverbykey.php
*
* @param string $server_key The key identifying the server to store the value on.
* @return array Array with host, post, and weight on success, false on failure.
*/
public function getServerByKey( $server_key ) {
return $this->m->getServerByKey( $server_key );
}
/**
* Gets the list of servers in the pool.
*
* @link https://www.php.net/manual/en/memcached.getserverlist.php
*
* @return array The list of all servers in the server pool.
*/
public function getServerList() {
return $this->m->getServerList();
}
/**
* Gets server pool statistics.
*
* @link https://www.php.net/manual/en/memcached.getstats.php
*
* @return array Array of server statistics, one entry per server.
*/
public function getStats() {
return $this->m->getStats();
}
/**
* Gets server pool memcached version information.
*
* @link https://www.php.net/manual/en/memcached.getversion.php
*
* @return array Array of server versions, one entry per server.
*/
public function getVersion() {
return $this->m->getVersion();
}
/**
* Increments a numeric item's value.
*
* @link https://www.php.net/manual/en/memcached.increment.php
*
* @param string $key The key under which to store the value.
* @param int $offset The amount by which to increment the item's value.
* @param string $group The group value appended to the $key.
* @return int|bool Item's new value on success, false on failure.
*/
public function increment( $key, $offset = 1, $group = 'default' ) {
$derived_key = $this->buildKey( $key, $group );
// Increment values in no_mc_groups.
if ( in_array( $group, $this->no_mc_groups, true ) ) {
// Only increment if the key already exists and the number is currently 0 or greater (mimics memcached behavior).
if ( isset( $this->cache[ $derived_key ] ) && $this->cache[ $derived_key ] >= 0 ) {
// If numeric, add; otherwise, consider it 0 and do nothing.
if ( is_numeric( $this->cache[ $derived_key ] ) ) {
$this->cache[ $derived_key ] += (int) $offset;
} else {
$this->cache[ $derived_key ] = 0;
}
// Returned value cannot be less than 0.
if ( $this->cache[ $derived_key ] < 0 ) {
$this->cache[ $derived_key ] = 0;
}
return $this->cache[ $derived_key ];
} else {
return false;
}
}
$result = $this->m->increment( $derived_key, $offset );
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
$this->add_to_internal_cache( $derived_key, $result );
}
return $result;
}
/**
* Alias for $this->incr().
*
* Certain plugins expect an "incr" method on the $wp_object_cache object (e.g., Batcache).
* Since the original version of this library matched names to the memcached methods,
* the "incr" method was missing. Adding this method restores compatibility with plugins
* expecting an "incr" method.
*
* @param string $key The key under which to store the value.
* @param int $offset The amount by which to increment the item's value.
* @param string $group The group value appended to the $key.
* @return int|bool Item's new value on success, false on failure.
*/
public function incr( $key, $offset = 1, $group = 'default' ) {
return $this->increment( $key, $offset, $group );
}
/**
* Prepends data to an existing item.
*
* This method should throw an error if it is used with compressed data. This is an expected behavior.
* Memcached casts the value to be prepended to the initial value to the type of the initial value.
* Be careful as this leads to unexpected behavior at times. For instance, prepending (float) 45.23
* to (int) 23 will result in 45, because the value is first combined (45.2323) then cast to "integer"
* (the original value), which will be (int) 45. Due to how memcached treats types, the behavior has been
* mimicked in the internal cache to produce similar results and improve consistency. It is recommended
* that prepends only occur with data of the same type.
*
* @link https://www.php.net/manual/en/memcached.prepend.php
*
* @param string $key The key under which to store the value.
* @param string $value Must be string as prepending mixed values is not well-defined.
* @param string $group The group value prepended to the $key.
* @param string $server_key The key identifying the server to store the value on.
* @param bool $by_key True to store in internal cache by key; false to not store by key.
* @return bool True on success, false on failure.
*/
public function prepend( $key, $value, $group = 'default', $server_key = '', $by_key = false ) {
if ( ! is_string( $value ) && ! is_int( $value ) && ! is_float( $value ) ) {
return false;
}
$derived_key = $this->buildKey( $key, $group );
// If group is a non-Memcached group, prepend to runtime cache value, not Memcached.
if ( in_array( $group, $this->no_mc_groups, true ) ) {
if ( ! isset( $this->cache[ $derived_key ] ) ) {
return false;
}
$combined = $this->combine_values( $this->cache[ $derived_key ], $value, 'pre' );
$this->add_to_internal_cache( $derived_key, $combined );
return true;
}
// Append to Memcached value.
if ( $by_key ) {
$result = $this->m->prependByKey( $server_key, $derived_key, $value );
} else {
$result = $this->m->prepend( $derived_key, $value );
}
// Store in runtime cache if add was successful.
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
$combined = $this->combine_values( $this->cache[ $derived_key ], $value, 'pre' );
$this->add_to_internal_cache( $derived_key, $combined );
}
return $result;
}
/**
* Appends data to an existing item by server key.
*
* This method should throw an error if it is used with compressed data. This is an expected behavior.
* Memcached casts the value to be prepended to the initial value to the type of the initial value.
* Be careful as this leads to unexpected behavior at times. For instance, prepending (float) 45.23
* to (int) 23 will result in 45, because the value is first combined (45.2323) then cast to "integer"
* (the original value), which will be (int) 45. Due to how memcached treats types, the behavior has been
* mimicked in the internal cache to produce similar results and improve consistency. It is recommended
* that prepends only occur with data of the same type.
*
* @link https://www.php.net/manual/en/memcached.prependbykey.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param string $key The key under which to store the value.
* @param string $value Must be string as prepending mixed values is not well-defined.
* @param string $group The group value prepended to the $key.
* @return bool True on success, false on failure.
*/
public function prependByKey( $server_key, $key, $value, $group = 'default' ) {
return $this->prepend( $key, $value, $group, $server_key, true );
}
/**
* Replaces a value in cache.
*
* This method is similar to "add"; however, is does not successfully set a value
* if the object's key is not already set in cache.
*
* @link https://www.php.net/manual/en/memcached.replace.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param bool $by_key True to store in internal cache by key; false to not store by key.
* @param int $expiration The expiration time, defaults to 0.
* @return bool True on success, false on failure.
*/
public function replace( $key, $value, $group = 'default', $expiration = 0, $server_key = '', $by_key = false ) {
$derived_key = $this->buildKey( $key, $group );
// If group is a non-Memcached group, save to runtime cache, not Memcached.
if ( in_array( $group, $this->no_mc_groups, true ) ) {
// Replace won't save unless the key already exists; mimic this behavior here.
if ( ! isset( $this->cache[ $derived_key ] ) ) {
return false;
}
$this->cache[ $derived_key ] = $value;
return true;
}
$expiration = $this->sanitize_expiration( $expiration );
// Save to Memcached.
if ( $by_key ) {
$result = $this->m->replaceByKey( $server_key, $derived_key, $value, $expiration );
} else {
$result = $this->m->replace( $derived_key, $value, $expiration );
}
// Store in runtime cache if add was successful.
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
$this->add_to_internal_cache( $derived_key, $value );
}
return $result;
}
/**
* Replaces a value in cache on a specific server.
*
* This method is similar to "addByKey"; however, is does not successfully set a value
* if the object's key is not already set in cache.
*
* @link https://www.php.net/manual/en/memcached.addbykey.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
* @return bool True on success, false on failure.
*/
public function replaceByKey( $server_key, $key, $value, $group = 'default', $expiration = 0 ) {
return $this->replace( $key, $value, $group, $expiration, $server_key, true );
}
/**
* Sets a value in cache.
*
* The value is set whether or not this key already exists in memcached.
*
* @link https://www.php.net/manual/en/memcached.set.php
*
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
* @param string $server_key The key identifying the server to store the value on.
* @param bool $by_key True to store in internal cache by key; false to not store by key.
* @return bool True on success, false on failure.
*/
public function set( $key, $value, $group = 'default', $expiration = 0, $server_key = '', $by_key = false ) {
$derived_key = $this->buildKey( $key, $group );
// If group is a non-Memcached group, save to runtime cache, not Memcached.
if ( in_array( $group, $this->no_mc_groups, true ) ) {
$this->add_to_internal_cache( $derived_key, $value );
return true;
}
$expiration = $this->sanitize_expiration( $expiration );
// Save to Memcached.
if ( $by_key ) {
$result = $this->m->setByKey( $server_key, $derived_key, $value, $expiration );
} else {
$result = $this->m->set( $derived_key, $value, $expiration );
}
// Store in runtime cache if add was successful.
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
$this->add_to_internal_cache( $derived_key, $value );
}
return $result;
}
/**
* Sets a value in cache on a specific server.
*
* The value is set whether or not this key already exists in memcached.
*
* @link https://www.php.net/manual/en/memcached.setbykey.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param string $key The key under which to store the value.
* @param mixed $value The value to store.
* @param string $group The group value appended to the $key.
* @param int $expiration The expiration time, defaults to 0.
* @return bool True on success, false on failure.
*/
public function setByKey( $server_key, $key, $value, $group = 'default', $expiration = 0 ) {
return $this->set( $key, $value, $group, $expiration, $server_key, true );
}
/**
* Sets multiple values to cache at once.
*
* By sending an array of $items to this function, all values are saved at once to
* memcached, reducing the need for multiple requests to memcached. The $items array
* keys and values are what are stored to memcached. The keys in the $items array
* are merged with the $groups array/string value via buildKeys to determine the
* final key for the object.
*
* @link https://www.php.net/manual/en/memcached.setmulti.php
*
* @param array $items An array of key/value pairs to store on the server.
* @param string|array $groups Group(s) to merge with key(s) in $items.
* @param int $expiration The expiration time, defaults to 0.
* @param string $server_key The key identifying the server to store the value on.
* @param bool $by_key True to store in internal cache by key; false to not store by key.
* @return bool True on success, false on failure.
*/
public function setMulti( $items, $groups = 'default', $expiration = 0, $server_key = '', $by_key = false ) {
// Build final keys and replace $items keys with the new keys.
$derived_keys = $this->buildKeys( array_keys( $items ), $groups );
$derived_items = array_combine( $derived_keys, $items );
// Do not add to memcached if in no_mc_groups.
foreach ( $derived_items as $derived_key => $value ) {
// Get the individual item's group.
$key_pieces = explode( ':', $derived_key );
// If group is a non-Memcached group, save to runtime cache, not Memcached.
if ( in_array( $key_pieces[1], $this->no_mc_groups, true ) ) {
$this->add_to_internal_cache( $derived_key, $value );
unset( $derived_items[ $derived_key ] );
}
}
$expiration = $this->sanitize_expiration( $expiration );
// Save to memcached.
if ( $by_key ) {
$result = $this->m->setMultiByKey( $server_key, $derived_items, $expiration );
} else {
$result = $this->m->setMulti( $derived_items, $expiration );
}
// Store in runtime cache if add was successful.
if ( Memcached::RES_SUCCESS === $this->getResultCode() ) {
$this->cache = array_merge( $this->cache, $derived_items );
}
return $result;
}
/**
* Sets multiple values to cache at once on specified server.
*
* By sending an array of $items to this function, all values are saved at once to
* memcached, reducing the need for multiple requests to memcached. The $items array
* keys and values are what are stored to memcached. The keys in the $items array
* are merged with the $groups array/string value via buildKeys to determine the
* final key for the object.
*
* @link https://www.php.net/manual/en/memcached.setmultibykey.php
*
* @param string $server_key The key identifying the server to store the value on.
* @param array $items An array of key/value pairs to store on the server.
* @param string|array $groups Group(s) to merge with key(s) in $items.
* @param int $expiration The expiration time, defaults to 0.
* @return bool True on success, false on failure.
*/
public function setMultiByKey( $server_key, $items, $groups = 'default', $expiration = 0 ) {
return $this->setMulti( $items, $groups, $expiration, $server_key, true );
}
/**
* Sets multiple values in cache.
*
* @param array $items Array of keys and values to be set.
* @param string $group Optional. Where the cache contents are grouped. Default empty.
* @param int $expiration Optional. When to expire the cache contents, in seconds.
* Default 0 (no expiration).
* @return bool[] Array of return values, grouped by key. Each value is either
* true on success, or false on failure.
*/
public function setMultiple( array $items, $group = '', $expiration = 0 ) {
$values = array();
foreach ( $items as $key => $value ) {
$values[ $key ] = $this->set( $key, $value, $group, $expiration );
}
return $values;
}
/**
* Sets a Memcached option.
*
* @link https://www.php.net/manual/en/memcached.setoption.php
*
* @param int $option Option name.
* @param mixed $value Option value.
* @return bool True on success, false on failure.
*/
public function setOption( $option, $value ) {
return $this->m->setOption( $option, $value );
}
/**
* Builds a key for the cached object using the blog_id, key, and group values.
*
* This function is inspired by the original WP Memcached Object cache.
*
* @author Ryan Boren
* @link http://wordpress.org/extend/plugins/memcached/
*
* @param string $key The key under which to store the value.
* @param string $group The group value appended to the $key.
* @return string
*/
public function buildKey( $key, $group = 'default' ) {
if ( empty( $group ) ) {
$group = 'default';
}
if ( false !== array_search( $group, $this->global_groups, true ) ) {
$prefix = $this->global_prefix;
} else {
$prefix = $this->blog_prefix;
}
return preg_replace( '/\s+/', '', WP_CACHE_KEY_SALT . "$prefix$group:$key" );
}
/**
* Creates an array of keys from passed key(s) and group(s).
*
* This function takes a string or array of key(s) and group(s) and combines them into a single dimensional
* array that merges the keys and groups. If the same number of keys and groups exist, the final keys will
* append $groups[n] to $keys[n]. If there are more keys than groups and the $groups parameter is an array,
* $keys[n] will be combined with $groups[n] until $groups runs out of values. 'default' will be used for remaining
* values. If $keys is an array and $groups is a string, all final values will append $groups to $keys[n].
* If both values are strings, they will be combined into a single string. Note that if more $groups are received
* than $keys, the method will return an empty array. This method is primarily a helper method for methods
* that call memcached with an array of keys.
*
* @param string|array $keys Key(s) to merge with group(s).
* @param string|array $groups Group(s) to merge with key(s).
* @return array Array that combines keys and groups into a single set of memcached keys.
*/
public function buildKeys( $keys, $groups = 'default' ) {
$derived_keys = array();
// If strings sent, convert to arrays for proper handling.
if ( ! is_array( $groups ) ) {
$groups = (array) $groups;
}
if ( ! is_array( $keys ) ) {
$keys = (array) $keys;
}
$keys = array_values( $keys );
// If we have equal numbers of keys and groups, merge $keys[n] and $group[n].
if ( count( $keys ) === count( $groups ) ) {
for ( $i = 0; $i < count( $keys ); $i++ ) {
$derived_keys[] = $this->buildKey( $keys[ $i ], $groups[ $i ] );
}
// If more keys are received than groups, merge $keys[n] and $group[n]
// until no more groups are left; remaining groups are 'default'.
} elseif ( count( $keys ) > count( $groups ) ) {
for ( $i = 0; $i < count( $keys ); $i++ ) {
if ( isset( $groups[ $i ] ) ) {
$derived_keys[] = $this->buildKey( $keys[ $i ], $groups[ $i ] );
} elseif ( count( $groups ) === 1 ) {
$derived_keys[] = $this->buildKey( $keys[ $i ], $groups[0] );
} else {
$derived_keys[] = $this->buildKey( $keys[ $i ], 'default' );
}
}
}
return $derived_keys;
}
/**
* Ensures that a proper expiration time is set.
*
* Memcached treats any value over 30 days as a timestamp. If a developer sets the expiration
* for greater than 30 days or less than the current timestamp, the timestamp is in the past
* and the value isn't cached. This function detects values in that range and corrects them.
*
* @param string|int $expiration The dirty expiration time.
* @return string|int The sanitized expiration time.
*/
public function sanitize_expiration( $expiration ) {
if ( $expiration > $this->thirty_days && $expiration <= $this->now ) {
$expiration = $expiration + $this->now;
}
return $expiration;
}
/**
* Concatenates two values and casts to type of the first value.
*
* This is used in append and prepend operations to match how these functions are handled
* by memcached. In both cases, whichever value is the original value in the combined value
* will dictate the type of the combined value.
*
* @param mixed $original Original value that dictates the combined type.
* @param mixed $pended Value to combine with original value.
* @param string $direction Either 'pre' or 'app'.
* @return mixed Combined value casted to the type of the first value.
*/
public function combine_values( $original, $pended, $direction ) {
$type = gettype( $original );
// Combine the values based on direction of the "pend".
if ( 'pre' === $direction ) {
$combined = $pended . $original;
} else {
$combined = $original . $pended;
}
// Cast type of combined value.
settype( $combined, $type );
return $combined;
}
/**
* Simple wrapper for saving object to the internal cache.
*
* @param string $derived_key Key to save value under.
* @param mixed $value Object value.
*/
public function add_to_internal_cache( $derived_key, $value ) {
if ( is_object( $value ) ) {
$value = clone $value;
}
$this->cache[ $derived_key ] = $value;
}
/**
* Determines if a no_mc_group exists in a group of groups.
*
* @param mixed $groups The groups to search.
* @return bool True if a no_mc_group is present; false if a no_mc_group is not present.
*/
public function contains_no_mc_group( $groups ) {
if ( is_scalar( $groups ) ) {
return in_array( $groups, $this->no_mc_groups, true );
}
if ( ! is_array( $groups ) ) {
return false;
}
foreach ( $groups as $group ) {
if ( in_array( $group, $this->no_mc_groups, true ) ) {
return true;
}
}
return false;
}
/**
* Adds global groups.
*
* This function comes straight from the original WP Memcached Object cache.
*
* @author Ryan Boren
* @link http://wordpress.org/extend/plugins/memcached/
*
* @param array $groups Array of groups.
* @return void
*/
public function add_global_groups( $groups ) {
if ( ! is_array( $groups ) ) {
$groups = (array) $groups;
}
$this->global_groups = array_merge( $this->global_groups, $groups );
$this->global_groups = array_unique( $this->global_groups );
}
/**
* Adds non-persistent groups.
*
* This function comes straight from the original WP Memcached Object cache.
*
* @author Ryan Boren
* @link http://wordpress.org/extend/plugins/memcached/
*
* @param array $groups Array of groups.
* @return void
*/
public function add_non_persistent_groups( $groups ) {
if ( ! is_array( $groups ) ) {
$groups = (array) $groups;
}
$this->no_mc_groups = array_merge( $this->no_mc_groups, $groups );
$this->no_mc_groups = array_unique( $this->no_mc_groups );
}
/**
* Gets a value specifically from the internal, run-time cache, not memcached.
*
* @param int|string $key Key value.
* @param int|string $group Group that the value belongs to.
* @return bool|mixed Value on success, false on failure.
*/
public function get_from_runtime_cache( $key, $group ) {
$derived_key = $this->buildKey( $key, $group );
if ( isset( $this->cache[ $derived_key ] ) ) {
return $this->cache[ $derived_key ];
}
return false;
}
/**
* Switches blog prefix, which changes the cache that is accessed.
*
* @param int $blog_id Blog to switch to.
* @return void
*/
public function switch_to_blog( $blog_id ) {
global $table_prefix;
$blog_id = (int) $blog_id;
$this->blog_prefix = ( is_multisite() ? $blog_id : $table_prefix ) . ':';
}
}
// phpcs:enable
|