Make WordPress Core

Changeset 18580


Ignore:
Timestamp:
08/22/2011 05:39:44 PM (15 years ago)
Author:
ryan
Message:

Introduce wp_cache_incr() and wp_cache_decr(). fixes #18494

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/cache.php

    r17613 r18580  
    4545
    4646/**
     47 * Decrement numeric cache item's value
     48 *
     49 * @since 3.3.0
     50 * @uses $wp_object_cache Object Cache Class
     51 * @see WP_Object_Cache::decr()
     52 *
     53 * @param int|string $key The cache ID to increment
     54 * @param int $offset The amount by which to decrement the item's value.  Default is 1.
     55 * @param string $flag The group the key is in.
     56 * @return false|int False on failure, the item's new value on success.
     57 */
     58function wp_cache_decr( $key, $offset = 1, $flag = '' ) {
     59        global $wp_object_cache;
     60
     61        return $wp_object_cache->decr( $key, $offset, $flag );
     62}
     63
     64/**
    4765 * Removes the cache contents matching ID and flag.
    4866 *
    … …  
    92110
    93111        return $wp_object_cache->get($id, $flag);
     112}
     113
     114/**
     115 * Increment numeric cache item's value
     116 *
     117 * @since 3.3.0
     118 * @uses $wp_object_cache Object Cache Class
     119 * @see WP_Object_Cache::incr()
     120 *
     121 * @param int|string $key The cache ID to increment
     122 * @param int $offset The amount by which to increment the item's value.  Default is 1.
     123 * @param string $flag The group the key is in.
     124 * @return false|int False on failure, the item's new value on success.
     125 */
     126function wp_cache_incr( $key, $offset = 1, $flag = '' ) {
     127        global $wp_object_cache;
     128
     129        return $wp_object_cache->incr( $key, $offset, $flag );
    94130}
    95131
    … …  
    279315                $this->global_groups = array_merge($this->global_groups, $groups);
    280316                $this->global_groups = array_unique($this->global_groups);
     317        }
     318
     319        /**
     320         * Decrement numeric cache item's value
     321         *
     322         * @since 3.3.0
     323         *
     324         * @param int|string $id The cache ID to increment
     325         * @param int $offset The amount by which to decrement the item's value.  Default is 1.
     326         * @param string $flag The group the key is in.
     327         * @return false|int False on failure, the item's new value on success.
     328         */
     329        function decr( $id, $offset = 1, $group = 'default' ) {
     330                if ( ! isset( $this->cache[ $group ][ $id ] ) )
     331                        return false;
     332
     333                if ( ! is_numeric( $this->cache[ $group ][ $id ] ) )
     334                        $this->cache[ $group ][ $id ] = 0;
     335
     336                $offset = (int) $offset;
     337
     338                $this->cache[ $group ][ $id ] -= $offset;
     339
     340                if ( $this->cache[ $group ][ $id ] < 0 )
     341                        $this->cache[ $group ][ $id ] = 0;
     342
     343                return $this->cache[ $group ][ $id ];
    281344        }
    282345
    … …  
    362425                $this->cache_misses += 1;
    363426                return false;
     427        }
     428
     429        /**
     430         * Increment numeric cache item's value
     431         *
     432         * @since 3.3.0
     433         *
     434         * @param int|string $id The cache ID to increment
     435         * @param int $offset The amount by which to increment the item's value.  Default is 1.
     436         * @param string $flag The group the key is in.
     437         * @return false|int False on failure, the item's new value on success.
     438         */
     439        function incr( $id, $offset = 1, $group = 'default' ) {
     440                if ( ! isset( $this->cache[ $group ][ $id ] ) )
     441                        return false;
     442
     443                if ( ! is_numeric( $this->cache[ $group ][ $id ] ) )
     444                        $this->cache[ $group ][ $id ] = 0;
     445
     446                $offset = (int) $offset;
     447
     448                $this->cache[ $group ][ $id ] += $offset;
     449
     450                if ( $this->cache[ $group ][ $id ] < 0 )
     451                        $this->cache[ $group ][ $id ] = 0;
     452
     453                return $this->cache[ $group ][ $id ];
    364454        }
    365455
Note: See TracChangeset for help on using the changeset viewer.