-
Notifications
You must be signed in to change notification settings - Fork 896
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
537 refactor microcaching the api endpoints into using cache decorators #21879
Changes from 4 commits
f809804
bf9d254
9526999
a25f378
4408c2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong | ||
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded | ||
namespace Yoast\WP\SEO\Dashboard\Infrastructure\Score_Results\Readability_Score_Results; | ||
|
||
use WPSEO_Utils; | ||
use Yoast\WP\SEO\Dashboard\Domain\Content_Types\Content_Type; | ||
use Yoast\WP\SEO\Dashboard\Domain\Score_Groups\Readability_Score_Groups\Readability_Score_Groups_Interface; | ||
use Yoast\WP\SEO\Dashboard\Domain\Score_Results\Score_Results_Not_Found_Exception; | ||
use Yoast\WP\SEO\Dashboard\Infrastructure\Score_Results\Score_Results_Collector_Interface; | ||
|
||
/** | ||
* The caching decorator to get readability score results. | ||
*/ | ||
class Cached_Readability_Score_Results_Collector implements Score_Results_Collector_Interface { | ||
|
||
/** | ||
* The actual collector implementation. | ||
* | ||
* @var Readability_Score_Results_Collector $readability_score_results_collector | ||
*/ | ||
private $readability_score_results_collector; | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param Readability_Score_Results_Collector $readability_score_results_collector The collector implementation to | ||
* use. | ||
*/ | ||
public function __construct( Readability_Score_Results_Collector $readability_score_results_collector ) { | ||
$this->readability_score_results_collector = $readability_score_results_collector; | ||
} | ||
|
||
/** | ||
* Retrieves readability score results for a content type. | ||
* Based on caching returns either the result or gets it from the collector. | ||
* | ||
* @param Readability_Score_Groups_Interface[] $score_groups All readability score groups. | ||
* @param Content_Type $content_type The content type. | ||
* @param int|null $term_id The ID of the term we're filtering for. | ||
* @param bool|null $is_troubleshooting Whether we're in troubleshooting mode. | ||
* | ||
* @throws Score_Results_Not_Found_Exception When the query of getting score results fails. | ||
* @return array<string, object|bool|float> The readability score results for a content type. | ||
*/ | ||
public function get_score_results( | ||
array $score_groups, | ||
Content_Type $content_type, | ||
?int $term_id, | ||
?bool $is_troubleshooting | ||
) { | ||
$content_type_name = $content_type->get_name(); | ||
$transient_name = Readability_Score_Results_Collector::READABILITY_SCORES_TRANSIENT . '_' . $content_type_name . ( ( $term_id === null ) ? '' : '_' . $term_id ); | ||
|
||
$results = []; | ||
$transient = \get_transient( $transient_name ); | ||
if ( $is_troubleshooting !== true && $transient !== false ) { | ||
$results['scores'] = \json_decode( $transient, false ); | ||
$results['cache_used'] = true; | ||
$results['query_time'] = 0; | ||
|
||
return $results; | ||
} | ||
$results = $this->readability_score_results_collector->get_score_results( $score_groups, $content_type, $term_id, $is_troubleshooting ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like an empty line above, but that's maybe just me? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for the other cached collector |
||
if ( $is_troubleshooting !== true ) { | ||
\set_transient( $transient_name, WPSEO_Utils::format_json_encode( $results['scores'] ), \MINUTE_IN_SECONDS ); | ||
} | ||
return $results; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded | ||
namespace Yoast\WP\SEO\Dashboard\Infrastructure\Score_Results\Readability_Score_Results; | ||
|
||
use WPSEO_Utils; | ||
use Yoast\WP\Lib\Model; | ||
use Yoast\WP\SEO\Dashboard\Domain\Content_Types\Content_Type; | ||
use Yoast\WP\SEO\Dashboard\Domain\Score_Groups\Readability_Score_Groups\Readability_Score_Groups_Interface; | ||
|
@@ -34,18 +33,7 @@ public function get_score_results( array $readability_score_groups, Content_Type | |
$results = []; | ||
|
||
$content_type_name = $content_type->get_name(); | ||
$transient_name = self::READABILITY_SCORES_TRANSIENT . '_' . $content_type_name . ( ( $term_id === null ) ? '' : '_' . $term_id ); | ||
|
||
$transient = \get_transient( $transient_name ); | ||
if ( $is_troubleshooting !== true && $transient !== false ) { | ||
$results['scores'] = \json_decode( $transient, false ); | ||
$results['cache_used'] = true; | ||
$results['query_time'] = 0; | ||
|
||
return $results; | ||
} | ||
|
||
$select = $this->build_select( $readability_score_groups, $is_troubleshooting ); | ||
$select = $this->build_select( $readability_score_groups, $is_troubleshooting ); | ||
|
||
$replacements = \array_merge( | ||
\array_values( $select['replacements'] ), | ||
|
@@ -106,10 +94,6 @@ public function get_score_results( array $readability_score_groups, Content_Type | |
|
||
$end_time = \microtime( true ); | ||
|
||
if ( $is_troubleshooting !== true ) { | ||
\set_transient( $transient_name, WPSEO_Utils::format_json_encode( $current_scores ), \MINUTE_IN_SECONDS ); | ||
} | ||
|
||
$results['scores'] = $current_scores; | ||
$results['cache_used'] = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that the collector should be agnostic of anything cache related, so I think it's the cached collectors' responsibility to set |
||
$results['query_time'] = ( $end_time - $start_time ); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong | ||
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded | ||
namespace Yoast\WP\SEO\Dashboard\Infrastructure\Score_Results\SEO_Score_Results; | ||
|
||
use WPSEO_Utils; | ||
use Yoast\WP\SEO\Dashboard\Domain\Content_Types\Content_Type; | ||
use Yoast\WP\SEO\Dashboard\Domain\Score_Groups\SEO_Score_Groups\SEO_Score_Groups_Interface; | ||
use Yoast\WP\SEO\Dashboard\Domain\Score_Results\Score_Results_Not_Found_Exception; | ||
use Yoast\WP\SEO\Dashboard\Infrastructure\Score_Results\Score_Results_Collector_Interface; | ||
|
||
/** | ||
* The caching decorator to get readability score results. | ||
*/ | ||
class Cached_SEO_Score_Results_Collector implements Score_Results_Collector_Interface { | ||
|
||
/** | ||
* The actual collector implementation. | ||
* | ||
* @var SEO_Score_Results_Collector $seo_score_results_collector | ||
*/ | ||
private $seo_score_results_collector; | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param SEO_Score_Results_Collector $seo_score_results_collector The collector implementation to use. | ||
*/ | ||
public function __construct( SEO_Score_Results_Collector $seo_score_results_collector ) { | ||
$this->seo_score_results_collector = $seo_score_results_collector; | ||
} | ||
|
||
/** | ||
* Retrieves the SEO score results for a content type. | ||
* Based on caching returns either the result or gets it from the collector. | ||
* | ||
* @param SEO_Score_Groups_Interface[] $score_groups All SEO score groups. | ||
* @param Content_Type $content_type The content type. | ||
* @param int|null $term_id The ID of the term we're filtering for. | ||
* @param bool|null $is_troubleshooting Whether we're in troubleshooting mode. | ||
* | ||
* @throws Score_Results_Not_Found_Exception When the query of getting score results fails. | ||
* @return array<string, object|bool|float> The SEO score results for a content type. | ||
*/ | ||
public function get_score_results( | ||
array $score_groups, | ||
Content_Type $content_type, | ||
?int $term_id, | ||
?bool $is_troubleshooting | ||
) { | ||
$content_type_name = $content_type->get_name(); | ||
$transient_name = SEO_Score_Results_Collector::SEO_SCORES_TRANSIENT . '_' . $content_type_name . ( ( $term_id === null ) ? '' : '_' . $term_id ); | ||
$results = []; | ||
$transient = \get_transient( $transient_name ); | ||
if ( $is_troubleshooting !== true && $transient !== false ) { | ||
$results['scores'] = \json_decode( $transient, false ); | ||
$results['cache_used'] = true; | ||
$results['query_time'] = 0; | ||
|
||
return $results; | ||
} | ||
$results = $this->seo_score_results_collector->get_score_results( $score_groups, $content_type, $term_id, $is_troubleshooting ); | ||
if ( $is_troubleshooting !== true ) { | ||
\set_transient( $transient_name, WPSEO_Utils::format_json_encode( $results['scores'] ), \MINUTE_IN_SECONDS ); | ||
} | ||
return $results; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
READABILITY_SCORES_TRANSIENT
constant should be set in this class, instead of the cache-agnostic collector, I think.