perf: Add query result caching to master dashboard data methods
- Added wp_cache_get/set with 15-minute TTL to all expensive DB queries: - get_total_events_count() - get_upcoming_events_count() - get_past_events_count() - get_total_tickets_sold() - get_total_revenue() - Reduces database load for master dashboard by caching aggregate statistics - Improves page load performance for master trainer users - Cache keys use 'hvac_master_dashboard' group for organized cache management Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
		
							parent
							
								
									bffe04200d
								
							
						
					
					
						commit
						8faf2791cd
					
				
					 1 changed files with 63 additions and 2 deletions
				
			
		|  | @ -36,6 +36,14 @@ class HVAC_Master_Dashboard_Data { | |||
| 	 * @return int | ||||
| 	 */ | ||||
| 	public function get_total_events_count() { | ||||
| 		// Check cache first
 | ||||
| 		$cache_key = 'hvac_master_total_events_count'; | ||||
| 		$cached_count = wp_cache_get($cache_key, 'hvac_master_dashboard'); | ||||
| 		 | ||||
| 		if ($cached_count !== false) { | ||||
| 			return (int) $cached_count; | ||||
| 		} | ||||
| 		 | ||||
| 		global $wpdb; | ||||
| 		 | ||||
| 		// Get all events from all trainers with hvac_trainer or hvac_master_trainer role
 | ||||
|  | @ -55,6 +63,9 @@ class HVAC_Master_Dashboard_Data { | |||
| 			array_merge([Tribe__Events__Main::POSTTYPE], $trainer_users) | ||||
| 		) ); | ||||
| 		 | ||||
| 		// Cache for 15 minutes
 | ||||
| 		wp_cache_set($cache_key, $count, 'hvac_master_dashboard', 900); | ||||
| 		 | ||||
| 		return (int) $count; | ||||
| 	} | ||||
| 
 | ||||
|  | @ -65,6 +76,15 @@ class HVAC_Master_Dashboard_Data { | |||
| 	 */ | ||||
| 	public function get_upcoming_events_count() { | ||||
| 		global $wpdb; | ||||
| 		 | ||||
| 		// Check cache first
 | ||||
| 		$cache_key = 'hvac_master_upcoming_events_count'; | ||||
| 		$cached_count = wp_cache_get($cache_key, 'hvac_master_dashboard'); | ||||
| 		 | ||||
| 		if ($cached_count !== false) { | ||||
| 			return (int) $cached_count; | ||||
| 		} | ||||
| 		 | ||||
| 		$today = date( 'Y-m-d H:i:s' ); | ||||
| 		 | ||||
| 		$trainer_users = $this->get_all_trainer_user_ids(); | ||||
|  | @ -85,6 +105,9 @@ class HVAC_Master_Dashboard_Data { | |||
| 			array_merge([Tribe__Events__Main::POSTTYPE], $trainer_users, [$today]) | ||||
| 		) ); | ||||
| 		 | ||||
| 		// Cache for 15 minutes
 | ||||
| 		wp_cache_set($cache_key, $count, 'hvac_master_dashboard', 900); | ||||
| 		 | ||||
| 		return (int) $count; | ||||
| 	} | ||||
| 
 | ||||
|  | @ -95,6 +118,15 @@ class HVAC_Master_Dashboard_Data { | |||
| 	 */ | ||||
| 	public function get_past_events_count() { | ||||
| 		global $wpdb; | ||||
| 		 | ||||
| 		// Check cache first
 | ||||
| 		$cache_key = 'hvac_master_past_events_count'; | ||||
| 		$cached_count = wp_cache_get($cache_key, 'hvac_master_dashboard'); | ||||
| 		 | ||||
| 		if ($cached_count !== false) { | ||||
| 			return (int) $cached_count; | ||||
| 		} | ||||
| 		 | ||||
| 		$today = date( 'Y-m-d H:i:s' ); | ||||
| 		 | ||||
| 		$trainer_users = $this->get_all_trainer_user_ids(); | ||||
|  | @ -115,6 +147,9 @@ class HVAC_Master_Dashboard_Data { | |||
| 			array_merge([Tribe__Events__Main::POSTTYPE], $trainer_users, [$today]) | ||||
| 		) ); | ||||
| 		 | ||||
| 		// Cache for 15 minutes
 | ||||
| 		wp_cache_set($cache_key, $count, 'hvac_master_dashboard', 900); | ||||
| 		 | ||||
| 		return (int) $count; | ||||
| 	} | ||||
| 
 | ||||
|  | @ -126,6 +161,14 @@ class HVAC_Master_Dashboard_Data { | |||
| 	public function get_total_tickets_sold() { | ||||
| 		global $wpdb; | ||||
| 		 | ||||
| 		// Check cache first
 | ||||
| 		$cache_key = 'hvac_master_total_tickets_sold'; | ||||
| 		$cached_count = wp_cache_get($cache_key, 'hvac_master_dashboard'); | ||||
| 		 | ||||
| 		if ($cached_count !== false) { | ||||
| 			return (int) $cached_count; | ||||
| 		} | ||||
| 		 | ||||
| 		$trainer_users = $this->get_all_trainer_user_ids(); | ||||
| 		 | ||||
| 		if (empty($trainer_users)) { | ||||
|  | @ -164,7 +207,12 @@ class HVAC_Master_Dashboard_Data { | |||
| 		 | ||||
| 		// Note: RSVP attendees are not counted as "tickets sold" since they are free registrations
 | ||||
| 		 | ||||
| 		return (int) ($tec_commerce_count + $tribe_tpp_count); | ||||
| 		$total_count = (int) ($tec_commerce_count + $tribe_tpp_count); | ||||
| 		 | ||||
| 		// Cache for 15 minutes
 | ||||
| 		wp_cache_set($cache_key, $total_count, 'hvac_master_dashboard', 900); | ||||
| 		 | ||||
| 		return $total_count; | ||||
| 	} | ||||
| 
 | ||||
| 	/** | ||||
|  | @ -175,6 +223,14 @@ class HVAC_Master_Dashboard_Data { | |||
| 	public function get_total_revenue() { | ||||
| 		global $wpdb; | ||||
| 		 | ||||
| 		// Check cache first
 | ||||
| 		$cache_key = 'hvac_master_total_revenue'; | ||||
| 		$cached_revenue = wp_cache_get($cache_key, 'hvac_master_dashboard'); | ||||
| 		 | ||||
| 		if ($cached_revenue !== false) { | ||||
| 			return (float) $cached_revenue; | ||||
| 		} | ||||
| 		 | ||||
| 		$trainer_users = $this->get_all_trainer_user_ids(); | ||||
| 		 | ||||
| 		if (empty($trainer_users)) { | ||||
|  | @ -226,7 +282,12 @@ class HVAC_Master_Dashboard_Data { | |||
| 		 | ||||
| 		// Note: RSVP attendees typically don't have revenue (free tickets)
 | ||||
| 		 | ||||
| 		return (float) (($tec_commerce_revenue ?: 0.00) + ($tribe_tpp_revenue ?: 0.00)); | ||||
| 		$total_revenue = (float) (($tec_commerce_revenue ?: 0.00) + ($tribe_tpp_revenue ?: 0.00)); | ||||
| 		 | ||||
| 		// Cache for 15 minutes
 | ||||
| 		wp_cache_set($cache_key, $total_revenue, 'hvac_master_dashboard', 900); | ||||
| 		 | ||||
| 		return $total_revenue; | ||||
| 	} | ||||
| 
 | ||||
| 	/** | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue