Make WordPress Core

Ticket #22661: 22661-cache_via_filters.diff

File 22661-cache_via_filters.diff, 5.0 KB (added by jipmoors, 10 years ago)

implemented filters which enable cache fall through

  • src/wp-includes/cache.php

     
    2222 * @return bool False if cache key and group already exist, true on success
    2323 */
    2424function wp_cache_add( $key, $data, $group = '', $expire = 0 ) {
     25        // Try to add cache via hooked up method(s)
     26        $result = apply_filters('wp_cache_add', null, $key, $data, $group, (int) $expire);
     27        if (null !== $result) {
     28                return $result;
     29        }
     30
     31        // Fall back on internal cache if nothing has been done yet
    2532        global $wp_object_cache;
    2633
    2734        return $wp_object_cache->add( $key, $data, $group, (int) $expire );
     
    4047 * @return bool Always returns True
    4148 */
    4249function wp_cache_close() {
     50        $result = apply_filters('wp_cache_close', null);
     51        if ( null !== $result ) {
     52                return $result;
     53        }
     54
    4355        return true;
    4456}
    4557
     
    5668 * @return false|int False on failure, the item's new value on success.
    5769 */
    5870function wp_cache_decr( $key, $offset = 1, $group = '' ) {
     71        $result = apply_filters('wp_cache_decr', null, $key, $offset, $group);
     72        if (null !== $result) {
     73                return $result;
     74        }
     75
    5976        global $wp_object_cache;
    6077
    6178        return $wp_object_cache->decr( $key, $offset, $group );
     
    7390 * @return bool True on successful removal, false on failure
    7491 */
    7592function wp_cache_delete($key, $group = '') {
     93        $result = apply_filters('wp_cache_delete', null, $key, $group);
     94        if (null !== $result) {
     95                return $result;
     96        }
     97
    7698        global $wp_object_cache;
    7799
    78100        return $wp_object_cache->delete($key, $group);
     
    88110 * @return bool False on failure, true on success
    89111 */
    90112function wp_cache_flush() {
     113        $result = apply_filters('wp_cache_flush', null);
     114        if ( null !== $result ) {
     115                return $result;
     116        }
     117
    91118        global $wp_object_cache;
    92119
    93120        return $wp_object_cache->flush();
     
    108135 *              contents on success
    109136 */
    110137function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
     138        $result = apply_filters('wp_cache_get', null, $key, $group, $force);
     139        if (null !== $result) {
     140                $found = true;
     141                return $result;
     142        }
     143
    111144        global $wp_object_cache;
    112145
    113146        return $wp_object_cache->get( $key, $group, $force, $found );
     
    126159 * @return false|int False on failure, the item's new value on success.
    127160 */
    128161function wp_cache_incr( $key, $offset = 1, $group = '' ) {
     162        $result = apply_filters('wp_cache_inc', null, $key, $offset, $group);
     163        if (null !== $result) {
     164                return $result;
     165        }
     166
    129167        global $wp_object_cache;
    130168
    131169        return $wp_object_cache->incr( $key, $offset, $group );
     
    138176 * @global WP_Object_Cache $wp_object_cache WordPress Object Cache
    139177 */
    140178function wp_cache_init() {
     179        do_action('wp_cache_init');
     180
     181        // Create cache object in case of failing cache implementation or filters not being applied
    141182        $GLOBALS['wp_object_cache'] = new WP_Object_Cache();
     183       
     184        /*
     185         * Because of the filter implementation this would not be called on 'first-run',
     186         * and it would make sense to switch to the current blog on initialize
     187         */
     188        global $blog_id;
     189        wp_cache_switch_to_blog( $blog_id );
     190
    142191}
    143192
    144193/**
     
    155204 * @return bool False if not exists, true if contents were replaced
    156205 */
    157206function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
     207        $result = apply_filters('wp_cache_replace', null, $key, $data, $group, (int) $expire);
     208        if ( null !== $result ) {
     209                return $result;
     210        }
     211
    158212        global $wp_object_cache;
    159213
    160214        return $wp_object_cache->replace( $key, $data, $group, (int) $expire );
     
    175229 * @return bool False on failure, true on success
    176230 */
    177231function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
     232        $result = apply_filters('wp_cache_set', null, $key, $data, $group, (int) $expire);
     233        if ( null !== $result ) {
     234                return $result;
     235        }
     236
    178237        global $wp_object_cache;
    179238
    180239        return $wp_object_cache->set( $key, $data, $group, (int) $expire );
     
    188247 * @since 3.5.0
    189248 *
    190249 * @param int $blog_id Blog ID
     250 *
     251 * @return mixed|void
    191252 */
    192253function wp_cache_switch_to_blog( $blog_id ) {
     254        $result = apply_filters( 'wp_cache_switch_to_blog', null, $blog_id );
     255        if ( null !== $result ) {
     256                return $result;
     257        }
     258
    193259        global $wp_object_cache;
    194260
    195261        return $wp_object_cache->switch_to_blog( $blog_id );
     
    201267 * @since 2.6.0
    202268 *
    203269 * @param string|array $groups A group or an array of groups to add
     270 *
     271 * @return mixed|void
    204272 */
    205273function wp_cache_add_global_groups( $groups ) {
     274        $result = apply_filters('wp_cache_add_global_groups', null, $groups);
     275        if ( null !== $result ) {
     276                return $result;
     277        }
    206278        global $wp_object_cache;
    207279
    208280        return $wp_object_cache->add_global_groups( $groups );
     
    214286 * @since 2.6.0
    215287 *
    216288 * @param string|array $groups A group or an array of groups to add
     289 *
     290 * @return mixed|void
    217291 */
    218292function wp_cache_add_non_persistent_groups( $groups ) {
     293        $result = apply_filters( 'wp_cache_add_non_persistent_groups', null, $groups );
     294        if ( null !== $result ) {
     295                return $result;
     296        }
    219297        // Default cache doesn't persist so nothing to do here.
    220298        return;
    221299}