forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.php
More file actions
494 lines (403 loc) · 13.3 KB
/
cache.php
File metadata and controls
494 lines (403 loc) · 13.3 KB
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
<?php
/**
* @group cache
*/
class Tests_Cache extends WP_UnitTestCase {
public $cache = null;
public function set_up() {
parent::set_up();
$this->cache =& $this->init_cache();
}
public function tear_down() {
$this->flush_cache();
parent::tear_down();
}
private function &init_cache() {
global $wp_object_cache;
$cache_class = get_class( $wp_object_cache );
$cache = new $cache_class();
$cache->add_global_groups( array( 'global-cache-test' ) );
return $cache;
}
/**
* @ticket 56198
*
* @covers WP_Object_Cache::is_valid_key
* @dataProvider data_is_valid_key
*/
public function test_is_valid_key( $key, $valid ) {
if ( wp_using_ext_object_cache() ) {
$this->markTestSkipped( 'This test requires that an external object cache is not in use.' );
}
$val = 'val';
if ( $valid ) {
$this->assertTrue( $this->cache->add( $key, $val ), 'WP_Object_Cache:add() should return true for valid keys.' );
$this->assertSame( $val, $this->cache->get( $key ), 'The retrieved value should match the added value.' );
} else {
$this->setExpectedIncorrectUsage( 'WP_Object_Cache::add' );
$this->assertFalse( $this->cache->add( $key, $val ), 'WP_Object_Cache:add() should return false for invalid keys.' );
}
}
/**
* Data provider for test_is_valid_key().
*
* @return array[] Test parameters {
* @type mixed $key Cache key value.
* @type bool $valid Whether the key should be considered valid.
* }
*/
public function data_is_valid_key() {
return array(
'false' => array( false, false ),
'null' => array( null, false ),
'line break' => array( "\n", false ),
'null character' => array( "\0", false ),
'empty string' => array( '', false ),
'single space' => array( ' ', false ),
'two spaces' => array( ' ', false ),
'float 0' => array( 0.0, false ),
'int 0' => array( 0, true ),
'int 1' => array( 1, true ),
'string 0' => array( '0', true ),
'string' => array( 'key', true ),
);
}
public function test_miss() {
$this->assertFalse( $this->cache->get( 'test_miss' ) );
}
public function test_add_get() {
$key = __FUNCTION__;
$val = 'val';
$this->cache->add( $key, $val );
$this->assertSame( $val, $this->cache->get( $key ) );
}
public function test_add_get_0() {
$key = __FUNCTION__;
$val = 0;
// You can store zero in the cache.
$this->assertTrue( $this->cache->add( $key, $val ) );
$this->assertSame( $val, $this->cache->get( $key ) );
}
/**
* @ticket 20004
*/
public function test_add_get_null() {
$key = __FUNCTION__;
$val = null;
// You can store `null` in the cache.
$this->assertTrue( $this->cache->add( $key, $val ) );
$this->assertSame( $val, $this->cache->get( $key ) );
}
/**
* @ticket 20004
*/
public function test_add_get_false() {
$key = __FUNCTION__;
$val = false;
// You can store `false` in the cache.
$this->assertTrue( $this->cache->add( $key, $val ) );
$this->assertSame( $val, $this->cache->get( $key ) );
}
public function test_add() {
$key = __FUNCTION__;
$val1 = 'val1';
$val2 = 'val2';
// Add $key to the cache.
$this->assertTrue( $this->cache->add( $key, $val1 ) );
$this->assertSame( $val1, $this->cache->get( $key ) );
// $key is in the cache, so reject new calls to add().
$this->assertFalse( $this->cache->add( $key, $val2 ) );
$this->assertSame( $val1, $this->cache->get( $key ) );
}
public function test_replace() {
$key = __FUNCTION__;
$val = 'val1';
$val2 = 'val2';
// memcached rejects replace() if the key does not exist.
$this->assertFalse( $this->cache->replace( $key, $val ) );
$this->assertFalse( $this->cache->get( $key ) );
$this->assertTrue( $this->cache->add( $key, $val ) );
$this->assertSame( $val, $this->cache->get( $key ) );
$this->assertTrue( $this->cache->replace( $key, $val2 ) );
$this->assertSame( $val2, $this->cache->get( $key ) );
}
public function test_wp_cache_replace() {
$key = 'my-key';
$val1 = 'first-val';
$val2 = 'second-val';
$fake_key = 'my-fake-key';
// Save the first value to cache and verify.
wp_cache_set( $key, $val1 );
$this->assertSame( $val1, wp_cache_get( $key ) );
// Replace the value and verify.
wp_cache_replace( $key, $val2 );
$this->assertSame( $val2, wp_cache_get( $key ) );
// Non-existent key should fail.
$this->assertFalse( wp_cache_replace( $fake_key, $val1 ) );
// Make sure $fake_key is not stored.
$this->assertFalse( wp_cache_get( $fake_key ) );
}
public function test_set() {
$key = __FUNCTION__;
$val1 = 'val1';
$val2 = 'val2';
// memcached accepts set() if the key does not exist.
$this->assertTrue( $this->cache->set( $key, $val1 ) );
$this->assertSame( $val1, $this->cache->get( $key ) );
// Second set() with same key should be allowed.
$this->assertTrue( $this->cache->set( $key, $val2 ) );
$this->assertSame( $val2, $this->cache->get( $key ) );
}
public function test_flush() {
if ( wp_using_ext_object_cache() ) {
$this->markTestSkipped( 'This test requires that an external object cache is not in use.' );
}
$key = __FUNCTION__;
$val = 'val';
$this->cache->add( $key, $val );
// Item is visible to both cache objects.
$this->assertSame( $val, $this->cache->get( $key ) );
$this->cache->flush();
// If there is no value get returns false.
$this->assertFalse( $this->cache->get( $key ) );
}
/**
* @ticket 4476
* @ticket 9773
*
* @covers ::wp_cache_flush_group
*/
public function test_wp_cache_flush_group() {
$key = 'my-key';
$val = 'my-val';
wp_cache_set( $key, $val, 'group-test' );
wp_cache_set( $key, $val, 'group-kept' );
$this->assertSame( $val, wp_cache_get( $key, 'group-test' ), 'group-test should contain my-val' );
if ( wp_using_ext_object_cache() ) {
$this->setExpectedIncorrectUsage( 'wp_cache_flush_group' );
}
$results = wp_cache_flush_group( 'group-test' );
if ( wp_using_ext_object_cache() ) {
$this->assertFalse( $results );
} else {
$this->assertTrue( $results );
$this->assertFalse( wp_cache_get( $key, 'group-test' ), 'group-test should return false' );
$this->assertSame( $val, wp_cache_get( $key, 'group-kept' ), 'group-kept should still contain my-val' );
}
}
// Make sure objects are cloned going to and from the cache.
public function test_object_refs() {
$key = __FUNCTION__ . '_1';
$object_a = new stdClass();
$object_a->foo = 'alpha';
$this->cache->set( $key, $object_a );
$object_a->foo = 'bravo';
$object_b = $this->cache->get( $key );
$this->assertSame( 'alpha', $object_b->foo );
$object_b->foo = 'charlie';
$this->assertSame( 'bravo', $object_a->foo );
$key = __FUNCTION__ . '_2';
$object_a = new stdClass();
$object_a->foo = 'alpha';
$this->cache->add( $key, $object_a );
$object_a->foo = 'bravo';
$object_b = $this->cache->get( $key );
$this->assertSame( 'alpha', $object_b->foo );
$object_b->foo = 'charlie';
$this->assertSame( 'bravo', $object_a->foo );
}
public function test_incr() {
$key = __FUNCTION__;
$this->assertFalse( $this->cache->incr( $key ) );
$this->cache->set( $key, 0 );
$this->cache->incr( $key );
$this->assertSame( 1, $this->cache->get( $key ) );
$this->cache->incr( $key, 2 );
$this->assertSame( 3, $this->cache->get( $key ) );
}
public function test_wp_cache_incr() {
$key = __FUNCTION__;
$this->assertFalse( wp_cache_incr( $key ) );
wp_cache_set( $key, 0 );
wp_cache_incr( $key );
$this->assertSame( 1, wp_cache_get( $key ) );
wp_cache_incr( $key, 2 );
$this->assertSame( 3, wp_cache_get( $key ) );
}
public function test_decr() {
$key = __FUNCTION__;
$this->assertFalse( $this->cache->decr( $key ) );
$this->cache->set( $key, 0 );
$this->cache->decr( $key );
$this->assertSame( 0, $this->cache->get( $key ) );
$this->cache->set( $key, 3 );
$this->cache->decr( $key );
$this->assertSame( 2, $this->cache->get( $key ) );
$this->cache->decr( $key, 2 );
$this->assertSame( 0, $this->cache->get( $key ) );
}
/**
* @ticket 21327
*/
public function test_wp_cache_decr() {
$key = __FUNCTION__;
$this->assertFalse( wp_cache_decr( $key ) );
wp_cache_set( $key, 0 );
wp_cache_decr( $key );
$this->assertSame( 0, wp_cache_get( $key ) );
wp_cache_set( $key, 3 );
wp_cache_decr( $key );
$this->assertSame( 2, wp_cache_get( $key ) );
wp_cache_decr( $key, 2 );
$this->assertSame( 0, wp_cache_get( $key ) );
}
public function test_delete() {
$key = __FUNCTION__;
$val = 'val';
// Verify set.
$this->assertTrue( $this->cache->set( $key, $val ) );
$this->assertSame( $val, $this->cache->get( $key ) );
// Verify successful delete.
$this->assertTrue( $this->cache->delete( $key ) );
$this->assertFalse( $this->cache->get( $key ) );
$this->assertFalse( $this->cache->delete( $key, 'default' ) );
}
public function test_wp_cache_delete() {
$key = __FUNCTION__;
$val = 'val';
// Verify set.
$this->assertTrue( wp_cache_set( $key, $val ) );
$this->assertSame( $val, wp_cache_get( $key ) );
// Verify successful delete.
$this->assertTrue( wp_cache_delete( $key ) );
$this->assertFalse( wp_cache_get( $key ) );
// wp_cache_delete() does not have a $force method.
// Delete returns (bool) true when key is not set and $force is true.
// $this->assertTrue( wp_cache_delete( $key, 'default', true ) );
$this->assertFalse( wp_cache_delete( $key, 'default' ) );
}
public function test_switch_to_blog() {
if ( ! method_exists( $this->cache, 'switch_to_blog' ) ) {
$this->markTestSkipped( 'This test requires a switch_to_blog() method on the cache object.' );
}
$key = __FUNCTION__;
$val = 'val1';
$val2 = 'val2';
if ( ! is_multisite() ) {
// Single site ignores switch_to_blog().
$this->assertTrue( $this->cache->set( $key, $val ) );
$this->assertSame( $val, $this->cache->get( $key ) );
$this->cache->switch_to_blog( 999 );
$this->assertSame( $val, $this->cache->get( $key ) );
$this->assertTrue( $this->cache->set( $key, $val2 ) );
$this->assertSame( $val2, $this->cache->get( $key ) );
$this->cache->switch_to_blog( get_current_blog_id() );
$this->assertSame( $val2, $this->cache->get( $key ) );
} else {
// Multisite should have separate per-blog caches.
$this->assertTrue( $this->cache->set( $key, $val ) );
$this->assertSame( $val, $this->cache->get( $key ) );
$this->cache->switch_to_blog( 999 );
$this->assertFalse( $this->cache->get( $key ) );
$this->assertTrue( $this->cache->set( $key, $val2 ) );
$this->assertSame( $val2, $this->cache->get( $key ) );
$this->cache->switch_to_blog( get_current_blog_id() );
$this->assertSame( $val, $this->cache->get( $key ) );
$this->cache->switch_to_blog( 999 );
$this->assertSame( $val2, $this->cache->get( $key ) );
$this->cache->switch_to_blog( get_current_blog_id() );
$this->assertSame( $val, $this->cache->get( $key ) );
}
// Global group.
$this->assertTrue( $this->cache->set( $key, $val, 'global-cache-test' ) );
$this->assertSame( $val, $this->cache->get( $key, 'global-cache-test' ) );
$this->cache->switch_to_blog( 999 );
$this->assertSame( $val, $this->cache->get( $key, 'global-cache-test' ) );
$this->assertTrue( $this->cache->set( $key, $val2, 'global-cache-test' ) );
$this->assertSame( $val2, $this->cache->get( $key, 'global-cache-test' ) );
$this->cache->switch_to_blog( get_current_blog_id() );
$this->assertSame( $val2, $this->cache->get( $key, 'global-cache-test' ) );
}
public function test_wp_cache_init() {
$new_blank_cache_object = new WP_Object_Cache();
wp_cache_init();
global $wp_object_cache;
if ( wp_using_ext_object_cache() ) {
// External caches will contain property values that contain non-matching resource IDs.
$this->assertInstanceOf( 'WP_Object_Cache', $wp_object_cache );
} else {
$this->assertEquals( $wp_object_cache, $new_blank_cache_object );
}
}
/**
* @ticket 54574
*/
public function test_wp_cache_add_multiple() {
$found = wp_cache_add_multiple(
array(
'foo1' => 'bar',
'foo2' => 'bar',
'foo3' => 'bar',
),
'group1'
);
$expected = array(
'foo1' => true,
'foo2' => true,
'foo3' => true,
);
$this->assertSame( $expected, $found );
}
/**
* @ticket 54574
*/
public function test_wp_cache_set_multiple() {
$found = wp_cache_set_multiple(
array(
'foo1' => 'bar',
'foo2' => 'bar',
'foo3' => 'bar',
),
'group1'
);
$expected = array(
'foo1' => true,
'foo2' => true,
'foo3' => true,
);
$this->assertSame( $expected, $found );
}
/**
* @ticket 20875
*/
public function test_wp_cache_get_multiple() {
wp_cache_set( 'foo1', 'bar', 'group1' );
wp_cache_set( 'foo2', 'bar', 'group1' );
wp_cache_set( 'foo1', 'bar', 'group2' );
$found = wp_cache_get_multiple( array( 'foo1', 'foo2', 'foo3' ), 'group1' );
$expected = array(
'foo1' => 'bar',
'foo2' => 'bar',
'foo3' => false,
);
$this->assertSame( $expected, $found );
}
/**
* @ticket 54574
*/
public function test_wp_cache_delete_multiple() {
wp_cache_set( 'foo1', 'bar', 'group1' );
wp_cache_set( 'foo2', 'bar', 'group1' );
wp_cache_set( 'foo3', 'bar', 'group2' );
$found = wp_cache_delete_multiple(
array( 'foo1', 'foo2', 'foo3' ),
'group1'
);
$expected = array(
'foo1' => true,
'foo2' => true,
'foo3' => false,
);
$this->assertSame( $expected, $found );
}
}