From 5157d990a240b02a7744f4994c473b8d996e7a93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20dos=20Santos=20Gon=C3=A7alves=20Pacheco?= Date: Wed, 13 Dec 2023 20:31:46 -0300 Subject: [PATCH] Cache database results from the Association field --- core/Field/Association_Field.php | 61 +++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/core/Field/Association_Field.php b/core/Field/Association_Field.php index d46331b3..d0cff377 100644 --- a/core/Field/Association_Field.php +++ b/core/Field/Association_Field.php @@ -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. * @@ -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(); @@ -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; } @@ -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(); @@ -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,