set_transient()将某个值保留在数据库中一段时间

目录

描述

Set/update the value of a transient.

You do not need to serialize values. If the value needs to be serialized, then it will be serialized before it is set.

set_transient() 描述

用法

<?php set_transient$transient$value$expiration ); ?>

set_transient() 用法

参数

$transient

(string) (必填) Transient name. Expected to not be SQL-escaped. Should be 45 characters or less in length as WordPress will prefix your name with "_transient_" or "_transient_timeout_" in the options table (depending on whether it expires or not). Longer key names will silently fail. See Trac #15058.

默认值: None

$value

(mixed) (必填) Transient value. Expected to not be SQL-escaped.

默认值: None

$expiration

(int) (可选) Time until expiration in seconds from now, or 0 for never expires. Ex: For one day, the expiration value would be: (60 * 60 * 24).

默认值: 0.

set_transient() 参数

返回值

(boolean) 

False if value was not set and true if value was set.

set_transient() 返回值

示例

Saving the $special_query_results object for 12 hours

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php
set_transient( 'special_query_results', $special_query_results, 12 * HOUR_IN_SECONDS );
?>

set_transient() 示例

注意

If a transient exists, this function will update the transient's expiration time.

NB: transients that never expire are autoloaded, whereas transients with an expiration time are not autoloaded. Consider this when adding transients that may not be needed on every page, and thus do not need to be autoloaded, impacting page performance.

WordPress provides some constants for specifying time in seconds. Instead of multiplying out integers, see Transients_API#Using_Time_Constants.

Transient key names are limited to 64 characters due to the database settings in the wp_options table ( option_name: varchar(64) ).

set_transient() 注意

历史

添加于 版本: 2.8

set_transient() 历史

源文件

set_transient() 函数的代码位于 wp-includes/option.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Set/update the value of a transient.
 *
 * You do not need to serialize values. If the value needs to be serialized, then
 * it will be serialized before it is set.
 *
 * @since 2.8.0
 *
 * @param string $transient  Transient name. Expected to not be SQL-escaped. Must be
 *                           45 characters or fewer in length.
 * @param mixed  $value      Transient value. Must be serializable if non-scalar.
 *                           Expected to not be SQL-escaped.
 * @param int    $expiration Optional. Time until expiration in seconds. Default 0.
 * @return bool False if value was not set and true if value was set.
 */
function set_transient( $transient, $value, $expiration = 0 ) {

	$expiration = (int) $expiration;

	/**
	 * Filter a specific transient before its value is set.
	 *
	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
	 *
	 * @since 3.0.0
	 * @since 4.2.0 Added `$expiration` parameter.
	 *
	 * @param mixed $value      New value of transient.
	 * @param int   $expiration Time until expiration in seconds.
	 */
	$value = apply_filters( 'pre_set_transient_' . $transient, $value, $expiration );

	if ( wp_using_ext_object_cache() ) {
		$result = wp_cache_set( $transient, $value, 'transient', $expiration );
	} else {
		$transient_timeout = '_transient_timeout_' . $transient;
		$transient = '_transient_' . $transient;
		if ( false === get_option( $transient ) ) {
			$autoload = 'yes';
			if ( $expiration ) {
				$autoload = 'no';
				add_option( $transient_timeout, time() + $expiration, '', 'no' );
			}
			$result = add_option( $transient, $value, '', $autoload );
		} else {
			// If expiration is requested, but the transient has no timeout option,
			// delete, then re-create transient rather than update.
			$update = true;
			if ( $expiration ) {
				if ( false === get_option( $transient_timeout ) ) {
					delete_option( $transient );
					add_option( $transient_timeout, time() + $expiration, '', 'no' );
					$result = add_option( $transient, $value, '', 'no' );
					$update = false;
				} else {
					update_option( $transient_timeout, time() + $expiration );
				}
			}
			if ( $update ) {
				$result = update_option( $transient, $value );
			}
		}
	}

	if ( $result ) {

		/**
		 * Fires after the value for a specific transient has been set.
		 *
		 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
		 *
		 * @since 3.0.0
		 *
		 * @param mixed $value      Transient value.
		 * @param int   $expiration Time until expiration in seconds. Default 0.
		 */
		do_action( 'set_transient_' . $transient, $value, $expiration );

		/**
		 * Fires after the value for a transient has been set.
		 *
		 * @since 3.0.0
		 *
		 * @param string $transient  The name of the transient.
		 * @param mixed  $value      Transient value.
		 * @param int    $expiration Time until expiration in seconds. Default 0.
		 */
		do_action( 'setted_transient', $transient, $value, $expiration );
	}
	return $result;
}

set_transient() 源文件

相关