Make WordPress Core


Ignore:
Timestamp:
02/20/2009 02:23:11 AM (16 years ago)
Author:
ryan
Message:

Add expiration for transients

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions.php

    r10593 r10603  
    666666    } else {
    667667        $transient_option = '_transient_' . $wpdb->escape($transient);
     668        // If option is not in alloptions, it is not autoloaded and thus has a timeout
     669        $alloptions = wp_load_alloptions();
     670        if ( !isset( $alloptions[$transient_option] ) ) {
     671            $transient_timeout = '_transient_timeout_' . $wpdb->escape($transient);
     672            if ( get_option($transient_timeout) > time() ) {
     673                delete_option($transient_option);
     674                delete_option($transient_timeout);
     675                return false;
     676            }
     677        }
     678
    668679        $value = get_option($transient_option);
    669680    }
     
    684695 * @param string $transient Transient name. Expected to not be SQL-escaped
    685696 * @param mixed $value Transient value.
     697 * @param int $expiration Time until expiration in seconds, default 0
    686698 * @return bool False if value was not set and true if value was set.
    687699 */
    688 function set_transient($transient, $value) {
     700function set_transient($transient, $value, $expiration = 0) {
    689701    global $_wp_using_ext_object_cache, $wpdb;
    690702
    691703    if ( $_wp_using_ext_object_cache ) {
    692         return wp_cache_set($transient, $value, 'transient');
     704        return wp_cache_set($transient, $value, 'transient', $expiration);
    693705    } else {
     706        $transient_timeout = '_transient_timeout_' . $transient;
    694707        $transient = '_transient_' . $transient;
    695708        $safe_transient = $wpdb->escape($transient);
    696         if ( false === get_option( $safe_transient ) )
    697             return add_option($transient, $value, '', 'no');
    698         else
     709        if ( false === get_option( $safe_transient ) ) {
     710            $autoload = 'yes';
     711            if ( 0 != $expiration ) {
     712                $autoload = 'no';
     713                add_option($transient_timeout, time() + $expiration, '', 'no');
     714            }
     715            return add_option($transient, $value, '', $autoload);
     716        } else {
     717            if ( 0 != $expiration )
     718                update_option($transient_timeout, time() + $expiration);
    699719            return update_option($transient, $value);
     720        }
    700721    }
    701722}
Note: See TracChangeset for help on using the changeset viewer.