Make WordPress Core

Ticket #22661: 22661-cache_via_filters-ext_object_filter.diff

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

cache via filters + filter to enable external object cache

  • 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        // Calling init on attached cache objects, if one or more loads; we have external cache
     182        $result = apply_filters( 'wp_object_cache', false );
     183        if ( true === $result ) {
     184                wp_using_ext_object_cache( true );
     185        }
     186
     187        // Create cache object in case of failing cache implementation or filters not being applied
    141188        $GLOBALS['wp_object_cache'] = new WP_Object_Cache();
     189
     190        /*
     191         * Because of the filter implementation this would not be called on 'first-run',
     192         * and it would make sense to switch to the current blog on initialize
     193         */
     194        global $blog_id;
     195        wp_cache_switch_to_blog( $blog_id );
     196
    142197}
    143198
    144199/**
     
    155210 * @return bool False if not exists, true if contents were replaced
    156211 */
    157212function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
     213        $result = apply_filters('wp_cache_replace', null, $key, $data, $group, (int) $expire);
     214        if ( null !== $result ) {
     215                return $result;
     216        }
     217
    158218        global $wp_object_cache;
    159219
    160220        return $wp_object_cache->replace( $key, $data, $group, (int) $expire );
     
    175235 * @return bool False on failure, true on success
    176236 */
    177237function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
     238        $result = apply_filters('wp_cache_set', null, $key, $data, $group, (int) $expire);
     239        if ( null !== $result ) {
     240                return $result;
     241        }
     242
    178243        global $wp_object_cache;
    179244
    180245        return $wp_object_cache->set( $key, $data, $group, (int) $expire );
     
    188253 * @since 3.5.0
    189254 *
    190255 * @param int $blog_id Blog ID
     256 *
     257 * @return mixed|void
    191258 */
    192259function wp_cache_switch_to_blog( $blog_id ) {
     260        $result = apply_filters( 'wp_cache_switch_to_blog', null, $blog_id );
     261        if ( null !== $result ) {
     262                return $result;
     263        }
     264
    193265        global $wp_object_cache;
    194266
    195267        return $wp_object_cache->switch_to_blog( $blog_id );
     
    201273 * @since 2.6.0
    202274 *
    203275 * @param string|array $groups A group or an array of groups to add
     276 *
     277 * @return mixed|void
    204278 */
    205279function wp_cache_add_global_groups( $groups ) {
     280        $result = apply_filters('wp_cache_add_global_groups', null, $groups);
     281        if ( null !== $result ) {
     282                return $result;
     283        }
    206284        global $wp_object_cache;
    207285
    208286        return $wp_object_cache->add_global_groups( $groups );
     
    214292 * @since 2.6.0
    215293 *
    216294 * @param string|array $groups A group or an array of groups to add
     295 *
     296 * @return mixed|void
    217297 */
    218298function wp_cache_add_non_persistent_groups( $groups ) {
     299        $result = apply_filters( 'wp_cache_add_non_persistent_groups', null, $groups );
     300        if ( null !== $result ) {
     301                return $result;
     302        }
    219303        // Default cache doesn't persist so nothing to do here.
    220304        return;
    221305}