author | Anthony Ly <anthonyly.com@gmail.com> |
Mon, 19 Nov 2012 18:26:13 +0100 | |
changeset 194 | 32102edaa81b |
parent 136 | bde1974c263b |
child 204 | 09a1c134465b |
permissions | -rw-r--r-- |
136 | 1 |
<?php |
2 |
/** |
|
3 |
* Object Cache API |
|
4 |
* |
|
5 |
* @link http://codex.wordpress.org/Function_Reference/WP_Cache |
|
6 |
* |
|
7 |
* @package WordPress |
|
8 |
* @subpackage Cache |
|
9 |
*/ |
|
10 |
||
11 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
12 |
* Adds data to the cache, if the cache key doesn't already exist. |
136 | 13 |
* |
14 |
* @since 2.0.0 |
|
15 |
* @uses $wp_object_cache Object Cache Class |
|
16 |
* @see WP_Object_Cache::add() |
|
17 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
18 |
* @param int|string $key The cache key to use for retrieval later |
136 | 19 |
* @param mixed $data The data to add to the cache store |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
20 |
* @param string $group The group to add the cache to |
136 | 21 |
* @param int $expire When the cache data should be expired |
22 |
* @return unknown |
|
23 |
*/ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
24 |
function wp_cache_add($key, $data, $group = '', $expire = 0) { |
136 | 25 |
global $wp_object_cache; |
26 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
27 |
return $wp_object_cache->add($key, $data, $group, $expire); |
136 | 28 |
} |
29 |
||
30 |
/** |
|
31 |
* Closes the cache. |
|
32 |
* |
|
33 |
* This function has ceased to do anything since WordPress 2.5. The |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
34 |
* functionality was removed along with the rest of the persistent cache. This |
136 | 35 |
* does not mean that plugins can't implement this function when they need to |
36 |
* make sure that the cache is cleaned up after WordPress no longer needs it. |
|
37 |
* |
|
38 |
* @since 2.0.0 |
|
39 |
* |
|
40 |
* @return bool Always returns True |
|
41 |
*/ |
|
42 |
function wp_cache_close() { |
|
43 |
return true; |
|
44 |
} |
|
45 |
||
46 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
47 |
* Decrement numeric cache item's value |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
48 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
49 |
* @since 3.3.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
50 |
* @uses $wp_object_cache Object Cache Class |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
51 |
* @see WP_Object_Cache::decr() |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
52 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
53 |
* @param int|string $key The cache key to increment |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
54 |
* @param int $offset The amount by which to decrement the item's value. Default is 1. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
55 |
* @param string $group The group the key is in. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
56 |
* @return false|int False on failure, the item's new value on success. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
57 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
58 |
function wp_cache_decr( $key, $offset = 1, $group = '' ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
59 |
global $wp_object_cache; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
60 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
61 |
return $wp_object_cache->decr( $key, $offset, $group ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
62 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
63 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
64 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
65 |
* Removes the cache contents matching key and group. |
136 | 66 |
* |
67 |
* @since 2.0.0 |
|
68 |
* @uses $wp_object_cache Object Cache Class |
|
69 |
* @see WP_Object_Cache::delete() |
|
70 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
71 |
* @param int|string $key What the contents in the cache are called |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
72 |
* @param string $group Where the cache contents are grouped |
136 | 73 |
* @return bool True on successful removal, false on failure |
74 |
*/ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
75 |
function wp_cache_delete($key, $group = '') { |
136 | 76 |
global $wp_object_cache; |
77 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
78 |
return $wp_object_cache->delete($key, $group); |
136 | 79 |
} |
80 |
||
81 |
/** |
|
82 |
* Removes all cache items. |
|
83 |
* |
|
84 |
* @since 2.0.0 |
|
85 |
* @uses $wp_object_cache Object Cache Class |
|
86 |
* @see WP_Object_Cache::flush() |
|
87 |
* |
|
88 |
* @return bool Always returns true |
|
89 |
*/ |
|
90 |
function wp_cache_flush() { |
|
91 |
global $wp_object_cache; |
|
92 |
||
93 |
return $wp_object_cache->flush(); |
|
94 |
} |
|
95 |
||
96 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
97 |
* Retrieves the cache contents from the cache by key and group. |
136 | 98 |
* |
99 |
* @since 2.0.0 |
|
100 |
* @uses $wp_object_cache Object Cache Class |
|
101 |
* @see WP_Object_Cache::get() |
|
102 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
103 |
* @param int|string $key What the contents in the cache are called |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
104 |
* @param string $group Where the cache contents are grouped |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
105 |
* @param bool $force Whether to force an update of the local cache from the persistent cache (default is false) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
106 |
* @param &bool $found Whether key was found in the cache. Disambiguates a return of false, a storable value. |
136 | 107 |
* @return bool|mixed False on failure to retrieve contents or the cache |
108 |
* contents on success |
|
109 |
*/ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
110 |
function wp_cache_get( $key, $group = '', $force = false, &$found = null ) { |
136 | 111 |
global $wp_object_cache; |
112 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
113 |
return $wp_object_cache->get( $key, $group, $force, $found ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
114 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
115 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
116 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
117 |
* Increment numeric cache item's value |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
118 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
119 |
* @since 3.3.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
120 |
* @uses $wp_object_cache Object Cache Class |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
121 |
* @see WP_Object_Cache::incr() |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
122 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
123 |
* @param int|string $key The cache key to increment |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
124 |
* @param int $offset The amount by which to increment the item's value. Default is 1. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
125 |
* @param string $group The group the key is in. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
126 |
* @return false|int False on failure, the item's new value on success. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
127 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
128 |
function wp_cache_incr( $key, $offset = 1, $group = '' ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
129 |
global $wp_object_cache; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
130 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
131 |
return $wp_object_cache->incr( $key, $offset, $group ); |
136 | 132 |
} |
133 |
||
134 |
/** |
|
135 |
* Sets up Object Cache Global and assigns it. |
|
136 |
* |
|
137 |
* @since 2.0.0 |
|
138 |
* @global WP_Object_Cache $wp_object_cache WordPress Object Cache |
|
139 |
*/ |
|
140 |
function wp_cache_init() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
141 |
$GLOBALS['wp_object_cache'] = new WP_Object_Cache(); |
136 | 142 |
} |
143 |
||
144 |
/** |
|
145 |
* Replaces the contents of the cache with new data. |
|
146 |
* |
|
147 |
* @since 2.0.0 |
|
148 |
* @uses $wp_object_cache Object Cache Class |
|
149 |
* @see WP_Object_Cache::replace() |
|
150 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
151 |
* @param int|string $key What to call the contents in the cache |
136 | 152 |
* @param mixed $data The contents to store in the cache |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
153 |
* @param string $group Where to group the cache contents |
136 | 154 |
* @param int $expire When to expire the cache contents |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
155 |
* @return bool False if cache key and group already exist, true on success |
136 | 156 |
*/ |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
157 |
function wp_cache_replace($key, $data, $group = '', $expire = 0) { |
136 | 158 |
global $wp_object_cache; |
159 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
160 |
return $wp_object_cache->replace($key, $data, $group, $expire); |
136 | 161 |
} |
162 |
||
163 |
/** |
|
164 |
* Saves the data to the cache. |
|
165 |
* |
|
166 |
* @since 2.0 |
|
167 |
* @uses $wp_object_cache Object Cache Class |
|
168 |
* @see WP_Object_Cache::set() |
|
169 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
170 |
* @param int|string $key What to call the contents in the cache |
136 | 171 |
* @param mixed $data The contents to store in the cache |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
172 |
* @param string $group Where to group the cache contents |
136 | 173 |
* @param int $expire When to expire the cache contents |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
174 |
* @return bool False if cache key and group already exist, true on success |
136 | 175 |
*/ |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
176 |
function wp_cache_set($key, $data, $group = '', $expire = 0) { |
136 | 177 |
global $wp_object_cache; |
178 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
179 |
return $wp_object_cache->set($key, $data, $group, $expire); |
136 | 180 |
} |
181 |
||
182 |
/** |
|
183 |
* Adds a group or set of groups to the list of global groups. |
|
184 |
* |
|
185 |
* @since 2.6.0 |
|
186 |
* |
|
187 |
* @param string|array $groups A group or an array of groups to add |
|
188 |
*/ |
|
189 |
function wp_cache_add_global_groups( $groups ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
190 |
global $wp_object_cache; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
191 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
192 |
return $wp_object_cache->add_global_groups($groups); |
136 | 193 |
} |
194 |
||
195 |
/** |
|
196 |
* Adds a group or set of groups to the list of non-persistent groups. |
|
197 |
* |
|
198 |
* @since 2.6.0 |
|
199 |
* |
|
200 |
* @param string|array $groups A group or an array of groups to add |
|
201 |
*/ |
|
202 |
function wp_cache_add_non_persistent_groups( $groups ) { |
|
203 |
// Default cache doesn't persist so nothing to do here. |
|
204 |
return; |
|
205 |
} |
|
206 |
||
207 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
208 |
* Reset internal cache keys and structures. If the cache backend uses global blog or site IDs as part of its cache keys, |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
209 |
* this function instructs the backend to reset those keys and perform any cleanup since blog or site IDs have changed since cache init. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
210 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
211 |
* @since 2.6.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
212 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
213 |
function wp_cache_reset() { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
214 |
global $wp_object_cache; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
215 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
216 |
return $wp_object_cache->reset(); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
217 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
218 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
219 |
/** |
136 | 220 |
* WordPress Object Cache |
221 |
* |
|
222 |
* The WordPress Object Cache is used to save on trips to the database. The |
|
223 |
* Object Cache stores all of the cache data to memory and makes the cache |
|
224 |
* contents available by using a key, which is used to name and later retrieve |
|
225 |
* the cache contents. |
|
226 |
* |
|
227 |
* The Object Cache can be replaced by other caching mechanisms by placing files |
|
228 |
* in the wp-content folder which is looked at in wp-settings. If that file |
|
229 |
* exists, then this file will not be included. |
|
230 |
* |
|
231 |
* @package WordPress |
|
232 |
* @subpackage Cache |
|
233 |
* @since 2.0 |
|
234 |
*/ |
|
235 |
class WP_Object_Cache { |
|
236 |
||
237 |
/** |
|
238 |
* Holds the cached objects |
|
239 |
* |
|
240 |
* @var array |
|
241 |
* @access private |
|
242 |
* @since 2.0.0 |
|
243 |
*/ |
|
244 |
var $cache = array (); |
|
245 |
||
246 |
/** |
|
247 |
* The amount of times the cache data was already stored in the cache. |
|
248 |
* |
|
249 |
* @since 2.5.0 |
|
250 |
* @access private |
|
251 |
* @var int |
|
252 |
*/ |
|
253 |
var $cache_hits = 0; |
|
254 |
||
255 |
/** |
|
256 |
* Amount of times the cache did not have the request in cache |
|
257 |
* |
|
258 |
* @var int |
|
259 |
* @access public |
|
260 |
* @since 2.0.0 |
|
261 |
*/ |
|
262 |
var $cache_misses = 0; |
|
263 |
||
264 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
265 |
* List of global groups |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
266 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
267 |
* @var array |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
268 |
* @access protected |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
269 |
* @since 3.0.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
270 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
271 |
var $global_groups = array(); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
272 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
273 |
/** |
136 | 274 |
* Adds data to the cache if it doesn't already exist. |
275 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
276 |
* @uses WP_Object_Cache::_exists Checks to see if the cache already has data. |
136 | 277 |
* @uses WP_Object_Cache::set Sets the data after the checking the cache |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
278 |
* contents existence. |
136 | 279 |
* |
280 |
* @since 2.0.0 |
|
281 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
282 |
* @param int|string $key What to call the contents in the cache |
136 | 283 |
* @param mixed $data The contents to store in the cache |
284 |
* @param string $group Where to group the cache contents |
|
285 |
* @param int $expire When to expire the cache contents |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
286 |
* @return bool False if cache key and group already exist, true on success |
136 | 287 |
*/ |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
288 |
function add( $key, $data, $group = 'default', $expire = '' ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
289 |
if ( wp_suspend_cache_addition() ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
290 |
return false; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
291 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
292 |
if ( empty( $group ) ) |
136 | 293 |
$group = 'default'; |
294 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
295 |
if ( $this->_exists($key, $group) ) |
136 | 296 |
return false; |
297 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
298 |
return $this->set($key, $data, $group, $expire); |
136 | 299 |
} |
300 |
||
301 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
302 |
* Sets the list of global groups. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
303 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
304 |
* @since 3.0.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
305 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
306 |
* @param array $groups List of groups that are global. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
307 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
308 |
function add_global_groups( $groups ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
309 |
$groups = (array) $groups; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
310 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
311 |
$this->global_groups = array_merge($this->global_groups, $groups); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
312 |
$this->global_groups = array_unique($this->global_groups); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
313 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
314 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
315 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
316 |
* Decrement numeric cache item's value |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
317 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
318 |
* @since 3.3.0 |
136 | 319 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
320 |
* @param int|string $key The cache key to increment |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
321 |
* @param int $offset The amount by which to decrement the item's value. Default is 1. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
322 |
* @param string $group The group the key is in. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
323 |
* @return false|int False on failure, the item's new value on success. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
324 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
325 |
function decr( $key, $offset = 1, $group = 'default' ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
326 |
if ( ! $this->_exists( $key, $group ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
327 |
return false; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
328 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
329 |
if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
330 |
$this->cache[ $group ][ $key ] = 0; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
331 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
332 |
$offset = (int) $offset; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
333 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
334 |
$this->cache[ $group ][ $key ] -= $offset; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
335 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
336 |
if ( $this->cache[ $group ][ $key ] < 0 ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
337 |
$this->cache[ $group ][ $key ] = 0; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
338 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
339 |
return $this->cache[ $group ][ $key ]; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
340 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
341 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
342 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
343 |
* Remove the contents of the cache key in the group |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
344 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
345 |
* If the cache key does not exist in the group and $force parameter is set |
136 | 346 |
* to false, then nothing will happen. The $force parameter is set to false |
347 |
* by default. |
|
348 |
* |
|
349 |
* @since 2.0.0 |
|
350 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
351 |
* @param int|string $key What the contents in the cache are called |
136 | 352 |
* @param string $group Where the cache contents are grouped |
353 |
* @param bool $force Optional. Whether to force the unsetting of the cache |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
354 |
* key in the group |
136 | 355 |
* @return bool False if the contents weren't deleted and true on success |
356 |
*/ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
357 |
function delete($key, $group = 'default', $force = false) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
358 |
if ( empty( $group ) ) |
136 | 359 |
$group = 'default'; |
360 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
361 |
if ( ! $force && ! $this->_exists( $key, $group ) ) |
136 | 362 |
return false; |
363 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
364 |
unset( $this->cache[$group][$key] ); |
136 | 365 |
return true; |
366 |
} |
|
367 |
||
368 |
/** |
|
369 |
* Clears the object cache of all data |
|
370 |
* |
|
371 |
* @since 2.0.0 |
|
372 |
* |
|
373 |
* @return bool Always returns true |
|
374 |
*/ |
|
375 |
function flush() { |
|
376 |
$this->cache = array (); |
|
377 |
||
378 |
return true; |
|
379 |
} |
|
380 |
||
381 |
/** |
|
382 |
* Retrieves the cache contents, if it exists |
|
383 |
* |
|
384 |
* The contents will be first attempted to be retrieved by searching by the |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
385 |
* key in the cache group. If the cache is hit (success) then the contents |
136 | 386 |
* are returned. |
387 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
388 |
* On failure, the number of cache misses will be incremented. |
136 | 389 |
* |
390 |
* @since 2.0.0 |
|
391 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
392 |
* @param int|string $key What the contents in the cache are called |
136 | 393 |
* @param string $group Where the cache contents are grouped |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
394 |
* @param string $force Whether to force a refetch rather than relying on the local cache (default is false) |
136 | 395 |
* @return bool|mixed False on failure to retrieve contents or the cache |
396 |
* contents on success |
|
397 |
*/ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
398 |
function get( $key, $group = 'default', $force = false, &$found = null ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
399 |
if ( empty( $group ) ) |
136 | 400 |
$group = 'default'; |
401 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
402 |
if ( $this->_exists( $key, $group ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
403 |
$found = true; |
136 | 404 |
$this->cache_hits += 1; |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
405 |
if ( is_object($this->cache[$group][$key]) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
406 |
return clone $this->cache[$group][$key]; |
136 | 407 |
else |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
408 |
return $this->cache[$group][$key]; |
136 | 409 |
} |
410 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
411 |
$found = false; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
412 |
$this->cache_misses += 1; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
413 |
return false; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
414 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
415 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
416 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
417 |
* Increment numeric cache item's value |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
418 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
419 |
* @since 3.3.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
420 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
421 |
* @param int|string $key The cache key to increment |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
422 |
* @param int $offset The amount by which to increment the item's value. Default is 1. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
423 |
* @param string $group The group the key is in. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
424 |
* @return false|int False on failure, the item's new value on success. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
425 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
426 |
function incr( $key, $offset = 1, $group = 'default' ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
427 |
if ( empty( $group ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
428 |
$group = 'default'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
429 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
430 |
if ( ! $this->_exists( $key, $group ) ) |
136 | 431 |
return false; |
432 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
433 |
if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
434 |
$this->cache[ $group ][ $key ] = 0; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
435 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
436 |
$offset = (int) $offset; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
437 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
438 |
$this->cache[ $group ][ $key ] += $offset; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
439 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
440 |
if ( $this->cache[ $group ][ $key ] < 0 ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
441 |
$this->cache[ $group ][ $key ] = 0; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
442 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
443 |
return $this->cache[ $group ][ $key ]; |
136 | 444 |
} |
445 |
||
446 |
/** |
|
447 |
* Replace the contents in the cache, if contents already exist |
|
448 |
* |
|
449 |
* @since 2.0.0 |
|
450 |
* @see WP_Object_Cache::set() |
|
451 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
452 |
* @param int|string $key What to call the contents in the cache |
136 | 453 |
* @param mixed $data The contents to store in the cache |
454 |
* @param string $group Where to group the cache contents |
|
455 |
* @param int $expire When to expire the cache contents |
|
456 |
* @return bool False if not exists, true if contents were replaced |
|
457 |
*/ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
458 |
function replace($key, $data, $group = 'default', $expire = '') { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
459 |
if ( empty( $group ) ) |
136 | 460 |
$group = 'default'; |
461 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
462 |
if ( ! $this->_exists( $key, $group ) ) |
136 | 463 |
return false; |
464 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
465 |
return $this->set($key, $data, $group, $expire); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
466 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
467 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
468 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
469 |
* Reset keys |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
470 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
471 |
* @since 3.0.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
472 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
473 |
function reset() { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
474 |
// Clear out non-global caches since the blog ID has changed. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
475 |
foreach ( array_keys($this->cache) as $group ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
476 |
if ( !in_array($group, $this->global_groups) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
477 |
unset($this->cache[$group]); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
478 |
} |
136 | 479 |
} |
480 |
||
481 |
/** |
|
482 |
* Sets the data contents into the cache |
|
483 |
* |
|
484 |
* The cache contents is grouped by the $group parameter followed by the |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
485 |
* $key. This allows for duplicate ids in unique groups. Therefore, naming of |
136 | 486 |
* the group should be used with care and should follow normal function |
487 |
* naming guidelines outside of core WordPress usage. |
|
488 |
* |
|
489 |
* The $expire parameter is not used, because the cache will automatically |
|
490 |
* expire for each time a page is accessed and PHP finishes. The method is |
|
491 |
* more for cache plugins which use files. |
|
492 |
* |
|
493 |
* @since 2.0.0 |
|
494 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
495 |
* @param int|string $key What to call the contents in the cache |
136 | 496 |
* @param mixed $data The contents to store in the cache |
497 |
* @param string $group Where to group the cache contents |
|
498 |
* @param int $expire Not Used |
|
499 |
* @return bool Always returns true |
|
500 |
*/ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
501 |
function set($key, $data, $group = 'default', $expire = '') { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
502 |
if ( empty( $group ) ) |
136 | 503 |
$group = 'default'; |
504 |
||
505 |
if ( is_object($data) ) |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
506 |
$data = clone $data; |
136 | 507 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
508 |
$this->cache[$group][$key] = $data; |
136 | 509 |
return true; |
510 |
} |
|
511 |
||
512 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
513 |
* Echoes the stats of the caching. |
136 | 514 |
* |
515 |
* Gives the cache hits, and cache misses. Also prints every cached group, |
|
516 |
* key and the data. |
|
517 |
* |
|
518 |
* @since 2.0.0 |
|
519 |
*/ |
|
520 |
function stats() { |
|
521 |
echo "<p>"; |
|
522 |
echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />"; |
|
523 |
echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />"; |
|
524 |
echo "</p>"; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
525 |
echo '<ul>'; |
136 | 526 |
foreach ($this->cache as $group => $cache) { |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
527 |
echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / 1024, 2 ) . 'k )</li>'; |
136 | 528 |
} |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
529 |
echo '</ul>'; |
136 | 530 |
} |
531 |
||
532 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
533 |
* Utility function to determine whether a key exists in the cache. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
534 |
* @access private |
136 | 535 |
*/ |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
536 |
protected function _exists($key, $group) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
537 |
return isset( $this->cache[$group] ) && is_array( $this->cache[$group] ) && array_key_exists( $key, $this->cache[$group] ); |
136 | 538 |
} |
539 |
||
540 |
/** |
|
541 |
* Sets up object properties; PHP 5 style constructor |
|
542 |
* |
|
543 |
* @since 2.0.8 |
|
544 |
* @return null|WP_Object_Cache If cache is disabled, returns null. |
|
545 |
*/ |
|
546 |
function __construct() { |
|
547 |
/** |
|
548 |
* @todo This should be moved to the PHP4 style constructor, PHP5 |
|
549 |
* already calls __destruct() |
|
550 |
*/ |
|
551 |
register_shutdown_function(array(&$this, "__destruct")); |
|
552 |
} |
|
553 |
||
554 |
/** |
|
555 |
* Will save the object cache before object is completely destroyed. |
|
556 |
* |
|
557 |
* Called upon object destruction, which should be when PHP ends. |
|
558 |
* |
|
559 |
* @since 2.0.8 |
|
560 |
* |
|
561 |
* @return bool True value. Won't be used by PHP |
|
562 |
*/ |
|
563 |
function __destruct() { |
|
564 |
return true; |
|
565 |
} |
|
566 |
} |