Add expiration for transients

git-svn-id: http://svn.automattic.com/wordpress/trunk@10603 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2009-02-20 02:23:11 +00:00
parent 4694b6c752
commit 0a6a9d0024
2 changed files with 31 additions and 23 deletions

View File

@ -665,6 +665,17 @@ function get_transient($transient) {
$value = wp_cache_get($transient, 'transient'); $value = wp_cache_get($transient, 'transient');
} else { } else {
$transient_option = '_transient_' . $wpdb->escape($transient); $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); $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 string $transient Transient name. Expected to not be SQL-escaped
* @param mixed $value Transient value. * @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. * @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; global $_wp_using_ext_object_cache, $wpdb;
if ( $_wp_using_ext_object_cache ) { if ( $_wp_using_ext_object_cache ) {
return wp_cache_set($transient, $value, 'transient'); return wp_cache_set($transient, $value, 'transient', $expiration);
} else { } else {
$transient_timeout = '_transient_timeout_' . $transient;
$transient = '_transient_' . $transient; $transient = '_transient_' . $transient;
$safe_transient = $wpdb->escape($transient); $safe_transient = $wpdb->escape($transient);
if ( false === get_option( $safe_transient ) ) if ( false === get_option( $safe_transient ) ) {
return add_option($transient, $value, '', 'no'); $autoload = 'yes';
else 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); return update_option($transient, $value);
}
} }
} }

View File

@ -712,10 +712,8 @@ class RSSCache {
function set ($url, $rss) { function set ($url, $rss) {
global $wpdb; global $wpdb;
$cache_option = 'rss_' . $this->file_name( $url ); $cache_option = 'rss_' . $this->file_name( $url );
$cache_timestamp = 'rss_' . $this->file_name( $url ) . '_ts';
set_transient($cache_option, $rss); set_transient($cache_option, $rss, $this->MAX_AGE);
set_transient($cache_timestamp, time() );
return $cache_option; return $cache_option;
} }
@ -749,23 +747,12 @@ class RSSCache {
\*=======================================================================*/ \*=======================================================================*/
function check_cache ( $url ) { function check_cache ( $url ) {
$this->ERROR = ""; $this->ERROR = "";
$cache_option = $this->file_name( $url ); $cache_option = 'rss_' . $this->file_name( $url );
$cache_timestamp = 'rss_' . $this->file_name( $url ) . '_ts';
if ( $mtime = get_transient($cache_timestamp) ) { if ( get_transient($cache_option) ) {
// find how long ago the file was added to the cache // object exists and is current
// and whether that is longer then MAX_AGE
$age = time() - $mtime;
if ( $this->MAX_AGE > $age ) {
// object exists and is current
return 'HIT'; return 'HIT';
} } else {
else {
// object exists but is old
return 'STALE';
}
}
else {
// object does not exist // object does not exist
return 'MISS'; return 'MISS';
} }