Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Caching get_matching_ad_code return value #77

Merged
merged 1 commit into from

1 participant

@rinatkhaziev
Collaborator

get_matching_ad_code() might be expensive when there's a lot of ad codes with complex conditionals. Mitigate that by caching matched ad code results.

@rinatkhaziev rinatkhaziev merged commit 3e7b858 into master
@rinatkhaziev rinatkhaziev deleted the acm-tag-caching branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 5, 2013
  1. @rinatkhaziev
This page is out of date. Refresh to see the latest.
Showing with 10 additions and 0 deletions.
  1. +10 −0 ad-code-manager.php
View
10 ad-code-manager.php
@@ -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;
}
Something went wrong with that request. Please try again.