From fa0f45b5e4fb08cc5ac2b20bc1ce8a688b838597 Mon Sep 17 00:00:00 2001 From: arttu Date: Wed, 2 Jan 2019 11:58:49 +0200 Subject: [PATCH 1/4] optimize post data stored in redis --- CHANGELOG.md | 3 +++ content/themes/custom/models/search.php | 23 +++++++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 47e732e..8cad64d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,9 @@ Perustuu projektiin [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) - PO-288: Teeman värit manifest tiedostoon ja meta tagiin - PO-267: Näytetään kun haku on käynnissä +### Muutettu +- PO-351: Optimoitu redikseen menevää dataa + ### Korjattu - PO-293: Vinkkien järjestäminen diff --git a/content/themes/custom/models/search.php b/content/themes/custom/models/search.php index 7f86851..fef8030 100644 --- a/content/themes/custom/models/search.php +++ b/content/themes/custom/models/search.php @@ -286,13 +286,28 @@ protected function do_pagination( array $posts, stdClass $params ) { * Get single acf post by post id * * @param mixed $post_id Post id. - * @return \WP_Post + * @return \stdClass */ protected function get_single_acf_post( $post_id ) { $cache_key = 'acfpost/' . $post_id; $post = wp_cache_get( $cache_key ); if ( empty( $post ) ) { - $post = \DustPress\Query::get_acf_post( $post_id ); + $post_obj = get_post( $post_id ); + + // Construct the actual used post object + $post = (object) [ + 'post_title' => $post_obj->post_title, + 'post_type' => $post_obj->post_type, + 'permalink' => get_permalink( $post_id ), + 'post_excerpt' => ! empty( $post_obj->post_excerpt ) ? $post_obj->post_excerpt : $post_obj->post_content, + 'fields' => [ + 'api_guid' => get_field( 'api_guid', $post_id ), + 'api_type' => get_field( 'api_type', $post_id ), + 'api_path' => get_field( 'api_path', $post_id ), + 'api_images' => get_field( 'api_images', $post_id ), + 'tags' => get_field( 'tags', $post_id ), + ], + ]; wp_cache_set( $cache_key, $post, null, HOUR_IN_SECONDS ); } @@ -363,14 +378,14 @@ protected function get_post_data( stdClass $result, stdClass $params ) { } // Overwrite tip link and title with parents - $post->permalink = $parent_post->permalink . '#' . $post->post_name; + $post->permalink = $parent_post->permalink . '#' . $post->post_title; $post->post_title = $parent_post->post_title; $post->fields = $parent_post->fields; $post->fields['api_type'] = 'pof_tip'; } // Map item parents - $api_path = json_decode_pof( $post->fields['api_path'] ?? '[]' ); + $api_path = json_decode_pof( $post->fields['api_path'] ?? '[]' ) ?? []; if ( $post->post_type === 'pof_tip' ) { // If this is a tip then add the task itself to the path as well $api_path[] = [ From fb53327e198c15319f40c7e49445030968ed2af2 Mon Sep 17 00:00:00 2001 From: arttu Date: Wed, 2 Jan 2019 12:17:59 +0200 Subject: [PATCH 2/4] optimize pof_tip cache calls --- content/themes/custom/models/search.php | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/content/themes/custom/models/search.php b/content/themes/custom/models/search.php index fef8030..8adb135 100644 --- a/content/themes/custom/models/search.php +++ b/content/themes/custom/models/search.php @@ -301,11 +301,12 @@ protected function get_single_acf_post( $post_id ) { 'permalink' => get_permalink( $post_id ), 'post_excerpt' => ! empty( $post_obj->post_excerpt ) ? $post_obj->post_excerpt : $post_obj->post_content, 'fields' => [ - 'api_guid' => get_field( 'api_guid', $post_id ), - 'api_type' => get_field( 'api_type', $post_id ), - 'api_path' => get_field( 'api_path', $post_id ), - 'api_images' => get_field( 'api_images', $post_id ), - 'tags' => get_field( 'tags', $post_id ), + 'api_guid' => get_field( 'api_guid', $post_id ), + 'api_type' => get_field( 'api_type', $post_id ), + 'api_path' => get_field( 'api_path', $post_id ), + 'api_images' => get_field( 'api_images', $post_id ), + 'tags' => get_field( 'tags', $post_id ), + 'pof_tip_parent' => get_field( 'pof_tip_parent', $post_id ), ], ]; @@ -367,15 +368,7 @@ protected function get_post_data( stdClass $result, stdClass $params ) { if ( $post->post_type === 'pof_tip' ) { // Get tip parent link & title - $tip_cache_key = 'tip_parent/' . $id; - $parent_post = wp_cache_get( $tip_cache_key ); - if ( empty( $parent_post ) ) { - // Get post parent - $parent_id = get_post_meta( $id, 'pof_tip_parent', true ); - $parent_post = $this->get_single_acf_post( $parent_id ); - - wp_cache_set( $tip_cache_key, $parent_post, null, HOUR_IN_SECONDS ); - } + $parent_post = $this->get_single_acf_post( $post->fields['pof_tip_parent'] ); // Overwrite tip link and title with parents $post->permalink = $parent_post->permalink . '#' . $post->post_title; From 9c1a6113033426daa6ac0eb9b1e83d044e878cd3 Mon Sep 17 00:00:00 2001 From: arttu Date: Wed, 2 Jan 2019 12:56:09 +0200 Subject: [PATCH 3/4] more cache optimizations --- .../functions/function-api_data_helpers.php | 57 ------------- content/themes/custom/models/search.php | 80 ++++++++++++++++++- 2 files changed, 79 insertions(+), 58 deletions(-) diff --git a/content/themes/custom/functions/function-api_data_helpers.php b/content/themes/custom/functions/function-api_data_helpers.php index 998e345..049abc3 100644 --- a/content/themes/custom/functions/function-api_data_helpers.php +++ b/content/themes/custom/functions/function-api_data_helpers.php @@ -53,29 +53,6 @@ function map_api_tags( &$repeater ) { } } -/** - * Modify parents array - * - * @param array $parents Parent list. - * @return array Modified $parents. - */ -function map_api_parents( array $parents ) { - // Remove programs from parents - $parents = array_filter( $parents, function( $parent ) { - return ! empty( $parent->type ) && $parent->type !== 'program'; - }); - - // Add logo data to agegroups - $parents = array_map( function( $parent ) { - if ( $parent->type === 'agegroup' ) { - $parent->logo = get_images_by_guid( $parent->guid ); - } - return $parent; - }, $parents ); - - return $parents; -} - // loads all children of a page in a tree format function get_child_page_tree( $post_id, $helper, $grandchildren = true ) { @@ -181,40 +158,6 @@ function pofapilink_shortcode( $atts ) { } add_shortcode( 'pofapilink', 'pofapilink_shortcode' ); -/** - * Get api images by guid - * - * @param string $guid Api item guid. - * @return mixed Image object or null. - */ -function get_images_by_guid( $guid ) { - $cache_key = 'get_images_by_guid/' . $guid . '/'; - $result = wp_cache_get( $cache_key ); - if ( empty( $result ) ) { - $args = [ - 'posts_per_page' => 1, - 'post_type' => 'page', - 'post_status' => 'publish', - 'meta_query' => [ - [ - 'key' => 'api_guid', - 'value' => $guid, - ], - ], - ]; - $pages = \DustPress\Query::get_acf_posts( $args ); - - if ( ! empty( $pages ) ) { - map_api_images( $pages[0]->fields['api_images'] ); - $result = $pages[0]->fields['api_images'][0]['logo']; - } - - wp_cache_set( $cache_key, $result, null, HOUR_IN_SECONDS ); - } - - return $result; -} - // Get hero args function get_hero_args() { $args = [ diff --git a/content/themes/custom/models/search.php b/content/themes/custom/models/search.php index 8adb135..85dc996 100644 --- a/content/themes/custom/models/search.php +++ b/content/themes/custom/models/search.php @@ -311,6 +311,10 @@ protected function get_single_acf_post( $post_id ) { ]; wp_cache_set( $cache_key, $post, null, HOUR_IN_SECONDS ); + + // Save id for guid data retrieval + $guid_cache_key = 'get_id_by_guid/' . $post->fields['api_guid'] . '/'; + wp_cache_set( $guid_cache_key, $post_id, null, HOUR_IN_SECONDS ); } return $post; @@ -391,7 +395,7 @@ protected function get_post_data( stdClass $result, stdClass $params ) { ], ]; } - $post->parents = map_api_parents( $api_path ); + $post->parents = array_reduce( $api_path, [ $this, 'map_api_parents' ], [] ); // Decode images map_api_images( $post->fields['api_images'] ); @@ -416,6 +420,80 @@ protected function get_post_data( stdClass $result, stdClass $params ) { return $result->posts; } + /** + * Set api item parents + * Should be used via array_reduce + * + * @param array $parents Complete array. + * @param array|\stdClass $parent Parent object. + * @return array Modified $parents. + */ + public function map_api_parents( array $parents, $parent ) { + $parent = (object) $parent; + + // Remove programs from parents + if ( ! $parent->type !== 'program' ) { + + // Add logo data to agegroup parent + if ( $parent->type === 'agegroup' ) { + $parent->logo = $this->get_images_by_guid( $parent->guid ); + } + + $parents[] = $parent; + } + + return $parents; + } + + /** + * Get api images by guid + * + * @param string $guid Api guid of post to get images of. + * @return mixed + */ + public function get_images_by_guid( string $guid ) { + $id = $this->get_id_by_guid( $guid ); + $result = null; + if ( ! empty( $id ) ) { + $post = $this->get_single_acf_post( $id ); + $result = $post->fields['api_images']; + map_api_images( $result ); + } + + return $result; + } + + /** + * Get post id by guid + * + * @param string $guid Post api guid. + * @return mixed + */ + public function get_id_by_guid( string $guid ) { + $cache_key = 'get_id_by_guid/' . $guid . '/'; + $result = wp_cache_get( $cache_key ); + if ( $result === false ) { + $args = [ + 'posts_per_page' => 1, + 'post_type' => 'page', + 'post_status' => 'publish', + 'fields' => 'ids', + 'meta_query' => [ + [ + 'key' => 'api_guid', + 'value' => $guid, + ], + ], + ]; + + $query = ( new WP_Query( $args ) )->posts[0] ?? null; + + wp_cache_set( $cache_key, $result, null, HOUR_IN_SECONDS ); + } + + return $result; + } + /** * Get api item term * From 24d9056ac0b24098565b09119f12a0df36f1fc48 Mon Sep 17 00:00:00 2001 From: arttu Date: Wed, 2 Jan 2019 13:04:22 +0200 Subject: [PATCH 4/4] id by guid locale fix --- content/themes/custom/models/search.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/themes/custom/models/search.php b/content/themes/custom/models/search.php index 85dc996..b187aeb 100644 --- a/content/themes/custom/models/search.php +++ b/content/themes/custom/models/search.php @@ -313,7 +313,7 @@ protected function get_single_acf_post( $post_id ) { wp_cache_set( $cache_key, $post, null, HOUR_IN_SECONDS ); // Save id for guid data retrieval - $guid_cache_key = 'get_id_by_guid/' . $post->fields['api_guid'] . '/'; + $guid_cache_key = 'get_id_by_guid/' . $post->fields['api_guid'] . '/' . get_locale(); wp_cache_set( $guid_cache_key, $post_id, null, HOUR_IN_SECONDS ); } @@ -470,7 +470,7 @@ public function get_images_by_guid( string $guid ) { * @return mixed */ public function get_id_by_guid( string $guid ) { - $cache_key = 'get_id_by_guid/' . $guid . '/'; + $cache_key = 'get_id_by_guid/' . $guid . '/' . get_locale(); $result = wp_cache_get( $cache_key ); if ( $result === false ) { $args = [