set_transient( string $transient, mixed $value, int $expiration )
更新瞬时值/a。
Sets/updates the value of a transient.
说明(Description)
不需要序列化值。如果需要序列化该值,则在设置之前将其序列化。
参数(Parameters)
参数 | 类型 | 说明 |
---|---|---|
$transient | (string) | 临时名称。不应为SQL转义。长度不得超过172个字符。 |
$value | (mixed) | 瞬态值。如果不是标量,则必须是可序列化的。不应为SQL转义。 |
$expiration | (int) | 过期时间(秒)。默认值为0(无过期)。 |
源码(Source)
/**
* 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;
}
更新版本 | 源码位置 | 使用 | 被使用 |
---|---|---|---|
2.8.0 | wp-includes/option.php | 12 | 6 |