From 0207e085fbcfbb4fe6bc207ecd86b6252f4f9b9a Mon Sep 17 00:00:00 2001 From: donncha Date: Tue, 25 Nov 2008 19:50:29 +0000 Subject: Cache rss feeds in sitemeta, added garbage collection git-svn-id: http://svn.automattic.com/wordpress-mu/branches/2.6@1547 7be80a69-a1ef-0310-a953-fb0f7c49ff36 --- wp-includes/rss.php | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/wp-includes/rss.php b/wp-includes/rss.php index 5b3741c..7636a75 100644 --- a/wp-includes/rss.php +++ b/wp-includes/rss.php @@ -673,12 +673,15 @@ class RSSCache { Output: true on sucess \*=======================================================================*/ function set ($url, $rss) { - global $wpdb; $cache_option = 'rss_' . $this->file_name( $url ); $cache_timestamp = 'rss_' . $this->file_name( $url ) . '_ts'; add_site_option( $cache_option, $rss ); - add_site_option( $cache_timestamp, $cache_timestamp ); + add_site_option( $cache_timestamp, time() ); + + if ( !wp_next_scheduled( 'wp_rss_gc' ) ) + wp_schedule_event(time(), 'twicedaily', 'wp_rss_gc'); + return $cache_option; } @@ -708,18 +711,11 @@ class RSSCache { Output: cached object on HIT, false on MISS \*=======================================================================*/ function check_cache ( $url ) { - global $wp_object_cache; $this->ERROR = ""; $cache_option = $this->file_name( $url ); $cache_timestamp = 'rss_' . $this->file_name( $url ) . '_ts'; - if( $wp_object_cache->cache_enabled ) { - $mtime = wp_cache_get( $cache_timestamp, 'rss' ); - } else { - $mtime = get_site_option($cache_timestamp); - } - - if ( $mtime ) { + if ( $mtime = get_site_option($cache_timestamp) ) { // find how long ago the file was added to the cache // and whether that is longer then MAX_AGE $age = time() - $mtime; @@ -873,4 +869,19 @@ function get_rss ($url, $num_items = 5) { // Like get posts, but for RSS } endif; +if ( !function_exists('rss_gc') ) : +function rss_gc() { + global $wpdb; + // Garbage Collection + $rows = $wpdb->get_results( "SELECT meta_key FROM {$wpdb->sitemeta} WHERE meta_key LIKE 'rss\_%\_ts' AND meta_value < unix_timestamp( date_sub( NOW(), interval 7200 second ) )" ); + if( is_array( $rows ) ) { + foreach( $rows as $row ) { + $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE meta_key = %s", $row->meta_key ) ); + $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE meta_key = %s", str_replace( '_ts', '', $row->meta_key ) ) ); + } + } +} +endif; +add_action( 'wp_rss_gc', 'rss_gc' ); + ?> -- cgit