Permalink
Browse files

Merge pull request #77 from Automattic/acm-tag-caching

Caching get_matching_ad_code return value
  • Loading branch information...
2 parents be10002 + 949c729 commit 3e7b858764d406c7995366f59a09394fc86d360f @rinatkhaziev rinatkhaziev committed Jun 5, 2013
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 3e7b858

Please sign in to comment.