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

537 refactor microcaching the api endpoints into using cache decorators #21879

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use Yoast\WP\SEO\Dashboard\Application\Score_Results\Abstract_Score_Results_Repository;
use Yoast\WP\SEO\Dashboard\Domain\Score_Groups\Readability_Score_Groups\Readability_Score_Groups_Interface;
use Yoast\WP\SEO\Dashboard\Infrastructure\Score_Results\Readability_Score_Results\Readability_Score_Results_Collector;
use Yoast\WP\SEO\Dashboard\Infrastructure\Score_Results\Readability_Score_Results\Cached_Readability_Score_Results_Collector;

/**
* The repository to get readability score results.
Expand All @@ -15,11 +15,11 @@ class Readability_Score_Results_Repository extends Abstract_Score_Results_Reposi
/**
* The constructor.
*
* @param Readability_Score_Results_Collector $readability_score_results_collector The readability score results collector.
* @param Readability_Score_Groups_Interface ...$readability_score_groups All readability score groups.
* @param Cached_Readability_Score_Results_Collector $readability_score_results_collector The cached readability score results collector.
* @param Readability_Score_Groups_Interface ...$readability_score_groups All readability score groups.
*/
public function __construct(
Readability_Score_Results_Collector $readability_score_results_collector,
Cached_Readability_Score_Results_Collector $readability_score_results_collector,
Readability_Score_Groups_Interface ...$readability_score_groups
) {
$this->score_results_collector = $readability_score_results_collector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use Yoast\WP\SEO\Dashboard\Application\Score_Results\Abstract_Score_Results_Repository;
use Yoast\WP\SEO\Dashboard\Domain\Score_Groups\SEO_Score_Groups\SEO_Score_Groups_Interface;
use Yoast\WP\SEO\Dashboard\Infrastructure\Score_Results\SEO_Score_Results\SEO_Score_Results_Collector;
use Yoast\WP\SEO\Dashboard\Infrastructure\Score_Results\SEO_Score_Results\Cached_SEO_Score_Results_Collector;

/**
* The repository to get SEO score results.
Expand All @@ -15,11 +15,11 @@ class SEO_Score_Results_Repository extends Abstract_Score_Results_Repository {
/**
* The constructor.
*
* @param SEO_Score_Results_Collector $seo_score_results_collector The SEO score results collector.
* @param SEO_Score_Groups_Interface ...$seo_score_groups All SEO score groups.
* @param Cached_SEO_Score_Results_Collector $seo_score_results_collector The cached SEO score results collector.
* @param SEO_Score_Groups_Interface ...$seo_score_groups All SEO score groups.
*/
public function __construct(
SEO_Score_Results_Collector $seo_score_results_collector,
Cached_SEO_Score_Results_Collector $seo_score_results_collector,
SEO_Score_Groups_Interface ...$seo_score_groups
) {
$this->score_results_collector = $seo_score_results_collector;
Expand Down
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 );
Copy link
Contributor

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.


$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 );
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Up @@ -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;
Expand Down Expand Up @@ -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'] ),
Expand Down Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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['cache_used'] = false;

$results['query_time'] = ( $end_time - $start_time );
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded
namespace Yoast\WP\SEO\Dashboard\Infrastructure\Score_Results\SEO_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\SEO_Score_Groups\SEO_Score_Groups_Interface;
Expand Down Expand Up @@ -34,18 +33,7 @@ public function get_score_results( array $seo_score_groups, Content_Type $conten
$results = [];

$content_type_name = $content_type->get_name();
$transient_name = self::SEO_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( $seo_score_groups, $is_troubleshooting );
$select = $this->build_select( $seo_score_groups, $is_troubleshooting );

$replacements = \array_merge(
\array_values( $select['replacements'] ),
Expand Down Expand Up @@ -108,10 +96,6 @@ public function get_score_results( array $seo_score_groups, Content_Type $conten

$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;
$results['query_time'] = ( $end_time - $start_time );
Expand Down
Loading