Permalink
Browse files

Caching get_matching_ad_code return value as it might be expensive wh…

…en there's a lot of ad codes
  • Loading branch information...
rinatkhaziev committed Jun 5, 2013
1 parent be10002 commit 949c729bc0dae4fe9e04e9727bedcd01f145551f
Showing with 10 additions and 0 deletions.
  1. +10 −0 ad-code-manager.php
View
@@ -886,10 +886,18 @@ function action_acm_tag( $tag_id ) {
* @since 0.4
*/
public function get_matching_ad_code( $tag_id ) {
+ global $wp_query;
// If there aren't any ad codes, it's not worth it for us to do anything.
if ( !isset( $this->ad_codes[$tag_id] ) )
return;
+ // This method might be expensive when there's a lot of ad codes
+ // So instead of executing over and over again, return cached matching ad code
+ $cache_key = "acm:{$tag_id}:" . md5( implode( ',', $wp_query->query_vars ) );
+
+ if ( false !== $ad_code = wp_cache_get( $cache_key, 'acm' ) )
+ return $ad_code;
+
// Run our ad codes through all of the conditionals to make sure we should
// be displaying it
$display_codes = array();
@@ -990,6 +998,8 @@ public function get_matching_ad_code( $tag_id ) {
ksort( $prioritized_display_codes, SORT_NUMERIC );
$code_to_display = array_shift( array_shift( $prioritized_display_codes ) );
+ wp_cache_add( $cache_key, $code_to_display, 'acm', 600 );
+
return $code_to_display;
}

0 comments on commit 949c729

Please sign in to comment.