forked from wp-cli/cache-command
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache_Command.php
More file actions
409 lines (375 loc) · 9.59 KB
/
Cache_Command.php
File metadata and controls
409 lines (375 loc) · 9.59 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
<?php
/**
* Adds, removes, fetches, and flushes the WP Object Cache object.
*
* By default, the WP Object Cache exists in PHP memory for the length of the
* request (and is emptied at the end). Use a persistent object cache drop-in
* to persist the object cache between requests.
*
* [Read the codex article](https://codex.wordpress.org/Class_Reference/WP_Object_Cache)
* for more detail.
*
* ## EXAMPLES
*
* # Set cache.
* $ wp cache set my_key my_value my_group 300
* Success: Set object 'my_key' in group 'my_group'.
*
* # Get cache.
* $ wp cache get my_key my_group
* my_value
*
* @package wp-cli
*/
class Cache_Command extends WP_CLI_Command {
/**
* Adds a value to the object cache.
*
* Errors if a value already exists for the key, which means the value can't
* be added.
*
* ## OPTIONS
*
* <key>
* : Cache key.
*
* <value>
* : Value to add to the key.
*
* [<group>]
* : Method for grouping data within the cache which allows the same key to be used across groups.
* ---
* default: default
* ---
*
* [<expiration>]
* : Define how long to keep the value, in seconds. `0` means as long as possible.
* ---
* default: 0
* ---
*
* ## EXAMPLES
*
* # Add cache.
* $ wp cache add my_key my_group my_value 300
* Success: Added object 'my_key' in group 'my_value'.
*/
public function add( $args, $assoc_args ) {
list( $key, $value, $group, $expiration ) = $args;
if ( ! wp_cache_add( $key, $value, $group, $expiration ) ) {
WP_CLI::error( "Could not add object '$key' in group '$group'. Does it already exist?" );
}
WP_CLI::success( "Added object '$key' in group '$group'." );
}
/**
* Decrements a value in the object cache.
*
* Errors if the value can't be decremented.
*
* ## OPTIONS
*
* <key>
* : Cache key.
*
* [<offset>]
* : The amount by which to decrement the item's value.
* ---
* default: 1
* ---
*
* [<group>]
* : Method for grouping data within the cache which allows the same key to be used across groups.
* ---
* default: default
* ---
*
* ## EXAMPLES
*
* # Decrease cache value.
* $ wp cache decr my_key 2 my_group
* 48
*/
public function decr( $args, $assoc_args ) {
list( $key, $offset, $group ) = $args;
$value = wp_cache_decr( $key, $offset, $group );
if ( false === $value ) {
WP_CLI::error( 'The value was not decremented.' );
}
WP_CLI::print_value( $value, $assoc_args );
}
/**
* Removes a value from the object cache.
*
* Errors if the value can't be deleted.
*
* ## OPTIONS
*
* <key>
* : Cache key.
*
* [<group>]
* : Method for grouping data within the cache which allows the same key to be used across groups.
* ---
* default: default
* ---
*
* ## EXAMPLES
*
* # Delete cache.
* $ wp cache delete my_key my_group
* Success: Object deleted.
*/
public function delete( $args, $assoc_args ) {
list( $key, $group ) = $args;
$result = wp_cache_delete( $key, $group );
if ( false === $result ) {
WP_CLI::error( 'The object was not deleted.' );
}
WP_CLI::success( 'Object deleted.' );
}
/**
* Flushes the object cache.
*
* For WordPress multisite instances using a persistent object cache,
* flushing the object cache will typically flush the cache for all sites.
* Beware of the performance impact when flushing the object cache in
* production.
*
* Errors if the object cache can't be flushed.
*
* ## EXAMPLES
*
* # Flush cache.
* $ wp cache flush
* Success: The cache was flushed.
*/
public function flush( $args, $assoc_args ) {
if ( WP_CLI::has_config( 'url' ) && ! empty( WP_CLI::get_config()['url'] ) && is_multisite() ) {
WP_CLI::warning( 'Flushing the cache may affect all sites in a multisite installation, depending on the implementation of the object cache.' );
}
$value = wp_cache_flush();
if ( false === $value ) {
WP_CLI::error( 'The object cache could not be flushed.' );
}
WP_CLI::success( 'The cache was flushed.' );
}
/**
* Gets a value from the object cache.
*
* Errors if the value doesn't exist.
*
* ## OPTIONS
*
* <key>
* : Cache key.
*
* [<group>]
* : Method for grouping data within the cache which allows the same key to be used across groups.
* ---
* default: default
* ---
*
* ## EXAMPLES
*
* # Get cache.
* $ wp cache get my_key my_group
* my_value
*/
public function get( $args, $assoc_args ) {
list( $key, $group ) = $args;
$value = wp_cache_get( $key, $group );
if ( false === $value ) {
WP_CLI::error( "Object with key '$key' and group '$group' not found." );
}
WP_CLI::print_value( $value, $assoc_args );
}
/**
* Increments a value in the object cache.
*
* Errors if the value can't be incremented.
*
* ## OPTIONS
*
* <key>
* : Cache key.
*
* [<offset>]
* : The amount by which to increment the item's value.
* ---
* default: 1
* ---
*
* [<group>]
* : Method for grouping data within the cache which allows the same key to be used across groups.
* ---
* default: default
* ---
*
* ## EXAMPLES
*
* # Increase cache value.
* $ wp cache incr my_key 2 my_group
* 50
*/
public function incr( $args, $assoc_args ) {
list( $key, $offset, $group ) = $args;
$value = wp_cache_incr( $key, $offset, $group );
if ( false === $value ) {
WP_CLI::error( 'The value was not incremented.' );
}
WP_CLI::print_value( $value, $assoc_args );
}
/**
* Replaces a value in the object cache, if the value already exists.
*
* Errors if the value can't be replaced.
*
* ## OPTIONS
*
* <key>
* : Cache key.
*
* <value>
* : Value to replace.
*
* [<group>]
* : Method for grouping data within the cache which allows the same key to be used across groups.
* ---
* default: default
* ---
*
* [<expiration>]
* : Define how long to keep the value, in seconds. `0` means as long as possible.
* ---
* default: 0
* ---
*
* ## EXAMPLES
*
* # Replace cache.
* $ wp cache replace my_key new_value my_group
* Success: Replaced object 'my_key' in group 'my_group'.
*/
public function replace( $args, $assoc_args ) {
list( $key, $value, $group, $expiration ) = $args;
$result = wp_cache_replace( $key, $value, $group, $expiration );
if ( false === $result ) {
WP_CLI::error( "Could not replace object '$key' in group '$group'. Does it not exist?" );
}
WP_CLI::success( "Replaced object '$key' in group '$group'." );
}
/**
* Sets a value to the object cache, regardless of whether it already exists.
*
* Errors if the value can't be set.
*
* ## OPTIONS
*
* <key>
* : Cache key.
*
* <value>
* : Value to set on the key.
*
* [<group>]
* : Method for grouping data within the cache which allows the same key to be used across groups.
* ---
* default: default
* ---
*
* [<expiration>]
* : Define how long to keep the value, in seconds. `0` means as long as possible.
* ---
* default: 0
* ---
*
* ## EXAMPLES
*
* # Set cache.
* $ wp cache set my_key my_value my_group 300
* Success: Set object 'my_key' in group 'my_group'.
*/
public function set( $args, $assoc_args ) {
list( $key, $value, $group, $expiration ) = $args;
$result = wp_cache_set( $key, $value, $group, $expiration );
if ( false === $result ) {
WP_CLI::error( "Could not add object '$key' in group '$group'." );
}
WP_CLI::success( "Set object '$key' in group '$group'." );
}
/**
* Attempts to determine which object cache is being used.
*
* Note that the guesses made by this function are based on the
* WP_Object_Cache classes that define the 3rd party object cache extension.
* Changes to those classes could render problems with this function's
* ability to determine which object cache is being used.
*
* ## EXAMPLES
*
* # Check cache type.
* $ wp cache type
* Default
*/
public function type( $args, $assoc_args ) {
$message = WP_CLI\Utils\wp_get_cache_type();
WP_CLI::line( $message );
}
/**
* Determines whether the object cache implementation supports a particular feature.
*
* ## OPTIONS
*
* <feature>
* : Name of the feature to check for.
*
* ## EXAMPLES
*
* # Check whether is add_multiple supported.
* $ wp cache supports add_multiple
* $ echo $?
* 0
*
* # Bash script for checking whether for support like this:
* if ! wp cache supports non_existing; then
* echo 'non_existing is not supported'
* fi
*/
public function supports( $args, $assoc_args ) {
list ( $feature ) = $args;
if ( ! function_exists( 'wp_cache_supports' ) ) {
WP_CLI::error( 'Checking cache features is only available in WordPress 6.1 and higher' );
}
$supports = wp_cache_supports( $feature );
if ( $supports ) {
WP_CLI::halt( 0 );
}
WP_CLI::halt( 1 );
}
/**
* Removes all cache items in a group, if the object cache implementation supports it.
*
* ## OPTIONS
*
* <group>
* : Cache group key.
*
* ## EXAMPLES
*
* # Clear cache group.
* $ wp cache flush-group my_group
* Success: Cache group 'my_group' was flushed.
*
* @subcommand flush-group
*/
public function flush_group( $args, $assoc_args ) {
list( $group ) = $args;
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
WP_CLI::error( 'Group flushing is not supported.' );
}
if ( ! wp_cache_flush_group( $group ) ) {
WP_CLI::error( "Cache group '$group' was not flushed." );
}
WP_CLI::success( "Cache group '$group' was flushed." );
}
}