From 0a6a9d00240324b4192913ff075b92a5f8b28b18 Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 20 Feb 2009 02:23:11 +0000 Subject: [PATCH] Add expiration for transients git-svn-id: http://svn.automattic.com/wordpress/trunk@10603 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/functions.php | 31 ++++++++++++++++++++++++++----- wp-includes/rss.php | 23 +++++------------------ 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 62d85810f..31f2e09de 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -665,6 +665,17 @@ function get_transient($transient) { $value = wp_cache_get($transient, 'transient'); } else { $transient_option = '_transient_' . $wpdb->escape($transient); + // 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_' . $wpdb->escape($transient); + if ( get_option($transient_timeout) > time() ) { + delete_option($transient_option); + delete_option($transient_timeout); + return false; + } + } + $value = get_option($transient_option); } @@ -683,20 +694,30 @@ function get_transient($transient) { * * @param string $transient Transient name. Expected to not be SQL-escaped * @param mixed $value Transient value. + * @param int $expiration 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) { +function set_transient($transient, $value, $expiration = 0) { global $_wp_using_ext_object_cache, $wpdb; if ( $_wp_using_ext_object_cache ) { - return wp_cache_set($transient, $value, 'transient'); + return wp_cache_set($transient, $value, 'transient', $expiration); } else { + $transient_timeout = '_transient_timeout_' . $transient; $transient = '_transient_' . $transient; $safe_transient = $wpdb->escape($transient); - if ( false === get_option( $safe_transient ) ) - return add_option($transient, $value, '', 'no'); - else + if ( false === get_option( $safe_transient ) ) { + $autoload = 'yes'; + if ( 0 != $expiration ) { + $autoload = 'no'; + add_option($transient_timeout, time() + $expiration, '', 'no'); + } + return add_option($transient, $value, '', $autoload); + } else { + if ( 0 != $expiration ) + update_option($transient_timeout, time() + $expiration); return update_option($transient, $value); + } } } diff --git a/wp-includes/rss.php b/wp-includes/rss.php index a33c3a7dc..3d9aa3214 100644 --- a/wp-includes/rss.php +++ b/wp-includes/rss.php @@ -712,10 +712,8 @@ class RSSCache { function set ($url, $rss) { global $wpdb; $cache_option = 'rss_' . $this->file_name( $url ); - $cache_timestamp = 'rss_' . $this->file_name( $url ) . '_ts'; - set_transient($cache_option, $rss); - set_transient($cache_timestamp, time() ); + set_transient($cache_option, $rss, $this->MAX_AGE); return $cache_option; } @@ -749,23 +747,12 @@ class RSSCache { \*=======================================================================*/ function check_cache ( $url ) { $this->ERROR = ""; - $cache_option = $this->file_name( $url ); - $cache_timestamp = 'rss_' . $this->file_name( $url ) . '_ts'; + $cache_option = 'rss_' . $this->file_name( $url ); - if ( $mtime = get_transient($cache_timestamp) ) { - // find how long ago the file was added to the cache - // and whether that is longer then MAX_AGE - $age = time() - $mtime; - if ( $this->MAX_AGE > $age ) { - // object exists and is current + if ( get_transient($cache_option) ) { + // object exists and is current return 'HIT'; - } - else { - // object exists but is old - return 'STALE'; - } - } - else { + } else { // object does not exist return 'MISS'; }