Skip to content

Commit

Permalink
Add microcaching around the score queries
Browse files Browse the repository at this point in the history
  • Loading branch information
leonidasmi committed Nov 22, 2024
1 parent 610c0d8 commit 9ad483b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded
namespace Yoast\WP\SEO\Dashboard\Infrastructure\Scores\Readability_Scores;

use WPSEO_Utils;
use Yoast\WP\Lib\Model;
use Yoast\WP\SEO\Dashboard\Domain\Content_Types\Content_Type;
use Yoast\WP\SEO\Dashboard\Domain\Scores\Readability_Scores\Readability_Scores_Interface;
Expand All @@ -13,6 +14,8 @@
*/
class Readability_Scores_Collector implements Scores_Collector_Interface {

public const READABILITY_SCORES_TRANSIENT = 'wpseo_readability_scores';

/**
* Retrieves the current readability scores for a content type.
*
Expand All @@ -24,13 +27,22 @@ class Readability_Scores_Collector implements Scores_Collector_Interface {
*/
public function get_current_scores( array $readability_scores, Content_Type $content_type, ?int $term_id ) {
global $wpdb;

$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 ( $transient !== false ) {
return \json_decode( $transient, false );
}

$select = $this->build_select( $readability_scores );

$replacements = \array_merge(
\array_values( $select['replacements'] ),
[
Model::get_table_name( 'Indexable' ),
$content_type->get_name(),
$content_type_name,
]
);

Expand All @@ -51,6 +63,7 @@ public function get_current_scores( array $readability_scores, Content_Type $con
)
);
//phpcs:enable
\set_transient( $transient_name, WPSEO_Utils::format_json_encode( $current_scores ), ( \MINUTE_IN_SECONDS ) );
return $current_scores;

}
Expand Down Expand Up @@ -79,6 +92,7 @@ public function get_current_scores( array $readability_scores, Content_Type $con
)
);
//phpcs:enable
\set_transient( $transient_name, WPSEO_Utils::format_json_encode( $current_scores ), ( \MINUTE_IN_SECONDS ) );
return $current_scores;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded
namespace Yoast\WP\SEO\Dashboard\Infrastructure\Scores\SEO_Scores;

use WPSEO_Utils;
use Yoast\WP\Lib\Model;
use Yoast\WP\SEO\Dashboard\Domain\Content_Types\Content_Type;
use Yoast\WP\SEO\Dashboard\Domain\Scores\SEO_Scores\SEO_Scores_Interface;
Expand All @@ -13,6 +14,8 @@
*/
class SEO_Scores_Collector implements Scores_Collector_Interface {

public const SEO_SCORES_TRANSIENT = 'wpseo_seo_scores';

/**
* Retrieves the current SEO scores for a content type.
*
Expand All @@ -24,13 +27,22 @@ class SEO_Scores_Collector implements Scores_Collector_Interface {
*/
public function get_current_scores( array $seo_scores, Content_Type $content_type, ?int $term_id ) {
global $wpdb;

$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 ( $transient !== false ) {
return \json_decode( $transient, false );
}

$select = $this->build_select( $seo_scores );

$replacements = \array_merge(
\array_values( $select['replacements'] ),
[
Model::get_table_name( 'Indexable' ),
$content_type->get_name(),
$content_type_name,
]
);

Expand All @@ -52,6 +64,7 @@ public function get_current_scores( array $seo_scores, Content_Type $content_typ
)
);
//phpcs:enable
\set_transient( $transient_name, WPSEO_Utils::format_json_encode( $current_scores ), ( \MINUTE_IN_SECONDS ) );
return $current_scores;

}
Expand Down Expand Up @@ -81,6 +94,7 @@ public function get_current_scores( array $seo_scores, Content_Type $content_typ
)
);
//phpcs:enable
\set_transient( $transient_name, WPSEO_Utils::format_json_encode( $current_scores ), ( \MINUTE_IN_SECONDS ) );
return $current_scores;
}

Expand Down

0 comments on commit 9ad483b

Please sign in to comment.