wp/wp-includes/class-wp-object-cache.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    19  * in the wp-content folder which is looked at in wp-settings. If that file
    19  * in the wp-content folder which is looked at in wp-settings. If that file
    20  * exists, then this file will not be included.
    20  * exists, then this file will not be included.
    21  *
    21  *
    22  * @since 2.0.0
    22  * @since 2.0.0
    23  */
    23  */
       
    24 #[AllowDynamicProperties]
    24 class WP_Object_Cache {
    25 class WP_Object_Cache {
    25 
    26 
    26 	/**
    27 	/**
    27 	 * Holds the cached objects.
    28 	 * Holds the cached objects.
    28 	 *
    29 	 *
    49 
    50 
    50 	/**
    51 	/**
    51 	 * List of global cache groups.
    52 	 * List of global cache groups.
    52 	 *
    53 	 *
    53 	 * @since 3.0.0
    54 	 * @since 3.0.0
    54 	 * @var array
    55 	 * @var string[]
    55 	 */
    56 	 */
    56 	protected $global_groups = array();
    57 	protected $global_groups = array();
    57 
    58 
    58 	/**
    59 	/**
    59 	 * The blog prefix to prepend to keys in non-global groups.
    60 	 * The blog prefix to prepend to keys in non-global groups.
   125 	 *
   126 	 *
   126 	 * @param string $name Property to unset.
   127 	 * @param string $name Property to unset.
   127 	 */
   128 	 */
   128 	public function __unset( $name ) {
   129 	public function __unset( $name ) {
   129 		unset( $this->$name );
   130 		unset( $this->$name );
       
   131 	}
       
   132 
       
   133 	/**
       
   134 	 * Serves as a utility function to determine whether a key is valid.
       
   135 	 *
       
   136 	 * @since 6.1.0
       
   137 	 *
       
   138 	 * @param int|string $key Cache key to check for validity.
       
   139 	 * @return bool Whether the key is valid.
       
   140 	 */
       
   141 	protected function is_valid_key( $key ) {
       
   142 		if ( is_int( $key ) ) {
       
   143 			return true;
       
   144 		}
       
   145 
       
   146 		if ( is_string( $key ) && trim( $key ) !== '' ) {
       
   147 			return true;
       
   148 		}
       
   149 
       
   150 		$type = gettype( $key );
       
   151 
       
   152 		if ( ! function_exists( '__' ) ) {
       
   153 			wp_load_translations_early();
       
   154 		}
       
   155 
       
   156 		$message = is_string( $key )
       
   157 			? __( 'Cache key must not be an empty string.' )
       
   158 			/* translators: %s: The type of the given cache key. */
       
   159 			: sprintf( __( 'Cache key must be an integer or a non-empty string, %s given.' ), $type );
       
   160 
       
   161 		_doing_it_wrong(
       
   162 			sprintf( '%s::%s', __CLASS__, debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 )[1]['function'] ),
       
   163 			$message,
       
   164 			'6.1.0'
       
   165 		);
       
   166 
       
   167 		return false;
   130 	}
   168 	}
   131 
   169 
   132 	/**
   170 	/**
   133 	 * Serves as a utility function to determine whether a key exists in the cache.
   171 	 * Serves as a utility function to determine whether a key exists in the cache.
   134 	 *
   172 	 *
   161 	public function add( $key, $data, $group = 'default', $expire = 0 ) {
   199 	public function add( $key, $data, $group = 'default', $expire = 0 ) {
   162 		if ( wp_suspend_cache_addition() ) {
   200 		if ( wp_suspend_cache_addition() ) {
   163 			return false;
   201 			return false;
   164 		}
   202 		}
   165 
   203 
       
   204 		if ( ! $this->is_valid_key( $key ) ) {
       
   205 			return false;
       
   206 		}
       
   207 
   166 		if ( empty( $group ) ) {
   208 		if ( empty( $group ) ) {
   167 			$group = 'default';
   209 			$group = 'default';
   168 		}
   210 		}
   169 
   211 
   170 		$id = $key;
   212 		$id = $key;
   214 	 * @param int        $expire Optional. When to expire the cache contents, in seconds.
   256 	 * @param int        $expire Optional. When to expire the cache contents, in seconds.
   215 	 *                           Default 0 (no expiration).
   257 	 *                           Default 0 (no expiration).
   216 	 * @return bool True if contents were replaced, false if original value does not exist.
   258 	 * @return bool True if contents were replaced, false if original value does not exist.
   217 	 */
   259 	 */
   218 	public function replace( $key, $data, $group = 'default', $expire = 0 ) {
   260 	public function replace( $key, $data, $group = 'default', $expire = 0 ) {
       
   261 		if ( ! $this->is_valid_key( $key ) ) {
       
   262 			return false;
       
   263 		}
       
   264 
   219 		if ( empty( $group ) ) {
   265 		if ( empty( $group ) ) {
   220 			$group = 'default';
   266 			$group = 'default';
   221 		}
   267 		}
   222 
   268 
   223 		$id = $key;
   269 		$id = $key;
   243 	 * The $expire parameter is not used, because the cache will automatically
   289 	 * The $expire parameter is not used, because the cache will automatically
   244 	 * expire for each time a page is accessed and PHP finishes. The method is
   290 	 * expire for each time a page is accessed and PHP finishes. The method is
   245 	 * more for cache plugins which use files.
   291 	 * more for cache plugins which use files.
   246 	 *
   292 	 *
   247 	 * @since 2.0.0
   293 	 * @since 2.0.0
       
   294 	 * @since 6.1.0 Returns false if cache key is invalid.
   248 	 *
   295 	 *
   249 	 * @param int|string $key    What to call the contents in the cache.
   296 	 * @param int|string $key    What to call the contents in the cache.
   250 	 * @param mixed      $data   The contents to store in the cache.
   297 	 * @param mixed      $data   The contents to store in the cache.
   251 	 * @param string     $group  Optional. Where to group the cache contents. Default 'default'.
   298 	 * @param string     $group  Optional. Where to group the cache contents. Default 'default'.
   252 	 * @param int        $expire Optional. Not used.
   299 	 * @param int        $expire Optional. Not used.
   253 	 * @return true Always returns true.
   300 	 * @return bool True if contents were set, false if key is invalid.
   254 	 */
   301 	 */
   255 	public function set( $key, $data, $group = 'default', $expire = 0 ) {
   302 	public function set( $key, $data, $group = 'default', $expire = 0 ) {
       
   303 		if ( ! $this->is_valid_key( $key ) ) {
       
   304 			return false;
       
   305 		}
       
   306 
   256 		if ( empty( $group ) ) {
   307 		if ( empty( $group ) ) {
   257 			$group = 'default';
   308 			$group = 'default';
   258 		}
   309 		}
   259 
   310 
   260 		if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
   311 		if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
   308 	 * @param bool       $found Optional. Whether the key was found in the cache (passed by reference).
   359 	 * @param bool       $found Optional. Whether the key was found in the cache (passed by reference).
   309 	 *                          Disambiguates a return of false, a storable value. Default null.
   360 	 *                          Disambiguates a return of false, a storable value. Default null.
   310 	 * @return mixed|false The cache contents on success, false on failure to retrieve contents.
   361 	 * @return mixed|false The cache contents on success, false on failure to retrieve contents.
   311 	 */
   362 	 */
   312 	public function get( $key, $group = 'default', $force = false, &$found = null ) {
   363 	public function get( $key, $group = 'default', $force = false, &$found = null ) {
       
   364 		if ( ! $this->is_valid_key( $key ) ) {
       
   365 			return false;
       
   366 		}
       
   367 
   313 		if ( empty( $group ) ) {
   368 		if ( empty( $group ) ) {
   314 			$group = 'default';
   369 			$group = 'default';
   315 		}
   370 		}
   316 
   371 
   317 		if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
   372 		if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
   366 	 * @param string     $group      Optional. Where the cache contents are grouped. Default 'default'.
   421 	 * @param string     $group      Optional. Where the cache contents are grouped. Default 'default'.
   367 	 * @param bool       $deprecated Optional. Unused. Default false.
   422 	 * @param bool       $deprecated Optional. Unused. Default false.
   368 	 * @return bool True on success, false if the contents were not deleted.
   423 	 * @return bool True on success, false if the contents were not deleted.
   369 	 */
   424 	 */
   370 	public function delete( $key, $group = 'default', $deprecated = false ) {
   425 	public function delete( $key, $group = 'default', $deprecated = false ) {
       
   426 		if ( ! $this->is_valid_key( $key ) ) {
       
   427 			return false;
       
   428 		}
       
   429 
   371 		if ( empty( $group ) ) {
   430 		if ( empty( $group ) ) {
   372 			$group = 'default';
   431 			$group = 'default';
   373 		}
   432 		}
   374 
   433 
   375 		if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
   434 		if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
   414 	 *                           Default 1.
   473 	 *                           Default 1.
   415 	 * @param string     $group  Optional. The group the key is in. Default 'default'.
   474 	 * @param string     $group  Optional. The group the key is in. Default 'default'.
   416 	 * @return int|false The item's new value on success, false on failure.
   475 	 * @return int|false The item's new value on success, false on failure.
   417 	 */
   476 	 */
   418 	public function incr( $key, $offset = 1, $group = 'default' ) {
   477 	public function incr( $key, $offset = 1, $group = 'default' ) {
       
   478 		if ( ! $this->is_valid_key( $key ) ) {
       
   479 			return false;
       
   480 		}
       
   481 
   419 		if ( empty( $group ) ) {
   482 		if ( empty( $group ) ) {
   420 			$group = 'default';
   483 			$group = 'default';
   421 		}
   484 		}
   422 
   485 
   423 		if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
   486 		if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
   453 	 *                           Default 1.
   516 	 *                           Default 1.
   454 	 * @param string     $group  Optional. The group the key is in. Default 'default'.
   517 	 * @param string     $group  Optional. The group the key is in. Default 'default'.
   455 	 * @return int|false The item's new value on success, false on failure.
   518 	 * @return int|false The item's new value on success, false on failure.
   456 	 */
   519 	 */
   457 	public function decr( $key, $offset = 1, $group = 'default' ) {
   520 	public function decr( $key, $offset = 1, $group = 'default' ) {
       
   521 		if ( ! $this->is_valid_key( $key ) ) {
       
   522 			return false;
       
   523 		}
       
   524 
   458 		if ( empty( $group ) ) {
   525 		if ( empty( $group ) ) {
   459 			$group = 'default';
   526 			$group = 'default';
   460 		}
   527 		}
   461 
   528 
   462 		if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
   529 		if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
   489 	 *
   556 	 *
   490 	 * @return true Always returns true.
   557 	 * @return true Always returns true.
   491 	 */
   558 	 */
   492 	public function flush() {
   559 	public function flush() {
   493 		$this->cache = array();
   560 		$this->cache = array();
       
   561 
       
   562 		return true;
       
   563 	}
       
   564 
       
   565 	/**
       
   566 	 * Removes all cache items in a group.
       
   567 	 *
       
   568 	 * @since 6.1.0
       
   569 	 *
       
   570 	 * @param string $group Name of group to remove from cache.
       
   571 	 * @return true Always returns true.
       
   572 	 */
       
   573 	public function flush_group( $group ) {
       
   574 		unset( $this->cache[ $group ] );
   494 
   575 
   495 		return true;
   576 		return true;
   496 	}
   577 	}
   497 
   578 
   498 	/**
   579 	/**