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
342 lines (315 loc) · 7.77 KB
/
Cache_Command.php
File metadata and controls
342 lines (315 loc) · 7.77 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
<?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 ) {
$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 );
}
}