| | 219 | * Attempt to retrieve a value from the cache. If it is not already set, the |
| | 220 | * callback function is attempted. If that does not return a result, the |
| | 221 | * $default is used. The $result is then set in the cache, and returned. |
| | 222 | * |
| | 223 | * This is a helper, and does not have a partner method in WP_Object_Cache |
| | 224 | * |
| | 225 | * @since 3.4 |
| | 226 | * @param int|string $key What to call the contents in the cache |
| | 227 | * @param mixed $default The default contents to cache |
| | 228 | * @param string Optional $callback Function name to call if cache not set |
| | 229 | * @param string Optional $group Where to group the cache contents |
| | 230 | * @param int Optional $expire When to expire the cache contents |
| | 231 | * @return mixed The cache contents on cache hit, the callback or default on cache miss |
| | 232 | */ |
| | 233 | function wp_cache_attempt( $key, $default, $callback = '', $group = '', $expire = 0 ) { |
| | 234 | $result = wp_cache_get( $key, $group ); |
| | 235 | if ( ! $result ) { |
| | 236 | if ( $callback ) { |
| | 237 | $result = call_user_func( $callback ); |
| | 238 | } |
| | 239 | if ( ! $result ) { |
| | 240 | $result = $default; |
| | 241 | } |
| | 242 | wp_cache_set( $key, $result, $group, $expire ); |
| | 243 | } |
| | 244 | return $result; |
| | 245 | } |
| | 246 | |
| | 247 | /** |