WordPress.org

Make WordPress Core

Ticket #9048: 9048.diff

File 9048.diff, 5.5 KB (added by ryan, 9 years ago)

Sample implementation of *_transient()

  • wp-includes/functions.php

     
    625625}
    626626
    627627/**
     628 * Delete a transient
     629 *
     630 * @since 2.8.0
     631 * @package WordPress
     632 * @subpackage Transient
     633 *
     634 * @param string $transient Transient name. Expected to not be SQL-escaped
     635 * @return bool true if successful, false otherwise
     636 */
     637function delete_transient($transient) {
     638        global $_wp_using_ext_object_cache, $wpdb;
     639
     640        if ( $_wp_using_ext_object_cache ) {
     641                return wp_cache_delete($transient, 'transient');
     642        } else {
     643                $transient = $wpdb->escape($transient);
     644                return delete_option($transient);
     645        }
     646}
     647
     648/**
     649 * Get the value of a transient
     650 *
     651 * If the transient does not exist or does not have a value, then the return value
     652 * will be false.
     653 *
     654 * @since 2.8.0
     655 * @package WordPress
     656 * @subpackage Transient
     657 *
     658 * @param string $transient Transient name. Expected to not be SQL-escaped
     659 * @return mixed Value of transient
     660 */
     661function get_transient($transient) {
     662        global $_wp_using_ext_object_cache, $wpdb;
     663
     664        if ( $_wp_using_ext_object_cache ) {
     665                return wp_cache_get($transient, 'transient');
     666        } else {
     667                $transient = $wpdb->escape($transient);
     668                return get_option($transient);
     669        }
     670}
     671
     672/**
     673 * Set/update the value of a transient
     674 *
     675 * You do not need to serialize values, if the value needs to be serialize, then
     676 * it will be serialized before it is set.
     677 *
     678 * @since 2.8.0
     679 * @package WordPress
     680 * @subpackage Transient
     681 *
     682 * @param string $transient Transient name. Expected to not be SQL-escaped
     683 * @param mixed $value Transient value.
     684 * @return bool False if value was not set and true if value was set.
     685 */
     686function set_transient($transient, $value) {
     687        global $_wp_using_ext_object_cache, $wpdb;
     688
     689        if ( $_wp_using_ext_object_cache ) {
     690                return wp_cache_set($transient, $value, 'transient');
     691        } else {
     692                $safe_transient = $wpdb->escape($transient);
     693                if ( false === get_option( $safe_transient ) )
     694                        return add_option($transient, $value, '', 'no');
     695                else
     696                        return update_option($transient, $value);
     697        }
     698}
     699
     700/**
    628701 * Saves and restores user interface settings stored in a cookie.
    629702 *
    630703 * Checks if the current user-settings cookie is updated and stores it. When no
  • wp-includes/rewrite.php

     
    15961596         * @return array Rewrite rules.
    15971597         */
    15981598        function wp_rewrite_rules() {
    1599                 $this->rules = get_option('rewrite_rules');
     1599                $this->rules = get_transient('rewrite_rules');
    16001600                if ( empty($this->rules) ) {
    16011601                        $this->matches = 'matches';
    16021602                        $this->rewrite_rules();
    1603                         update_option('rewrite_rules', $this->rules);
     1603                        set_transient('rewrite_rules', $this->rules);
    16041604                }
    16051605
    16061606                return $this->rules;
     
    17831783         * @access public
    17841784         */
    17851785        function flush_rules() {
    1786                 delete_option('rewrite_rules');
     1786                delete_transient('rewrite_rules');
    17871787                $this->wp_rewrite_rules();
    17881788                if ( function_exists('save_mod_rewrite_rules') )
    17891789                        save_mod_rewrite_rules();
  • wp-includes/rss.php

     
    714714                $cache_option = 'rss_' . $this->file_name( $url );
    715715                $cache_timestamp = 'rss_' . $this->file_name( $url ) . '_ts';
    716716
    717                 // shouldn't these be using get_option() ?
    718                 if ( !$wpdb->get_var( $wpdb->prepare( "SELECT option_name FROM $wpdb->options WHERE option_name = %s", $cache_option ) ) )
    719                         add_option($cache_option, '', '', 'no');
    720                 if ( !$wpdb->get_var( $wpdb->prepare( "SELECT option_name FROM $wpdb->options WHERE option_name = %s", $cache_timestamp ) ) )
    721                         add_option($cache_timestamp, '', '', 'no');
     717                set_transient($cache_option, $rss);
     718                set_transient($cache_timestamp, time() );
    722719
    723                 update_option($cache_option, $rss);
    724                 update_option($cache_timestamp, time() );
    725 
    726720                return $cache_option;
    727721        }
    728722
     
    736730                $this->ERROR = "";
    737731                $cache_option = 'rss_' . $this->file_name( $url );
    738732
    739                 if ( ! get_option( $cache_option ) ) {
     733                if ( ! $rss = get_transient( $cache_option ) ) {
    740734                        $this->debug(
    741735                                "Cache doesn't contain: $url (cache option: $cache_option)"
    742736                        );
    743737                        return 0;
    744738                }
    745739
    746                 $rss = get_option( $cache_option );
    747 
    748740                return $rss;
    749741        }
    750742
     
    760752                $cache_option = $this->file_name( $url );
    761753                $cache_timestamp = 'rss_' . $this->file_name( $url ) . '_ts';
    762754
    763                 if ( $mtime = get_option($cache_timestamp) ) {
     755                if ( $mtime = get_transient($cache_timestamp) ) {
    764756                        // find how long ago the file was added to the cache
    765757                        // and whether that is longer then MAX_AGE
    766758                        $age = time() - $mtime;
  • wp-settings.php

     
    257257if ( is_wp_error($prefix) )
    258258        wp_die(/*WP_I18N_BAD_PREFIX*/'<strong>ERROR</strong>: <code>$table_prefix</code> in <code>wp-config.php</code> can only contain numbers, letters, and underscores.'/*/WP_I18N_BAD_PREFIX*/);
    259259
    260 if ( file_exists(WP_CONTENT_DIR . '/object-cache.php') )
     260if ( file_exists(WP_CONTENT_DIR . '/object-cache.php') ) {
    261261        require_once (WP_CONTENT_DIR . '/object-cache.php');
    262 else
     262        $_wp_using_ext_object_cache = true;
     263} else {
    263264        require_once (ABSPATH . WPINC . '/cache.php');
     265        $_wp_using_ext_object_cache = false;
     266}
    264267
    265268wp_cache_init();
    266269if ( function_exists('wp_cache_add_global_groups') ) {