返回介绍

get_transient()

发布于 2017-09-11 00:40:43 字数 6045 浏览 1212 评论 0 收藏 0

get_transient( string $transient )

Get the value of a transient.


description

If the transient does not exist, does not have a value, or has expired, then the return value will be false.


参数

$transient

(string) (Required) Transient name. Expected to not be SQL-escaped.


返回值

(mixed) Value of transient.


源代码

File: wp-includes/option.php

function get_transient( $transient ) {

	/**
	 * Filters the value of an existing transient.
	 *
	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
	 *
	 * Passing a truthy value to the filter will effectively short-circuit retrieval
	 * of the transient, returning the passed value instead.
	 *
	 * @since 2.8.0
	 * @since 4.4.0 The `$transient` parameter was added
	 *
	 * @param mixed  $pre_transient The default value to return if the transient does not exist.
	 *                              Any value other than false will short-circuit the retrieval
	 *                              of the transient, and return the returned value.
	 * @param string $transient     Transient name.
	 */
	$pre = apply_filters( "pre_transient_{$transient}", false, $transient );
	if ( false !== $pre )
		return $pre;

	if ( wp_using_ext_object_cache() ) {
		$value = wp_cache_get( $transient, 'transient' );
	} else {
		$transient_option = '_transient_' . $transient;
		if ( ! wp_installing() ) {
			// If option is not in alloptions, it is not autoloaded and thus has a timeout
			$alloptions = wp_load_alloptions();
			if ( !isset( $alloptions[$transient_option] ) ) {
				$transient_timeout = '_transient_timeout_' . $transient;
				$timeout = get_option( $transient_timeout );
				if ( false !== $timeout && $timeout < time() ) {
					delete_option( $transient_option  );
					delete_option( $transient_timeout );
					$value = false;
				}
			}
		}

		if ( ! isset( $value ) )
			$value = get_option( $transient_option );
	}

	/**
	 * Filters an existing transient's value.
	 *
	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
	 *
	 * @since 2.8.0
	 * @since 4.4.0 The `$transient` parameter was added
	 *
	 * @param mixed  $value     Value of transient.
	 * @param string $transient Transient name.
	 */
	return apply_filters( "transient_{$transient}", $value, $transient );
}

更新日志

Versiondescription
2.8.0Introduced.

相关函数

Uses

  • wp-includes/load.php: wp_installing()
  • wp-includes/cache.php: wp_cache_get()
  • wp-includes/load.php: wp_using_ext_object_cache()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/option.php: pre_transient_{$transient}
  • wp-includes/option.php: transient_{$transient}
  • wp-includes/option.php: wp_load_alloptions()
  • wp-includes/option.php: delete_option()
  • wp-includes/option.php: get_option()
  • Show 4 more uses Hide more uses

Used By

  • wp-includes/class-wp-oembed-controller.php: WP_oEmbed_Controller::get_proxy_item()
  • wp-admin/includes/theme-install.php: install_themes_feature_list()
  • wp-admin/includes/dashboard.php: wp_dashboard_cached_rss_widget()
  • wp-admin/includes/deprecated.php: wp_dashboard_plugins_output()
  • wp-admin/includes/template.php: get_settings_errors()
  • wp-includes/cron.php: spawn_cron()
  • wp-includes/pluggable.php: wp_rand()
  • wp-includes/class-wp-feed-cache-transient.php: WP_Feed_Cache_Transient::load()
  • wp-includes/class-wp-feed-cache-transient.php: WP_Feed_Cache_Transient::mtime()
  • wp-includes/media.php: wp_maybe_generate_attachment_metadata()
  • wp-includes/ms-functions.php: get_dirsize()
  • wp-includes/author-template.php: is_multi_author()
  • wp-includes/rss.php: RSSCache::get()
  • wp-includes/rss.php: RSSCache::check_cache()
  • Show 9 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Example of using get_transient, set_transient and WP_Query

    
    // Get any existing copy of our transient data
    if ( false === ( $special_query_results = get_transient( 'special_query_results' ) ) ) {
    	// It wasn't there, so regenerate the data and save the transient
    	$special_query_results = new WP_Query( 'cat=5&order=random&tag=tech&post_meta_key=thumbnail' );
    	set_transient( 'special_query_results', $special_query_results );
    }
    
    // Use the data like you would have normally...
    
  2. Add WP_DEBUG in the conditional statement if you always want live data during development stage

    
    <?php
    
    if ( WP_DEBUG or false === ( $special_query_results = get_transient( 'special_query_results' ) ) ) ) {
    	// It wasn't there, so regenerate the data and save the transient
    	$special_query_results = new WP_Query( 'cat=5&order=random&tag=tech&post_meta_key=thumbnail' );
    	set_transient( 'special_query_results', $special_query_results );
    }
    
    ?>
    
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文