Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Association field performance #1208

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 57 additions & 4 deletions core/Field/Association_Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ class Association_Field extends Field {
),
);

/**
* Caches results.
*
* @var array
*/
protected static $cache = array();

/**
* Create a field from a certain type with the specified label.
*
Expand Down Expand Up @@ -198,8 +205,9 @@ public function get_options( $args = array() ) {
global $wpdb;

$args = wp_parse_args( $args, array(
'page' => 1,
'term' => '',
'page' => 1,
'term' => '',
'cache' => true
) );

$sql_queries = array();
Expand All @@ -211,7 +219,17 @@ public function get_options( $args = array() ) {

$callback = "get_{$type['type']}_options_sql";

$sql_statement = $this->$callback( $type_args );
if ( $args['cache'] ) {
$cache_key = $callback . '_' . md5( maybe_serialize( $type_args ) );
if ( isset( self::$cache['get_options']['sql_statement'][ $cache_key ] ) ) {
$sql_statement = self::$cache['get_options']['sql_statement'][ $cache_key ];
} else {
$sql_statement = $this->$callback( $type_args );
self::$cache['get_options']['sql_statement'][ $cache_key ] = $sql_statement;
}
} else {
$sql_statement = $this->$callback( $type_args );
}

$sql_queries[] = $sql_statement;
}
Expand All @@ -223,7 +241,17 @@ public function get_options( $args = array() ) {

$sql_queries .= " ORDER BY `title` ASC LIMIT {$per_page} OFFSET {$offset}";

$results = $wpdb->get_results( $sql_queries );
if ( $args['cache'] ) {
$cache_key = md5( $sql_queries );
if ( isset( self::$cache['get_options']['results'][ $cache_key ] ) ) {
$results = self::$cache['get_options']['results'][ $cache_key ];
} else {
$results = $wpdb->get_results( $sql_queries );
self::$cache['get_options']['results'][ $cache_key ] = $results;
}
} else {
$results = $wpdb->get_results( $sql_queries );
}

$options = array();

Expand All @@ -241,6 +269,31 @@ public function get_options( $args = array() ) {
*/
$options = apply_filters( 'carbon_fields_association_field_options', $options, $this->get_base_name() );

if ( $args['cache'] ) {
$cache_key = md5( $sql_queries );
if ( isset( self::$cache['get_options']['options'][ $cache_key ] ) ) {
return self::$cache['get_options']['options'][ $cache_key ];
} else {
$options_array = $this->get_options_array( $sql_queries, $options );
self::$cache['get_options']['options'][ $cache_key ] = $options_array;

return $options_array;
}
} else {
return $this->get_options_array( $sql_queries, $options );
}
}

/**
* Returns an array with the options from get_options() method.
*
* @param $sql_queries
* @param $options
*
* @return array
*/
function get_options_array( $sql_queries, $options ) {
global $wpdb;
return array(
'total_options' => $wpdb->get_var( "SELECT COUNT(*) FROM (" . preg_replace( '~(LIMIT .*)$~', '', $sql_queries ) . ") as t" ),
'options' => $options,
Expand Down